Q: Debugging a Service.

  • Thread starter Martin Arvidsson, Visual Systems AB
  • Start date
M

Martin Arvidsson, Visual Systems AB

Hi!

To debug a service i usually attach to the service when it is running.

How ever in this case. the service is not starting due to an error when i
initialize the
service, therefor the service is not started and i can not debug it. (Step
thru code)

Can you "pre" attach to the service before the service is running?

Any other tips on how to debug?

I have began to remove code line by code line, but there is a lot of code :)

Regards
Martin
 
J

Jon Skeet [C# MVP]

To debug a service i usually attach to the service when it is running.

How ever in this case. the service is not starting due to an error when i
initialize the
service, therefor the service is not started and i can not debug it. (Step
thru code)

Can you "pre" attach to the service before the service is running?

Any other tips on how to debug?

I have began to remove code line by code line, but there is a lot of code :)

The simplest way is often to add a long sleep as the very first line
of code, which allows you to attach the debugger to the process while
it's sleeping.

Jon
 
M

Martin Arvidsson, Visual Systems AB

well why didn't i think of that :) Because then i wouldnt ask you *lol*

Thanks
Martin
 
G

Guest

I use the following conditional pattern to debug a Windows Service in C#.
This has the advantage that if you run in debug mode in Visual Studio, the
service never needs to be installed - it is simply run as a "plain old"
executable. No need to attach anything, just set a breakpoint:

#if ( ! DEBUG )

ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] {new TestService()};
ServiceBase.Run(ServicesToRun);

#else

// debug code: allows the process to run as a non-service
// will kick off the service start point, but never kill it
// shut down the debugger to exit

TestService service = new TestService();
service.OnStart(null);
Thread.Sleep(Timeout.Infinite);

#endif
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top