3

I have .NET service and I need to debug it.

I want it to stop when it reaches some place in code and start the debugger. Can anyone provide some code sample?

UPDATE

Simply adding

Debugger.Launch(); 

does not work for Windows service.

Captain Comic
  • 15,744
  • 43
  • 110
  • 148
  • I've simply added debugger launches to my win services with it successfully working. – Jeff LaFay Oct 04 '10 at 12:36
  • 2 jlafay. Well may be I ve got some permission issue. I am running the service on Windows 2003 server, where I have Visual Studio installed and it does not start the debugger. – Captain Comic Oct 04 '10 at 12:43

4 Answers4

5

Try using Debugger.Break(), as per this answer.

Community
  • 1
  • 1
Nathan Baulch
  • 20,233
  • 5
  • 52
  • 56
4

Add a Debugger.Launch(); to the place in your code where you want to start debugging.

Klaus Byskov Pedersen
  • 117,245
  • 29
  • 183
  • 222
0
  1. Add the following code to the place where you want to stop service.
  2. Run Service.
  3. Attach debugger
  4. Add f to watch, change it value to false
  5. Continue debugging

    bool f=true; while(f) {//<- Add breakpoint here

    }

zabulus
  • 2,373
  • 3
  • 15
  • 27
-1

Here are the steps for that:

1) Stop & uninstall the running service 2) Rename or remove the release version of the service 2) Create a Debug build of the service 3) Install this debug build of service 4) Start the service 5) Open the service solution from Visual studio 6) Select Tools->Attach process 7) In the process list you will see the running service, Attach it 8) Set breakpoints wherever required

prashaNET
  • 51
  • 1
  • 3