Of course, before update your code and recompile your service, you must at least stop your service with "net stop". This whole procedure is sometimes tedious, and also poses a problem in debugging, if the service does not correctly startup, you haven't time to attach the debugger to trace what happens.
The idea to help us debugging is to create a console application that simulate the service execution when it run under Visual Studio. After, the idea is to check if we are in service mode or debug mode, for this we need to known if we are in interactive mode, we use the Environment. UserInteractive property for that, and we need to know if we are in debug mode with the System. IsAttached property. Another advantage is that you can create a log system that display message on the console, it's direct and readable.
You can resolve this problem, by running Visual Studio as Administrator, and the when the service is launched it get the administrator rights. A service, when it starting, do some tasks create an event log source, etc. It's your job to d all it's needed for your service. Caution, this mode don't make you free to debug as service mode, it allows you to quickly debug your service, but your to test in service mode to ensure the correct behavior of the service in is normal mode.
A service for running, need to be installed it is registered to the Windows ServiceManager. To install a. Net Service we need to run the command "InstallUtil. Net Framework folder. This command is sometimes a bit long to write, even when it is necessary to install the service with an installer, it is necessary to locate the folder of the corresponding framework, etc.
As we have now a console application, we can use it to facilitate the work. ManagedInstallerClass that contains some helpers methods to do it. We will change our console application to supports 'commands' like install and uninstall.
For the same reasons as before, we will use some command line arguments of our console application. And for this we use the System. ServiceController class. So we add deux commands start and stop between our two commands insall and uninstall. How we process the commands, allows us to combine installation and startup in one command line. Same for stop and uninstall. The order of the commands in the command line is not important, because we process the commands in the good order we test 'stop' before 'uninstall'.
We can create a command to process in one command the installation and the startup. Lasse V. Karlsen Lasse V. Karlsen k 95 95 gold badges silver badges bronze badges. Ya that only I am doing. Is it not possible to attach any process to debug or any other option rather than separate project.? Of course it is possible, but it's much easier to develop a windows service if you take out the service part during development.
I thought any other way would exist. I don't see how it would "double" the work. Sure, it'll add a small bit of overhead in making the extra project and separating out the code in the service to a third project, but other than that, you wouldn't make a copy of the code, you would move it, and reference that project.
It double the work to managing the service, which is pretty much zero in terms of development time, and you only do it once. Show 1 more comment. The simplest is while! Kirill: Use tildes to highlight code inside comments, e. Instance BindingFlags. Since OnStart is supposed to return quickly, you shouldn't need to do it in another thread.
However, if the service doesn't start another thread, your process will exit immediately. MattConnolly On the latter: if necessary, I change the above code to start a foreground thread that sleeps forever before the normal processing. This should be a real answer. Works beautifully! Below is my working code. I have followed the approach suggested by Microsoft. Add this code to program. OnStart args ; Console. ReadLine ; this. You can make a console application.
ReadKey ; if key. It's 2 differents projects with similar classes. In my case, it's a simple service with only the ImportFileService class wihch are duplicate.
Like Lasse V. Karlsen said, it's a debug program, all the logic business is on a third project. Yes, is is. It's why I said "except the inheritant ServiceBase. I find it easier to debug in a Console App, but I understand if that doesn't convince everyone. I found this question, but I think a clear and simple answer is missing. I found these brilliant guides that does this: Debugging a Windows Service Project Easier way to debug a Windows service Start by changing the projects Output type to Console Application.
Change your Program. WriteLine "Services running in interactive mode. ServiceName ; onStartMethod. WriteLine ; Console.
WriteLine "Press any key to stop the services and end the process ReadKey ; Console. ServiceName ; onStopMethod. Invoke service, null ; Console. WriteLine "All services stopped. Ogglas Ogglas Great Code! Simple, effective.
But just as easy I made this a Forms app. I really hate console apps. Also, you can easily implement a Forms Button for each service event. I use a great Nuget package called ServiceProcess.
And I quote All this with one line of code. Christophe Chang Christophe Chang 1 1 silver badge 8 8 bronze badges. Thanks for sharing! 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. Service registration When developing a Windows Service using C , as per most. This is shown in the code sample below. Run service ; After the code calls the Run method, a dialog box similar to the following will appear. Service installation Before moving on to the next section, if you do want to install your Windows Service there are two main options.
InstallUtil Firstly, you can use InstallUtil , as recommended by the dialog box message shown further above. The InstallUtil executable is usually found at the following location.
Debug service method Probably the easiest option for adding debugging support to a Windows Service is to create a debugging entry point method in the service class we have implemented. We can then control access to the debugging entry point via a conditional compiler pragma check. OnDebug ; Thread. Sleep Timeout. Run service ; endif Note the code highlighted in grey. The above code first of all creates a new instance of our service object.
0コメント