Search code examples
wcfipcnamed-pipes

How to fix AddressInUseException that occurs whenever service host is opened


I am working on WCF windows application... while restarting the service, I am getting the below exception.

outerType: System.ServiceModel.AddressAlreadyInUseException
outerMessage: "Cannot listen on pipe name 'net.pipe://0.0.0.1/MyService' because another pipe endpoint is already listening on that name."
type: System.IO.PipeException

App.Config file:

<services>
  <service name="ServiceName">
    <endpoint address="" binding="netNamedPipeBinding" contract="ServiceClient">
    </endpoint>
    <host>
      <baseAddresses>
        <add baseAddress="net.pipe://0.0.0.1/MyService"/>
      </baseAddresses>
    </host>
  </service>
</services>

Start Method:

public void Start(string serviceName)
    {
        _serviceName = serviceName;
        if (_serviceController == null)
        {
            _serviceController = new ServiceController(_serviceName);
        }

            TimeSpan timeout;
            switch (_serviceController.Status)
            {
                case ServiceControllerStatus.Stopped:
                    _serviceController.Start();
                    timeout = TimeSpan.FromMilliseconds(60000);
                    _serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    break;
                case ServiceControllerStatus.Paused:
                    _serviceController.Continue();
                    timeout = TimeSpan.FromMilliseconds(60000);
                    _serviceController.WaitForStatus(ServiceControllerStatus.Running, timeout);
                    break;
            }
    }

Stop Method:

public void Stop()
    {
            if (_serviceController == null)
            {
                _serviceController = new ServiceController(_serviceName);
            }
            if (_serviceController.Status == ServiceControllerStatus.Running)
            {
                _serviceController.Stop();
                TimeSpan timeout = TimeSpan.FromMilliseconds(20000);
                _serviceController.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
            }
    }

When ever I restarts my service, I will start my Service controller and also open a service host for hosting my service.

_serverHost = new ServiceHost(this); // this points my service name mentioned in App.Config
_serverHost.Open(); // Exception occurs here. 

I tried to increase my time out in WaitForStatus() on both Stop() method but it didn't worked out. Any suggestion will be a great help.

Thanks.


Solution

  • Since you merely post a management application of the window NT service, I don’t think any changes on this side would help.
    What is the Windows NT service code design? In my opinion, we should open and close the service host properly in the windows service OnStart event and windows service OnStop event. Windows NT service is the root of the issue.
    Besides, I suggest add the IF statement detection during opening the service host and stop the service host.
    Please consider the below code.

    public partial class Service1 : ServiceBase
        {
            public Service1()
            {
                InitializeComponent();
            }
            Uri uri = new Uri("net.pipe://localhost/mypipe");
            ServiceHost sh = null;
    
            protected override void OnStart(string[] args)
            {
                NetNamedPipeBinding binding = new NetNamedPipeBinding();
                try
                {
    if (sh == null)
                    {
                        sh = new ServiceHost(typeof(MyService), uri);
                        sh.Open();
                    }            }
                catch (Exception e)
                {
                }
    
            }
    
            protected override void OnStop()
            {
                if (sh != null && sh.State == CommunicationState.Opened)
                {
                    sh.Close();
                }
            }
    

    Feel free to let me know if the problem still exists.