Browse Source

Merge pull request #11014 from abpframework/empty-hosting-environment

When IWebHostEnvironment is not injected, hostGetHostingEnvironment will return an empty one instead of throwing exception
pull/11031/head
Yunus Emre Kalkan 5 years ago
committed by GitHub
parent
commit
a8e714972d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 16
      framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/AbpAspNetCoreServiceCollectionExtensions.cs
  2. 19
      framework/src/Volo.Abp.AspNetCore/Microsoft/Extensions/DependencyInjection/EmptyHostingEnvironment.cs

16
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,7 +8,17 @@ namespace Microsoft.Extensions.DependencyInjection
{ {
public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services) public static IWebHostEnvironment GetHostingEnvironment(this IServiceCollection services)
{ {
return services.GetSingletonInstance<IWebHostEnvironment>(); var hostingEnvironment = services.GetSingletonInstanceOrNull<IWebHostEnvironment>();
if (hostingEnvironment == null)
{
return new EmptyHostingEnvironment()
{
EnvironmentName = Environments.Development
};
}
return hostingEnvironment;
} }
} }
} }

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