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

pull/11014/head
Yunus Emre Kalkan 4 years ago
parent 5837a897b5
commit 96e4aeaf83

@ -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<IWebHostEnvironment>();
}
}

@ -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