簡體   English   中英

帶有 Autofac 的 ASP.Net Core 3 忽略 iis 設置; 日志顯示“正在監聽:http://localhost:5000”

[英]ASP.Net Core 3 with Autofac ignores iis settings; log displays "Now listening on: http://localhost:5000"

我正在使用 Autofac 運行 Asp.net core 3.0 Web API。 它的入口點如下所示:

using Autofac.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using System;
using System.IO;

namespace Myapp
{
    public class Program
    {
        public static void Main(string[] args)
        {
            var currentDirectory = Directory.GetCurrentDirectory();
            IHostBuilder builder = CreateHostBuilder(currentDirectory, args);
            using IHost host = builder.Build();
            host.Run();
        }

        public static IHostBuilder CreateHostBuilder(string currentDirectory, string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                       .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                       .ConfigureWebHostDefaults(webBuilder =>
                       {
                         webBuilder
                           .UseContentRoot(currentDirectory)
                           .UseIISIntegration()
                           .UseStartup<Startup>();
                       });
        }
    }
}

使用以下 appsettings.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "Authentication": {
    "AllowAnonymous": false
  },
  "Kestrel": {
    "Endpoints": {
      "Http": {
        "Url": "http://localhost:5000"
      },
      "HttpsInlineCertFile": {
        "Url": "https://localhost:5001",
        "Certificate": {
          "Path": "a.pfx",
          "Password": "a"
        }
      }
    }
  }
}

並將其作為控制台應用程序啟動,如下所示:

dotnet myapp.dll

它運行正確。

從 IIS Express 下的 Visual Studio 以調試模式啟動它,它也能正常運行(在 https 端口 44393 上)。

然后我在IIS下配置它。 我將綁定設置為 HTTP 端口 95 和 HTTPS 端口 96,並在 web.config 文件中啟用標准輸出日志。 但是,當嘗試從瀏覽器訪問應用程序時,調用失敗並且 stdout 日志文件顯示:

warn: Microsoft.AspNetCore.DataProtection.Repositories.EphemeralXmlRepository[50]
      Using an in-memory repository. Keys will not be persisted to storage.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[59]
      Neither user profile nor HKLM registry available. Using an ephemeral key repository. Protected data will be unavailable when application exits.
warn: Microsoft.AspNetCore.DataProtection.KeyManagement.XmlKeyManager[35]
      No XML encryptor configured. Key {c74e2418-52e2-492d-b49e-b28c053e4cc3} may be persisted to storage in unencrypted form.
info: Microsoft.Hosting.Lifetime[0]
      Now listening on: http://localhost:5000
2019-12-02 14:14:20,820 INFO  (1) Microsoft.Hosting.Lifetime - Now listening on: http://localhost:5000
info2019-12-02 14:14:20,827 INFO  (1) Microsoft.Hosting.Lifetime - Application started. Press Ctrl+C to shut down.

這(特別是說它正在偵聽端口 5000 的行)看起來與默認配置下顯示的輸出相同(即:未應用配置時)。 因此,在我看來,提供給 IIS 的所有設置都被忽略或沒有得到應用。

錯誤在哪里? 如何解決問題?

在 Program.cs 中,您是否啟用了 IISIntegration?

      webBuilder.ConfigureKestrel()
          .UseIISIntegration() <<-----
          .UseSetting("detailedErrors", "true")
          .CaptureStartupErrors(true)
          .UseStartup<Startup>();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM