19

I followed the steps from this URL to publish .NET core 2.1 web application code to Linux Centos 7 server.

https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-2.2

I tried to run "sudo dotnet application_name.dll". I received this following error message. Am I missing something? I published code using Visual Studio 2017 on windows machine and copied code to Linux server.

crit: Microsoft.AspNetCore.Server.Kestrel[0]
      Unable to start Kestrel.
System.IO.IOException: Failed to bind to address http://127.0.0.1:5000: address already in use. ---> Microsoft.AspNetCore.Connections.AddressInUseException: Address already in use ---> System.Net.Sockets.SocketException: Address already in use

Program.cs

  public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }

Startup.cs config

 public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        app.UseStaticFiles();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
        });

        app.UseForwardedHeaders(new ForwardedHeadersOptions
        {
            ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
        });
    }

launchSettings.json:

  "applicationUrl": "https://localhost:5001;http://localhost:5000"
nav100
  • 2,651
  • 18
  • 45
  • 87

7 Answers7

21

Surprisingly(or not surprisingly) I restarted my computer and then there was no error :| I checked ports and no app was using port 5000, I don't know what has caused that error

UPDATE :

restarting this service may fix the issue : Host Network Service on windows Services program

Parsa
  • 3,306
  • 1
  • 22
  • 32
14

In my case, I was able to work around this by changing the port Kestrel was using. Here are two options that worked for me (pick one):

  1. Add an "applicationUrl" setting to the launchSettings.json file (found under the Properties folder in VS2019). Using "https://localhost:5201" in the example below:

    "profiles": {
      "IIS Express": {
          ...
          "environmentVariables": {
            "ASPNETCORE_ENVIRONMENT": "Development"
          },
        "Host": {
          ...
        }
      },
      "Your.App.Namespace.Api": {
        ...
        "environmentVariables": {
          "ASPNETCORE_ENVIRONMENT": "Development"
        },
        "applicationUrl": "https://localhost:5201;http://localhost:5200"
      }
    }
    
  2. Add a "UseUrls" fluent method invocation to the WebHostBuilder in Program.Main(). i.e. "http://localhost:5200/" in the example below:

    using (var host = new WebHostBuilder()
    .UseKestrel(o => o.Limits.KeepAliveTimeout = TimeSpan.FromMinutes(30))
    ...
    .UseStartup<Startup>()
    .UseUrls("http://localhost:5200/")
    
Paul Schroeder
  • 1,270
  • 1
  • 12
  • 20
  • I found option #1 (launchsettings) only applies when running from the dev environment. Option #2 works even in deployment – Jason D Feb 04 '22 at 18:13
2

After Windows 10 Update KB4074588, some ports are reserved by Windows and applications cannot bind to these ports. 50067 is in the blocked range.

You can use netsh interface ipv4 show excludedportrange protocol=tcp to list the reserved ranges.

excludedportrange list

enter image description here

ref Issue: https://superuser.com/questions/1486417/unable-to-start-kestrel-getting-an-attempt-was-made-to-access-a-socket-in-a-way

2

Goto Powershell in admin mode and write the following command:

netsh interface ipv4 show excludedportrange protocol=tcp

Check port exclusion ranges whether your port falls under this list; if yes then use the following commands:

net stop winnat
net start winnat

The issue should be resolved.

pheeleeppoo
  • 1,421
  • 6
  • 22
  • 26
Deep2359
  • 21
  • 2
0

The error message is clear: something on your machine is already listening on port 5000.

2 solutions:

  • Find what is listening to this port and stop it.
  • Bind to another port.
StephaneM
  • 4,598
  • 1
  • 15
  • 32
0

I just restart IIS then remove and create a website again and it's works fine!

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Dec 23 '21 at 12:44
0

If you are using Mac "Kestrel doesn't support HTTP/2 with TLS on macOS and older Windows versions such as Windows 7" please see https://docs.microsoft.com/en-us/aspnet/core/grpc/troubleshoot?view=aspnetcore-6.0#unable-to-start-aspnet-core-grpc-app-on-macos for more details. A quick fix (only DEV ) is to remove the HTTPS URL from applicationUrl in the launchsettings.json

...
"applicationUrl": "http://localhost:5126",
...
Juan Carlos Ibarra
  • 1,028
  • 11
  • 14