Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,I have created a windows service using c#,vs 2008.it is build and deployed successfully.i am trying to debug the service.by pressing debug-->attach process-->and attaching the service all the breakpoints disabled.i am running vs2008 in admin mode.how do i debug the service?
Posted

It is really simple do make the executable assambly containing the service to have dual functionality: to run as service, and to run as console application. In this later case it is as easy to debug as any other console application. And by the way the assambly will be self-installing.
I found it originally here: https://groups.google.com/forum/?hl=en&fromgroups#!topic/microsoft.public.dotnet.languages.csharp/TUXp6lRxy6Q[^], see Marc Gravell's post. I have made my adaptation of it, a little easyer to read in my article[^] under The service section.
 
Share this answer
 
You need to add a delay in the windows service start-up to give you enough time to attach to the process.

C#
protected override void OnStart(string[] args)
{
        #if DEBUG
	// When debugging a service, you have to attach to a running process. This gives me 
	// time to attach for debugging to slowdown startup so I can set a breakpoint. 
	System.Threading.Thread.Sleep(15000);
	#endif
}
 
Share this answer
 
Comments
John Sathish Tamilarasu 14-Jun-12 5:47am    
Hi Thanks for the reply.I have developed this service and build in release mode without
#if DEBUG
System.Threading.Thread.Sleep(15000);
System.Diagnostics.Debugger.Launch();
#endif
code.and deployed the sevice.
then i added the same code in onstart function and in debug mode i am trying to attach the same happens.is the method i am doing right?
db7uk 14-Jun-12 5:54am    
sorry, not quite clear. Are you trying to debug your live (release) instance? If so remove the #if and just use the thread.sleep. I would also remove the debugger.launch method too.
John Sathish Tamilarasu 14-Jun-12 5:58am    
protected override void OnStart(string[] args)
{

System.Threading.Thread.Sleep(15000);



EventLog.WriteEntry("BioMetricService Started.", EventLogEntryType.Information);

System.Timers.Timer GetdataTimer = new System.Timers.Timer();
GetdataTimer.Interval = 1000;
// Hook up the Elapsed event for the timer.
GetdataTimer.Elapsed += new ElapsedEventHandler(GetdataTimer_Tick);
// Set the Interval to 2 seconds (2000 milliseconds).
//GetdataTimer.Interval = 2000;
GetdataTimer.Enabled = true;
GetdataTimer.Start();
//Logger.writeToLogFile("BioMetricService Started");
}


protected override void OnStop()
{
Logger.writeToLogFile("BioMetricService Stopped");
}

protected override void OnPause()
{
}

private void GetdataTimer_Tick(object sender, EventArgs e)
{

Logger.writeToLogFile("good");

}


This is my code.when i deploy the same and trying to attach still breakpoints are not enabled.
Your help is valuable
db7uk 14-Jun-12 6:02am    
How are you attaching to the process? Have you checked the box "Show processes from all users" and "Show processes in all sessions"? You service maybe being launched under a different user / session which is why the attach process does nothing???
John Sathish Tamilarasu 14-Jun-12 6:08am    
Yes both the check boxes are checked,then only the process is displayed.but not able to debug?
A professional solution is described in a CodeProject article: Debugging Windows Services under Visual Studio .NET[^]
 
Share this answer
 
Dear,

Use
C#
"Debugger.Launch();"
At start of the Function when Sevice Called.

Thanks
Ronal
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900