首页 > 解决方案 > AspNetCore Httpsys 从 webapi Appsettings 文件配置 useURLS

问题描述

我正在构建一个供内部企业使用的 AspNetCore webapi 应用程序,我需要启用 Windows 身份验证。

所以我正在创建一个 httpsys 服务器来监听特定的端点:

    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");

因此,虽然这显然可以正常工作,但我希望能够在配置文件中对其进行配置。

在项目的早期,我使用的是 Kestrel,所以我只是将这些设置添加到应用程序配置中:

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

现在我完全明白 HttpSYS 可以由注册表等配置,所以我对这些类型的响应不感兴趣。

我的具体问题是:对于 NetCoreApi Web api 应用程序,是否可以在(静态)CreateWebHostBuilder 方法中使用 IConfiguration?

我将 IConfiguration 注入到启动类中,但似乎限制在框架中,阻止在 CreateWebHostBuilder 方法中访问它。我错过了什么吗?

标签: asp.net-core-2.0

解决方案


对于 NetCoreApi Web api 应用程序,是否可以在(静态)CreateWebHostBuilder 方法中使用 IConfiguration?

是的,您将能够在内部访问它ConfigureServices,这足以进行配置。的重载UseHttpSys实际上做了完全相同的事情。

所以基本上你只需要配置你的HttpSysOptions.

对于 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"));
        });

对于 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.
                });
            });

如果你想使用选项 2,你appsettings.json应该看起来像这样:

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

请注意,in 中的属性UrlPrefixesHttpSysOptions一个相当复杂的对象,因此我不确定您是否能够从appsettings. urls但是,您可以像在配置中一样简单地设置该字段,如此所述。然后HttpSys只要你Configuration是正确的就会拿起它。

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

推荐阅读