【问题标题】:Debug a Windows Service with WCF library使用 WCF 库调试 Windows 服务
【发布时间】:2013-08-13 16:07:13
【问题描述】:

由于某种原因,我无法将 Visual Studio 2010 调试器附加到使用 WCF 服务库的服务中,我遵循了来自 here 的 MSDN 教程。当我安装并运行该服务时,它工作得很好,并且我可以从客户端 GUI 访问所有内容,但是当我尝试将调试器附加到服务时,我得到以下 WCF 服务主机窗口,该窗口弹出一个错误:

System.ServiceModel.AddressAlreadyInUseException: There is already a listener on IP endpoint 0.0.0.0:8000.  Make sure that you are not trying to use this endpoint multiple times in your application and that there are no other applications listening on this endpoint. ---> System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.ServiceModel.Channels.SocketConnectionListener.Listen()
   --- End of inner exception stack trace ---
   at System.ServiceModel.Channels.SocketConnectionListener.Listen()
   at System.ServiceModel.Channels.ConnectionAcceptor.StartAccepting()
   at System.ServiceModel.Channels.ExclusiveTcpTransportManager.OnOpen()
   at System.ServiceModel.Channels.TransportManager.Open(TransportChannelListener channelListener)
   at System.ServiceModel.Channels.TransportManagerContainer.Open(SelectTransportManagersCallback selectTransportManagerCallback)
   at System.ServiceModel.Channels.TcpChannelListener`2.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 Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)
System.Net.Sockets.SocketException (0x80004005): Only one usage of each socket address (protocol/network address/port) is normally permitted
   at System.Net.Sockets.Socket.DoBind(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Bind(EndPoint localEP)
   at System.ServiceModel.Channels.SocketConnectionListener.Listen()

该问题发生在服务中 OnStart() 函数中的 myServiceHost.open() 上,如下所示:

public partial class ORAS : ServiceBase
    {
        System.Timers.Timer aTimer;
        OWcfServiceLibrary.OService ORAS = new OService();
        internal static ServiceHost myServiceHost = null; 
        public ORAS()
        {
            InitializeComponent();
        }

        protected override void OnStart(string[] args)
        {
            System.Diagnostics.Debugger.Launch();
            aTimer = new System.Timers.Timer(60000);//every minute
            aTimer.AutoReset = true;
            aTimer.Elapsed += new System.Timers.ElapsedEventHandler(aTimer_Has_Alarm);
            aTimer.Enabled = true;
            if (myServiceHost != null)
            {
                myServiceHost.Close();
            }
            myServiceHost = new ServiceHost(typeof(OrionService));
            myServiceHost.Open();
        }

【问题讨论】:

  • 您究竟是如何“无法附加”的?错误?消息?我喜欢对服务做的是让它们在交互模式下运行时自动检测(例如,从命令行手动),以便更容易调试它们。更多详情bit.ly/LFEk6d
  • 我可以附加到服务可执行文件,但随后弹出上述消息
  • 这里是您的问题的解决方案stackoverflow.com/questions/283145/…

标签: c# visual-studio-2010 wcf service


【解决方案1】:

在我看来,您对 serviceHost.AddServiceEndpoint() 之类的东西进行了两次尝试,其中第一次是服务本身,然后在您开始调试时再次尝试。

如果您满足以下条件,则可以进入服务代码:

  1. “以管理员身份”运行 Visual Studio。
  2. 让服务运行并准备就绪。
  3. 在调试中启动您的客户端代码,并在调用服务的行上放置一个断点。

当您的客户端代码遇到断点时,使用Step Into (F11) 移动到服务代码中。如果您设置正确,它将无缝转换为您可以继续执行的服务代码。

另一种选择:

  1. 停止服务。
  2. 在您的服务的OnStart() 方法中,首先放置这一行:System.Diagnostics.Debugger.Launch();
  3. 重建然后启动服务
  4. 点击新代码行时,系统会提示您选择调试器...选择已打开的项目

这应该会让你进入新的launch() 行。

【讨论】:

  • 这似乎是一个解决方案,但我的服务没有被客户端调用,而是在服务中执行一些基于计时器的任务,我想调试它自己。所以客户端不只是运行 service.exe
  • 用另一个选项更新答案
  • BUUUUUT,在我的 serviceHost 启动时,问题再次发生。已更新问题以包括此
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-11
  • 2017-04-09
  • 1970-01-01
相关资源
最近更新 更多