ASP.Net 具有自动FAC的Core 3忽略IIS设置;日志显示"Now listening on: http://localhost:5000"



我正在使用Autofac运行 Asp.net 核心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的所有设置都被忽略或未应用。

错误在哪里?如何解决问题?

在程序.cs中,您是否启用了 IISIntegration?

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

最新更新