Browse Source

When IWebHostEnvironment is not injected, hostGetHostingEnvironment now returns an empty one instead of throwing exception

pull/11014/head
Yunus Emre Kalkan 5 years ago
parent
commit
96e4aeaf83
  1. 12
      framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs
  2. 19
      framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs

12
framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs

@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Hosting; using System.Linq;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.DependencyInjection namespace Microsoft.Extensions.DependencyInjection
{ {
@ -6,6 +8,14 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services) public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services)
{ {
if (!services.Any(d => d.ServiceType == typeof(IWebHostEnvironment)))
{
return new EmptyHostingEnvironment()
{
EnvironmentName = Environments.Development
};
}
return services.GetSingletonInstance<IWebHostEnvironment>(); return services.GetSingletonInstance<IWebHostEnvironment>();
} }
} }

19
framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs

@ -0,0 +1,19 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
namespace Microsoft.Extensions.DependencyInjection;
internal class EmptyHostingEnvironment : IWebHostEnvironment
{
public string EnvironmentName { get; set; }
public string ApplicationName { get; set; }
public string WebRootPath { get; set; }
public IFileProvider WebRootFileProvider { get; set; }
public string ContentRootPath { get; set; }
public IFileProvider ContentRootFileProvider { get; set; }
}
Loading…
Cancel
Save