C# debug windows service startup


















There are a number of restrictions placed on methods that use the Conditional attribute. For example, they must return void and cannot contain out parameters, hence the somewhat verbose method definition. However, this approach does have benefits over the compiler pragmas since you can always verify that your code is currently compiling regardless of what your selected solution configuration is e.

A slight variation on the previous approaches is to check if the current process is running in user interactive mode , rather than relying on compiler constants. The UserInteractive property will return false when a process is running within a host that does not allow user interface components to be displayed. Please note, however, that this approach may not be desirable if you do not want your application to be runnable in production outside the context of the Service Control Manager.

This can be beneficial when you are nearing the end of development work on your project and you want to test that your application is operating correctly when it is running as a bonafide Windows Service. It is also useful if you are experiencing issues in production that can only be reproduced when your Windows Service is running within the context of the Service Control Manager.

The debugger should now be attached to the process and your breakpoints should be hit as they normally would. In summary, I have explained how Windows Services are hosted by the Service Control Manager, making them less straightforward to debug compared to standard Windows and Console applications. I have covered how to install Windows Services using either the InstallUtil or sc utilities.

Lastly, I have demonstrated a few different ways in which you can add support for debugging Windows Services from Visual Studio. Yes, add me to your mailing list. This site uses Akismet to reduce spam. Learn how your comment data is processed. Home Blog About Contact. How to debug a.

Windows Service basics To start with, it is important to understand how Windows Services operate in regards to hosting. Also I used to do the following already mentioned in previous answers but with the conditional compiler [ if] flags to help avoid it firing in a Release build.

I stopped doing it this way because sometimes we'd forget to build in Release and have a debugger break in an app running on a client demo embarrasing! Personally, I'd run the code as a stand-alone program in the debugging phase, and when most bugs are ironed out, change to running as service. What I used to do was to have a command line switch which would start the program either as a service or as a regular application.

With some languages you can actually detect if it's running in an IDE, and perform this switch automatically. I think it depends on what OS you are using, Vista is much harder to attach to Services, because of the separation between sessions.

I like to be able to debug every aspect of my service, including any initialization in OnStart , while still executing it with full service behavior within the framework of the SCM I do this by creating a second service, in the same project, to use for debugging.

The debug service, when started as usual i. This gives you a process to attach the debugger to even though you haven't started your real service yet. After attaching the debugger to the process, start your real service and you can break into it anywhere in the service lifecycle, including OnStart. Because it requires very minimal code intrusion, the debug service can easily be included in your service setup project, and is easily removed from your production release by commenting out a single line of code and deleting a single project installer.

Add both to the ServiceBase array in Program. Both services real and debug get included when you add the service project output to the setup project for the service. After installation, both services will appear in the service.

When I write a service I put all the service logic in a dll project and create two "hosts" that call into this dll, one is a Windows service and the other is a command line application.

I use the command line application for debugging and attach the debugger to the real service only for bugs I can't reproduce in the command line application. I you use this approach just remember that you have to test all the code while running in a real service, while the command line tool is a nice debugging aid it's a different environment and it doesn't behave exactly like a real service.

Makes life much easier. Here is the simple method which I used to test the service, without any additional "Debug" methods and with integrated VS Unit Tests. If the service is called with this parameter, I don't do the usual service startup, but instead start all the listeners and just display a messagebox "Debug in progress, press ok to end".

This way I can easily debug for most small kind problems. I use a variation on JOP's answer. Using command line parameters you can set the debugging mode in the IDE with project properties or through the Windows service manager. For trouble-shooting on existing Windows Service program, use 'Debugger. Break ' as other guys suggested. Also you need to start VS as Administatrator and you need to allow, that a process can automatically be debugged by a diffrent user as explained here :.

Please refer THIS blog post that i created for the topic. Enclose your code in if else block for debug mode and release mode as shown below to switch between debug and release mode in visual studio,.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Easier way to debug a Windows service Ask Question.

Asked 13 years, 3 months ago. Active 1 year, 4 months ago. Viewed k times. Improve this question. I created this User Voice ticket. Consider voting for it: visualstudio. Add a comment. Active Oldest Votes. Improve this answer. Or you could use Debugger. Launch you will have to include a using statement for The Systems. Diagnostics namespace. Your blog post worked just fine and saved my day: however the Debugger. Break didn't worked for me. Net skips the DebugMode function for some optimization-related reasons.

Launch works for me when Debugger. Break doesn't. Process exits with code How are you guys getting this to work? Nothing happens. I've tried Break and Launch. Add the line Debugger.

Launch ; at the beginning of your Main. Build your code in Debug-mode. Overwrite the installed dll's with the debug-dll's. Start the service from the Windows Services panel. Now a popup appears to ask you to attach to a debugger.

This way worked for me. If you are debugging a service that you didn't build, you should first find symbols for the service and make sure they can be found by the debugger. See Specify Symbol. If you're debugging a system process or want to have symbols for system calls in your services, you should add the Microsoft Symbol Servers. See Debugging Symbols. Install your service. For more information, see How to: Install and Uninstall Services. For more information, see How to: Start Services.

On the menu bar, choose Attach to Process from the Debug or Tools menu. In the Available Processes section, choose the process for your service, and then choose Attach. Access the Services Control Manager and manipulate your service, sending stop, pause, and continue commands to hit your breakpoints. Also, see Troubleshooting: Debugging Windows Services. Attaching to the service's process allows you to debug most, but not all, the code for that service.

For example, because the service has already been started, you cannot debug the code in the service's OnStart method or the code in the Main method that is used to load the service this way. One way to work around this limitation is to create a temporary second service in your service application that exists only to aid in debugging. You can install both services, and then start this dummy service to load the service process.

After you are done with diagnosis, uninstall the hook using the —uninstall option — you may always check which hooks are installed with a —list option:. As you can imagine 30s usually is not enough to resolve an issue. Again, you may modify the registry manually or use the winsvcdiag. Topshelf is quite restrictive when it comes to its command line parameters. It also checks if its parent process is services.



0コメント

  • 1000 / 1000