From 96e4aeaf83adbfe7349cb6f0185d6a72c4cb225a Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Tue, 21 Dec 2021 14:16:06 +0300 Subject: [PATCH 1/2] When IWebHostEnvironment is not injected, hostGetHostingEnvironment now returns an empty one instead of throwing exception --- ...bpAspNetCoreServiceCollectionExtensions.cs | 12 +++++++++++- .../EmptyHostingEnvironment.cs | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs 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..d672952b09 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,6 +8,14 @@ namespace Microsoft.Extensions.DependencyInjection { public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services) { + if (!services.Any(d => d.ServiceType == typeof(IWebHostEnvironment))) + { + return new EmptyHostingEnvironment() + { + EnvironmentName = Environments.Development + }; + } + return services.GetSingletonInstance(); } } 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 From c02a2844ed28fb0d97bf6168143166b000daf272 Mon Sep 17 00:00:00 2001 From: Yunus Emre Kalkan Date: Wed, 22 Dec 2021 12:21:52 +0300 Subject: [PATCH 2/2] Refactor GetHostingEnvironment --- .../AbpAspNetCoreServiceCollectionExtensions.cs | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 d672952b09..41f0a16167 100644 --- a/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs +++ b/framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs @@ -8,7 +8,9 @@ namespace Microsoft.Extensions.DependencyInjection { public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services) { - if (!services.Any(d => d.ServiceType == typeof(IWebHostEnvironment))) + var hostingEnvironment = services.GetSingletonInstanceOrNull(); + + if (hostingEnvironment == null) { return new EmptyHostingEnvironment() { @@ -16,7 +18,7 @@ namespace Microsoft.Extensions.DependencyInjection }; } - return services.GetSingletonInstance(); + return hostingEnvironment; } } }