42

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

https://learn.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,923
  • 19
  • 51
  • 89

9 Answers9

71

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

Parsa
  • 7,995
  • 2
  • 27
  • 37
31

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,491
  • 6
  • 25
  • 29
Deep2359
  • 321
  • 3
  • 3
17

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,460
  • 1
  • 14
  • 21
  • 1
    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
4

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

4

This works for me with Windows 10

Open CMD Prompt as Administrator and run:

net stop hns
net start hns

Killing processes by port, restarted my Visual Studio, but did not solve my problem.

Jeremy A. West
  • 2,162
  • 4
  • 27
  • 40
1

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

dev_hassan
  • 21
  • 1
  • 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://learn.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,299
  • 14
  • 15
0

This issue is easily resolved by changing the Port number.

0

I had this problem using VS for macOS, however, I was getting sick of VS for macOS's lack of quality, so switched to JetBrains Rider, quit VS, and this particular problem disappeared.

Also, using VS for macOS, the ports specified in launchsettings.json were being ignored, but using Rider, they are being used.

Keith Davidson
  • 740
  • 7
  • 9
  • 1
    I resolved the error running ```dotnet clean``` command and killing all ```dotnet``` processes in ```Activity Monitor``` app – 0xNIC Jul 31 '23 at 12:45