Tuesday, April 12, 2022

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

Issue

I have a .Net Core 6 app which runs fine locally, it also runs fine when I manually start the exe in the publish folder.

However when I run on IIS, it gives the following error.

enter image description here

The server is an AWS EC2 server, I have all ports open as a test as incomming on the firewall, I have also tried to disable the firewall completely and also allowing the app through the firewall. As far as i'm aware there is no AV on the EC2 instance.

I have added the .net core 6 runtime and I have also installed the .Net core hosting bundle found here:

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

When I have chekced the event viewer logs I get this error message:

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 ---

I have read there is a setting on old versions of .NetCore to specify the use of IIS, but I can't see one for .Net Core 6.

I am pulling my hair out trying to figure out what is wrong.

Here is what is shown when I run the exe on it's own, which is correct.

enter image description here


Solution

I have finally found the reason.

It was due to my program.cs file having this line at the bottom:

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

I changed this to the following and changed the port to 5000 on IIS and it all started working

app.Run();


Answered By - user3284707
Answer Checked By - David Marino (WPSolving Volunteer)