How to fix "HTTP could not register URL http://... because TCP port xxx is being used by another application"?

John Thompson 1 Reputation point
2022-08-09T13:37:47.263+00:00

From: https://stackoverflow.com/q/73283828/1188758

I'm getting the exception:

HTTP could not register URL http://+:50005/JTCrawlerAppServer/ because TCP port 50005 is being used by another application.: The process cannot access the file because it is being used by another process

This is running in a WPF app. It ran fine last week, and runs fine on another computer, so I don't know what changed. I've rebooted, run it as administrator, tried a different port number, and several other suggestions from StackOverflow, but no change. Running "netstat -o -n -a" shows no one else on that port. Running "netsh http show url" shows that I should have permission:

Reserved URL : http://+:50005/
User: \Everyone
Listen: Yes
Delegate: No
SDDL: D:(A;;GX;;;WD)

Here's the relevant app.cfg and C# code:

<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="JTCrawlerApp.JTCrawlerAppService">
<endpoint address="" binding="basicHttpBinding" contract="JTCrawlerApp.IJTCrawlerAppService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<!-- Don't let this through the firewall!! -->
<add baseAddress="http://localhost:50005/JTCrawlerAppServer" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>

public class RunWebServiceCommand : BaseCrawlerCommand
{
// Implementatiion data.
public static string ServerEndpoint = "JTCrawlerAppServer";
protected ServiceHost Host;
// ...
protected bool OnInitializeWebService()
{
try
{
Host = new ServiceHost(typeof(JTCrawlerAppService));

        // Service host is opened on the UI thread  
        Host.AddServiceEndpoint(  
            typeof(IJTCrawlerAppService),  
            new WSHttpBinding(),  
            ServerEndpoint);  

        Host.Open();  

        PutLogMessage("RunWebServiceCommand: Service started.");  

        return true;  
    }  
    catch (Exception exc)  
    {  
        PutExceptionErrorLogMessage("Error starting JTCrawlerApp web service", exc);  
    }  

    return false;  
}  

}
Here is the stack trace from the exception:

at System.ServiceModel.Channels.SharedHttpTransportManager.OnOpen()
at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
at System.ServiceModel.Channels.TransportChannelListener.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.HttpChannelListener`1.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Dispatcher.ChannelDispatcher.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
at System.ServiceModel.Channels.CommunicationObject.Open()
at JTCrawlerApp.Commands.RunWebServiceCommand.OnInitializeWebService() in C:\JTLanguage\JTCrawlerApp\Commands\RunWebServiceCommand.cs:line

It's curious the duplicate CommunicationObject.Open calls. Is it because it's switching to another thread?

Developer technologies | .NET | Other
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jiayao Wu 1 Reputation point
    2022-08-10T03:20:47.017+00:00

    Hi @John Thompson ,

    If you still want to use port 50005, try the following steps:

    1. Used in the CMD command netstat aon | findstr searches "50005", will get a PID.
    2. Find the process corresponding to the PID in the task manager and terminate it.
    3. Try again to see if port 50005 can be used.

    If there are other important programs on this port, you can try testing on another port.

    Best Regards,
    Jiayao Wu

    --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.