diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs index 33e685ac4e..41f0a16167 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs +++ b/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 { @@ -6,7 +8,17 @@ namespace Microsoft.Extensions.DependencyInjection { public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services) { - return services.GetSingletonInstance(); + var hostingEnvironment = services.GetSingletonInstanceOrNull(); + + if (hostingEnvironment == null) + { + return new EmptyHostingEnvironment() + { + EnvironmentName = Environments.Development + }; + } + + return hostingEnvironment; } } } diff --git a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs new file mode 100644 index 0000000000..371162f0da --- /dev/null +++ b/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; } +} \ No newline at end of file