[C#] IHostEnvironment In ConfigureServices

Macus.y
May 7, 2021

--

Simple way to access IHostEnvironment in ConfigureServices.
You can declare HostEnvironment in global scope. it work for me!

public class Startup
{
public Startup(IConfiguration configuration
, IHostingEnvironment environment)
{
Configuration = configuration;
HostingEnvironment = environment;
}

public IConfiguration Configuration { get; }
public IHostingEnvironment HostingEnvironment { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
if(HostingEnvironment.IsDevelopment()){
Console.WriteLine("IsDevelopment")
}
}
public void Configure(IApplicationBuilder app)
{
if (HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllers();
});
}
}

Done 😎😎

--

--

Macus.y
Macus.y

Written by Macus.y

β€œMany of life’s failures are people who did not realize how close they were to success when they gave up.”– Thomas A. Edison πŸ˜‡πŸ˜‡πŸ˜‡

No responses yet