繁体   English   中英

如何调试由 Windows 服务托管的 WCF 服务?

[英]How to debug a WCF service that hosted by Windows Service?

我使用 windows 服务托管来托管我的 WCF 服务...现在当我调用我的服务时我无法调试它?我可以调试我的服务吗?

此外,请考虑在开发期间不要将其托管在 windows SERVICE 中。 每当我有服务时,我都有一个替代代码路径将其作为命令行程序启动(如果可能使用 /interactive 命令行参数等),这样我就不必处理服务调试的细节(需要停止更换组件等)。

我只转向“服务”进行部署等。调试总是在非服务模式下完成。

  1. 以管理模式运行VS
  2. 从调试菜单中选择附加到进程...
  3. 选择您的服务流程
  4. 在您的服务中放置一个断点

Debugger.Launch()一直为我工作。

我在这里找到了演练。 它建议向服务添加两个方法 OnDebugMode_Start 和 OnDebugMode_Stop(实际上公开 OnStart 和 OnStop 受保护的方法),因此 Service1 class 将是这样的:

public partial class Service1 : ServiceBase
{
    ServiceHost _host;
    public Service1()
    {
        InitializeComponent();
    }

    protected override void OnStart(string[] args)
    {
        Type serviceType = typeof(MyWcfService.Service1);
        _host = new ServiceHost(serviceType);
        _host.Open();
    }

    protected override void OnStop()
    {
        _host.Close();
    }

    public void OnDebugMode_Start()
    {
         OnStart(null);
    }

     public void OnDebugMode_Stop()
     {
         OnStop();
     }
}

并在这样的程序中启动它:

static void Main()
{
    try
    {
#if DEBUG
        // Run as interactive exe in debug mode to allow easy debugging. 

        var service = new Service1();
        service.OnDebugMode_Start();
        // Sleep the main thread indefinitely while the service code runs in OnStart() 
        System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
        service.OnDebugMode_Stop();
#else
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
                           { 
                                     new Service1() 
                           };
        ServiceBase.Run(ServicesToRun);
#endif
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

在 app.config 中配置服务:

<configuration>
<system.serviceModel>
<services>
  <service name="MyWcfService.Service1">
    <endpoint address="" binding="basicHttpBinding" bindingConfiguration=""
      contract="MyWcfService.IService1">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
      contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8732/Design_Time_Addresses/MyWcfService/Service1/" />
      </baseAddresses>
    </host>
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior>
      <serviceMetadata  httpGetEnabled="True"  policyVersion="Policy15"/>
      <serviceDebug includeExceptionDetailInFaults="True" />
    </behavior>
  </serviceBehaviors>
</behaviors>

一切就绪。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM