0

I know this question has been asked 1000 times but I have never seen a good answer. I want to debug my Windows Service and I tried it with attaching visual Studio to it. From there on I have no idea what I should do?

My Service is running, I placed some breakpoints and the Debugger is attached but it never does something. I have a timer in my Service which executes a method at a Special time. I set a breakpoint to that method, my Service executed it but no breakpoint was recognized.

Can someone explain to me how to do it properly or if it is possible step by step with Screen shots.

I really want to do it with the attaching m

Thank you

Mr. Paul
  • 103
  • 3
  • 14
  • for that, you need to create one form in solution and copy service code function to button click event. As I am doing this I can suggest you to do same. – pedram Jan 19 '16 at 13:03
  • 2
    Not really an answer, so you get a comment instead. If you create your services with topshelf (http://topshelf-project.com/) you can start them as normal application and they will work the same as when running as a service, but they are easier to debug – Dimse Jan 19 '16 at 13:03
  • 4
    Possible duplicate of [Easier way to debug a C# Windows Service](http://stackoverflow.com/questions/125964/easier-way-to-debug-a-c-sharp-windows-service), also [Debug Windows Service](http://stackoverflow.com/questions/2629720/debug-windows-service), also [How to debug Windows services in Visual Studio?](http://stackoverflow.com/questions/4678819/how-to-debug-windows-services-in-visual-studio) and [more](https://www.google.co.uk/search?q=debug+windows+service+site:stackoverflow.com) – stuartd Jan 19 '16 at 13:04
  • Ensure you have the full and up to date symbols for the assemblies making up the service: without these VS will not be able to associate source code with the compiled assemblies.Looking at Debug | Windows | Modules will show you what symbols have been found. – Richard Jan 19 '16 at 13:53

2 Answers2

1

In the overridden OnStart() you have to add following conditional block for it:

protected override void OnStart(string[] args)
{
  #if DEBUG
  System.Diagnostics.Debugger.Launch();
  #endif

  // you code here
  // for example you time initialization here
}

See this MSDN link for more details on this.

The above approach is when you want to install it on system and then debug it, if you can also debug it without installing then refer to this article

Ehsan Sajjad
  • 59,154
  • 14
  • 90
  • 146
0

An alternative could be, depending on what your windows service does, use a Unit Test as a "Entry Point" into the application and step through it that way.

[TestMethod]
public void MyServiceEntryPoint()
{
   var service = new Service1();   
}
NikolaiDante
  • 17,673
  • 14
  • 75
  • 112