soddoff Baldrick
soddoff Baldrick

Reputation: 186

AspNetCore Httpsys configure useURLS from webapi Appsettings file

I am building a AspNetCore webapi application for internal corporate use and I need to enable Windows Authentication.

So I am creating a httpsys server to listen at a specific endpoint:

    public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
               .UseStartup<Startup>()
               .UseHttpSys(options =>
                {
                    options.Authentication.Schemes =
                            AuthenticationSchemes.NTLM | AuthenticationSchemes.Negotiate;
                    options.Authentication.AllowAnonymous = true;
                    options.UrlPrefixes.Add("http://localhost:16000");
                }).UseUrls("http://localhost:16000");

so while this obviously works fine, I want to be able to configure it in the config file.

Earlier in the project I was using Kestrel, so I just added these settings to the application config:

"Kestrel": {
   "EndPoints": {
     "HttpsInlineCertStore": {
       "Url": "https://*:16000",
        "Certificate": {
          "Subject": "localhost",
          "Store": "My",
          "Location": "LocalMachine",
          "AllowInvalid": "true"
        }
    } ...

Now I understand perfectly that HttpSYS can be configured by the registry etc, so I am not interested in those kinds of responses.

My Specific question is: For a NetCoreApi web api application, is it possible to use the IConfiguration inside the (static) CreateWebHostBuilder method?

I am injecting the IConfiguration into the startup class, but it appears the limitation is in the framework preventing access to it in the CreateWebHostBuilder method. Have I missed something?

Upvotes: 2

Views: 1340

Answers (1)

smoksnes
smoksnes

Reputation: 10851

For a NetCoreApi web api application, is it possible to use the IConfiguration inside the (static) CreateWebHostBuilder method?

Yes, you will be able to access it inside ConfigureServices, which is enough to make your configurations. The overloaded of UseHttpSys actually does the exact same thing.

So basically you just have to configure your HttpSysOptions.

For netcoreapp2.1 :

public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .UseStartup<Startup>()
        .UseHttpSys()
        .ConfigureServices((context, services) =>
        {
            // Option 1. Set options manually.
            services.Configure<HttpSysOptions>(options =>
            {
                // Use context.Configuration to access your config.
                var url = context.Configuration.GetSection("MySection")["Url"];
                options.UrlPrefixes.Add(url);
            });

            // Option 2. Build options from settings.
            services.Configure<HttpSysOptions>(context.Configuration.GetSection("WebSys"));
        });

For netcoreapp3.1:

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
                webBuilder.ConfigureServices((context, services) =>
                {
                    // Option 1. Set options manually.
                    services.Configure<HttpSysOptions>(options =>
                    {
                        // Use context.Configuration to access your config.
                        var url = context.Configuration.GetSection("MySection")["Url"];
                        options.UrlPrefixes.Add(url);
                    });

                    // Option 2. Build options from settings.
                    services.Configure<HttpSysOptions>(context.Configuration.GetSection("HttpSys"));
                });
                webBuilder.UseHttpSys(options =>
                {
                    // Verify that your options is correct here.
                });
            });

In case you want to use option 2, your appsettings.json should look something like this:

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "HttpSys": {
    "AllowSynchronousIO": false,
    "RequestQueueLimit": 2,
    "MaxAccepts": 3
  },
  "AllowedHosts": "*"
}

Note that the property UrlPrefixes in HttpSysOptions is a rather complex object, so I'm not sure if you will be able to serialize it correctly from appsettings. However, you can simply set the field as urls in your config, as mentioned here. Then HttpSys will pick it up as long as your Configuration is correct.

 {
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "urls": "http://*:5005;",
  "HttpSys": {
    "AllowSynchronousIO": false,
    "RequestQueueLimit": 2,
    "MaxAccepts": 3
  },
  "AllowedHosts": "*"
 }

Upvotes: 2

Related Questions