使用WCF库调试Windows服务

时间:2013-08-13 16:07:14

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

由于某种原因,我无法将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();
        }

1 个答案:

答案 0 :(得分:2)

在我看来,你有两次尝试serviceHost.AddServiceEndpoint(),其中第一次是服务本身,然后是你开始调试的时候。

如果您:

,您可以进入服务代码
  1. 以管理员身份运行Visual Studio。
  2. 让服务运行并准备就绪。
  3. 在调试中启动客户端代码,并在调用服务的行上放置一个断点。
  4. 当您的客户端代码命中断点时,请使用Step Into (F11)进入服务代码。如果您设置正确,它将无缝转换为服务代码,您可以继续步进。

    替代方案:

    1. 停止服务。
    2. 在您的服务的OnStart()方法中,首先放置此行:System.Diagnostics.Debugger.Launch();
    3. 重建然后启动服务
    4. 当遇到新的代码行时,系统会提示您选择调试器...选择已打开的项目
    5. 这应该会让你进入新的launch()行。