C# Self Hosted Web - TCP Port used by another application (win10)

Just start to get an exception when trying to use a self-hosted webapi application in Windows 10 1703 (15063.483)

VS2017: Version 15.2 (26430.15) Release VisualStudio.15.Release/15.2.0+26430.15

I tried running VS in administrator mode, with the same result as non-Admin mode. My app is trying to use http://192.168.12.118:50231 The project is a .NET 4.5 WPF application.

After the app failed the first time, I did:

Here's the code I am using:

            HttpSelfHostConfiguration config = new HttpSelfHostConfiguration(new Uri("http://" + MyIP + ":" + MyPort));
            config.MaxReceivedMessageSize = Int32.MaxValue - 1000;
            config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;              
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            // Startup server 
            server = new HttpSelfHostServer(config);
            server.OpenAsync().Wait();          

This is the exception I am getting, when execution hits the OpenAsync:

.Inner Type: AddressAlreadyInUseException
.Inner Message: HTTP could not register URL http://+:50231/ because TCP port 50231 is being used by another application.
.Inner Stack:    at System.Runtime.AsyncResult.End[TAsyncResult](IAsyncResult result)
at System.ServiceModel.Channels.CommunicationObject.EndOpen(IAsyncResult result)
at System.Web.Http.SelfHost.HttpSelfHostServer.OpenListenerComplete(IAsyncResult result)

 ..Inner Type: HttpListenerException
 ..Inner Message: The process cannot access the file because it is being    used by another process
 ..Inner Stack:    at System.Runtime.AsyncResult.End[TAsyncResult]    (IAsyncResult result)
   at System.ServiceModel.Channels.CommunicationObject.EndOpen(IAsyncResult result)
   at System.Web.Http.SelfHost.HttpSelfHostServer.OpenListenerComplete(IAsyncResult result)`

TIA

Upvotes: 3

Views: 1581

Answers (1)

"Did you reboot?" That did it. Doesn't make sense to me, as there was nothing listed as using that port ... oh well.

Upvotes: 4

Related Questions