如何修复每次打开服务主机时都会发生的AddressInUseException [英] How to fix AddressInUseException that occurs whenever service host is opened

查看:70
本文介绍了如何修复每次打开服务主机时都会发生的AddressInUseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用WCF Windows应用程序...在重新启动服务时,出现以下异常.

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文件:

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>

启动方法:

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;
            }
    }

停止方法:

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. 

我试图增加两个Stop()方法的WaitForStatus()超时时间,但没有成功. 任何建议都会有很大帮助.

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.

谢谢.

推荐答案

由于您仅发布了Windows NT服务的管理应用程序,因此我认为这方面的任何更改都不会有所帮助.
什么是Windows NT服务代码设计?我认为,我们应该在Windows服务OnStart事件和Windows服务OnStop事件中正确打开和关闭服务主机. Windows NT服务是问题的根源.
此外,我建议在打开服务主机时添加IF语句检测并停止服务主机.
请考虑以下代码.

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.

这篇关于如何修复每次打开服务主机时都会发生的AddressInUseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆