Reputation: 3763
I host my WCF Service with windows service hosting... now when I call my service I cant debug it!Can I debug my service?
Upvotes: 3
Views: 3832
Reputation: 2488
I found a walkthrough here. It suggests adding two methods OnDebugMode_Start and OnDebugMode_Stop to the service (actually exposing OnStart and OnStop protected methods), so the Service1 class would be like this:
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();
}
}
and start it in program like this:
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;
}
}
Configure service in 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>
You're all set.
Upvotes: 0
Reputation: 62157
In addition, consider NOT hosting it in a windows SERVICE during development. Whenever I have a service, I have an alterantive code path to start it as a command line program (if possibly with an /interactive command line parameter etc.) so that I do not ahve to deal with the specifics of Service debugging (need to stop to replace assemblies etc.).
I only turn to "Service" for deployment etc. Debugging is always done in non-service-mode.
Upvotes: 7
Reputation: 81700
Upvotes: 4