我使用Windows服务托管来托管我的WCF服务...现在当我调用我的服务时,我无法进行调试!我能调试我的服务吗?
我使用Windows服务托管来托管我的WCF服务...现在当我调用我的服务时,我无法进行调试!我能调试我的服务吗?
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>
一切准备就绪。