.Net 核心应用 运行 带有 EXE 但在 IIS 上不 运行

.Net core app runs with EXE but does not run on IIS

我有一个 .Net Core 6 应用程序,它在本地 运行 很好,当我在发布文件夹中手动启动 exe 时它也 运行 很好。

但是当我在 IIS 上 运行 时,出现以下错误。

服务器是 AWS EC2 服务器,我打开所有端口作为防火墙上的传入测试,我还尝试完全禁用防火墙并允许应用程序通过防火墙。据我所知,EC2 实例上没有 AV。

我已经添加了 .net core 6 运行time 并且我还安装了 .NET Core 托管包:

https://dotnet.microsoft.com/en-us/download/dotnet/6.0

当我检查事件查看器日志时,我收到此错误消息:

Application: WebApi.exe
CoreCLR Version: 6.0.222.6406
.NET Version: 6.0.2
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.IOException: Failed to bind to address http://localhost:4000.

System.AggregateException: One or more errors occurred. (An attempt was made to access a socket in a way forbidden by its access permissions.) (An attempt was made to access a socket in a way forbidden by its access permissions.)

System.Net.Sockets.SocketException (10013): An attempt was made to access a socket in a way forbidden by its access permissions.

at System.Net.Sockets.Socket.UpdateStatusAfterSocketErrorAndThrowException (SocketError error, String callerName)
at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Bind(EndPoint localEP)
at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportOptions.CreateDefaultBoundListenSocket(EndPoint endpoint) at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketConnectionListener.Bind() at Microsoft.AspNetCore.Server.Kestrel.Transport.Sockets.SocketTransportFactory.BindAsync(EndPoint endpoint, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.Infrastructure.TransportManager.BindAsync(EndPoint endPoint, ConnectionDelegate connectionDelegate, EndpointConfig endpointConfig, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.KestrelServerImpl.<>c__DisplayClass30_0`1.<g__OnBind|0>d.MoveNext() --- End of stack trace from previous location --- at Microsoft.AspNetCore.Server.Kestrel.Core.Internal.AddressBinder.BindEndpointAsync(ListenOptions endpoint, AddressBindContext context, CancellationToken cancellationToken) at Microsoft.AspNetCore.Server.Kestrel.Core.LocalhostListenOptions.BindAsync(AddressBindContext context, CancellationToken cancellationToken) --- End of inner exception stack trace ---

我看过旧版本的 .NetCore 上有一个设置来指定使用 IIS,但我看不到 .Net Core 6 的设置。

我正在努力找出问题所在。

这是我 运行 自己的 exe 时显示的内容,这是正确的。

终于找到原因了

这是因为我的 program.cs 文件底部有这一行:

app.Run("http://localhost:4000");

我将其更改为以下内容,并将 IIS 上的端口更改为 5000,一切都开始工作了

app.Run();