none
AddressAlreadyInUseException occurs when restarting windows service RRS feed

  • Question

  • I am getting an AddressAlreadyInUseException error when ever I restart my windows service. Issue is not occurring if the service is stopped first and restarted then. 

    I have googled the problem and tried few suggestions, nothing worked. So I thought to have a fresh perspective from technical experts. Any help would be highly appreciated. 

    Exception: 

    Cannot listen on pipe name 'net.pipe://0.0.0.0/SomeService' because another pipe endpoint is already listening on that name.

    When Starting service, I will open a service host.

    ServiceHost _serverHost = new ServiceHost(this);
    _serverHost.Open();  // Exception occurs here.

    Start Method: 

    public void Start(string serviceName)
            {
            _serviceName = serviceName;
    
            if (_serviceController == null)
            {
                _serviceController = new ServiceController(_serviceName);
            }
    
            try
            {
                switch (_serviceController.Status)
                {
                    case ServiceControllerStatus.Stopped:
                        _serviceController.Start();
                        _serviceController.WaitForStatus(ServiceControllerStatus.Running);
                        break;
                    case ServiceControllerStatus.Paused:
                        _serviceController.Continue();
                        _serviceController.WaitForStatus(ServiceControllerStatus.Running);
                        break;
                }
            }
            catch
            {
            }
        }

    Stop Method:

    public void Stop()
        {
            try
            {
                if (_serviceController == null)
                {
                    _serviceController = new ServiceController(_serviceName);
                }
    
                if (_serviceController.Status == ServiceControllerStatus.Running)
                {
                    _serviceController.Stop();
                    _serviceController.WaitForStatus(ServiceControllerStatus.Stopped);
                }
            }
            catch
            {  
            }
        }

    App.Config:

    <endpoint address="" binding="netNamedPipeBinding" contract="Messages">
            </endpoint>
            <host>
              <baseAddresses>
                <add baseAddress="net.pipe://0.0.0.0/SomeService"/>
              </baseAddresses>
            </host>




    Monday, August 26, 2019 11:17 AM

Answers

  • Hi,

    In my opinion, we should open and close the service host properly in the windows service OnStart event and windows service OnStop event.
    The code segments on this side seem nothing with the service host, it is just in charge of windows service startup and shutdown. The windows service controlled by Service Controller ought to be the root cause that fails to close the service host properly.
    Please consider the below code design.

    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
                {
                    ServiceHost 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.

    Best Regards

    Abraham

    Tuesday, August 27, 2019 6:05 AM
    Moderator

All replies

  • Hi,

    In my opinion, we should open and close the service host properly in the windows service OnStart event and windows service OnStop event.
    The code segments on this side seem nothing with the service host, it is just in charge of windows service startup and shutdown. The windows service controlled by Service Controller ought to be the root cause that fails to close the service host properly.
    Please consider the below code design.

    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
                {
                    ServiceHost 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.

    Best Regards

    Abraham

    Tuesday, August 27, 2019 6:05 AM
    Moderator
  • Hi Abraham,

    Thanks much for replying. I got the issue fixed. While opening the service host, I am checking   if(_serverHost object is null && _serverHost.State == CommunicationState.Opened), if condition is true, I am closing the _serverHost object and initialize the object again. Then I will call _serverHost.Open(), all works good. 

    Now, another issue came up that same issue is happening if we stop and restart the service. Issue is no more when I just restart the service. 

    Any thoughts?

    Wednesday, August 28, 2019 10:43 AM
  • Hi,
    I have a little confused about the way you solved the issue.  Why not add the IF statement to the event that we close the service host? I suggest you add the same detection when we close the service host. 
    Besides, what is the Windows NT service code design? In my opinion, you merely post a management application of the Windows NT service instead of Windows NT Service itself.  I think Windows NT service is the root of the issue.
    Best Regards
    Abraham

    Thursday, August 29, 2019 2:28 AM
    Moderator
  • I have called Dispose() in OnStop() which fixed the issue. Marking it as answer as you have pointed out that OnStop() might be the culprit. Thanks @Abraham for the help. 
    Tuesday, September 10, 2019 7:51 AM