1

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;

        }

    }
poke
  • 369,085
  • 72
  • 557
  • 602
Bercovici Adrian
  • 8,794
  • 17
  • 73
  • 152
  • 1
    Possible duplicate of [Can I set listen URLs in appsettings.json in ASP.net Core 2.0 Preview?](https://stackoverflow.com/questions/44117840/can-i-set-listen-urls-in-appsettings-json-in-asp-net-core-2-0-preview) – poke Dec 20 '18 at 13:57
  • 1
    Your question is answered on this SO question: https://stackoverflow.com/questions/41738692/read-appsettings-json-in-main-program-cs – tech-y Dec 20 '18 at 14:12
  • 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`? – Bercovici Adrian Dec 20 '18 at 14:16

1 Answers1

1

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);
        })
    
itminus
  • 23,772
  • 2
  • 53
  • 88