Bercovici Adrian
Bercovici Adrian

Reputation: 9360

Setting url of application from appsettings

Hello i need to use the appsettings (or another jsonfile) before the Startup class is initialized , specifically in the CreateWebHostBuilder that is called in Program.Main . I want to set the UseUrls(url) for the application. I want to somehow reuse the same resource when using the IConfiguration in the Startup class.

How can achieve this ?

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

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) {

            WebHostBuilder builder = new WebHostBuilder();
            builder.UseStartup<Startup>();
            //load the Port and Host from appsettings
            var url =$"http://{appsettings.host}:{appsettings.port}/";
            Debug.WriteLine(url);
            builder.UseKestrel().UseUrls(url);
            return builder;

        }

    }

Upvotes: 1

Views: 9378

Answers (1)

itminus
itminus

Reputation: 25350

I know there're two great answers by @Ígor Krug and @Tseng (and I've voted up for the two answers). The explanation below is just to answer the OP's question in the comment. (I tried to file a comment , but it's awful to paste so much words in a comment)


I do not understand : so you create a Configuration in the main method but how do you bind it to the application so that you can later refer it in the Startup?

As you know, the Startup doesn't care about how the Configuration is constructed. If you want to share the configuration with host builder and application (including Startup), there're two ways to do that.

  1. As @Tseng does, simply invoke .UseConfiguration(config). The .UseConfiguration method will specify a configuration for host builder, and also this configuration will be reused by application. As for your original question, add an urls:"https://your.host.name:port" setting in your appsettings.json:

    {
        "urls": "http://localhost:8809",
         ...
    }
    

    and simply copy Tseng's answer with a little change:

    WebHostBuilder builder = new WebHostBuilder();
    builder.UseStartup<Startup>();
    
    var config = new ConfigurationBuilder()
        .SetBasePath(Directory.GetCurrentDirectory())
        .AddJsonFile("appsettings.json", optional: false)
        // ...
        .Build();
    
    return builder
         // the `config` will be reused by application (including Startup)
        .UseConfiguration(config)      
        .UseKestrel();
    
  2. Or if you would like to add a separate configuration for your application (including Startup), you could invoke .ConfigureAppConfiguration(c=>{ /*...*/ }) . This part of configuration will not be shared with host builder :

    return builder
        // .UseConfiguration(config) 
        .UseUrls(config["urls"])
        .UseKestrel()
        .ConfigureAppConfiguration(c =>{
            c.AddConfiguration(config);
        })
    

Upvotes: 1

Related Questions