Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use IWebHostEnvironment in Program.cs web host builder in ASP.NET Core 3.1

I have an ASP.NET Core 3.1 application, and I need to access the IWebHostEnvironment in the Program.cs web host builder. This is what I have right now (Program.cs):

public class Program {

    public static void Main(string[] args) {
        CreateWebHostBuilder(args).Build().Run();
    }

    private static bool IsDevelopment => 
        Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development";

    public static string HostPort => 
        IsDevelopment 
            ? "5000" 
            : Environment.GetEnvironmentVariable("PORT");

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseUrls($"http://+:{HostPort}")
            .UseSerilog((context, config) => {
                config.ReadFrom.Configuration(context.Configuration);
            })
            .UseStartup<Startup>();
}

Instead of doing the IsDevelopment check the way I do it, I would like to use the IsDevelopment() method from IWebHostEnvironment, not entirely sure how to do that. What I have right now works, but honestly it doesn't "feel right".

like image 505
Dimitar Dimitrov Avatar asked Nov 30 '25 11:11

Dimitar Dimitrov


2 Answers

In .NET 6 you can use the new minimal hosting model.

In your main program, you can create an application builder and an application. The Environment (as a IWebHostEnvironment) is available on both objects.

For example in Program.cs:

var builder = WebApplication.CreateBuilder(args);
var environment = builder.Environment;

Or:

var app = builder.Build();
var environment = app.Environment;

You may want to pass the environment to ConfigureServices like this:

var startup = new Startup(builder.Configuration);
startup.ConfigureServices(builder.Services, builder.Environment);

(PS: I don't think this code is dependent on using the minimal hosting model, but that makes it simpler. I think the Environment properties are also available back to .NET 3.1 which is the original question.)

like image 169
Jess Avatar answered Dec 02 '25 00:12

Jess


If you want to use the IWebHostingEnvironment you should get it from the context parameter of ConfigureAppConfiguration

 public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureAppConfiguration((context, builder) =>
                {
                    var isDev = context.HostingEnvironment.IsDevelopment();
                    var isProd = context.HostingEnvironment.IsProduction();
                    var envName = context.HostingEnvironment.EnvironmentName;
                })
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
like image 23
Zinov Avatar answered Dec 02 '25 01:12

Zinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!