Launch ;. If you don't supply a 'start parameter' then your service will run without breaking to the debugger, so no need to ifdef. I still get the error that I can not debug a windows service while using this method, even when changing my output to console application.
ManualReset ; Console. InternalStart ; WaitHandle. Stephen 1, 2 2 gold badges 26 26 silver badges 36 36 bronze badges. Lavanya Lavanya 61 1 1 silver badge 1 1 bronze badge. In your Service1. Very easy. Andrey's approach is also very good. Ronnie Overby Ronnie Overby The most easy way to debug windows service. In order to be able to debug my service without deploying it, I always write it in the following way: In your program. OnDebug ; System.
Sleep System. Hope this helps. Minas Minas 3 3 silver badges 2 2 bronze badges. This is nice, but I find that the first answer does a better test of the OnStart and OnStop methods within the service. Disclaimer: Remember to remove the infinite loop when you want to release a build, or when you simply want to run the service normally.
Sachin Chavan 5, 5 5 gold badges 46 46 silver badges 73 73 bronze badges. Onion-Knight Onion-Knight 3, 7 7 gold badges 29 29 silver badges 34 34 bronze badges. But the VS doesn't let me drag the control outside the loop! Launch ; endif To delete service using SC : sc delete MyService.
Maciej Maciej 7, 1 1 gold badge 29 29 silver badges 34 34 bronze badges. Thread ThreadStart service1 ; service1. Start ;. Dan H Dan H 1, 2 2 gold badges 21 21 silver badges 37 37 bronze badges. OnStart Nothing Application. To add ' another service to this process, change the following line to ' create a second service object. Greg Bogumil Greg Bogumil 1, 15 15 silver badges 22 22 bronze badges.
Michael W 1 1 gold badge 9 9 silver badges 22 22 bronze badges. Efe Ariaroo Efe Ariaroo 9 9 silver badges 10 10 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. Email Required, but never shown. The Overflow Blog. Podcast Making Agile work for data science. Stack Gives Back Featured on Meta.
New post summary designs on greatest hits now, everywhere else eventually. Linked Related While your service is running, go to the Debug menu, click on Attach Process or process in old Visual Studio Find your running service, and then make sure the Show process from all users and Show processes in all sessions is selected, if not then select it.
It will debug automatic whenever your code reaches to that point. Remember, put your breakpoint at reachable place , if it is onStart , then stop and start the service again After a lot of googling, I found this in "How to debug the Windows Services in Visual Studio". Chris Schiffhauer Can't see this option in VS Update 5. But you need to run your Vs as administrator — chozha rajan. I tried this. It worked with a breakpoint in onStop but not on onStart because when the service stops the debugger gets unattached — KansaiRobot.
The service project would be just the shell needed to implement the service part of it. 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! The call stack shows mscorlib. The exception apparently occurs in Debugger. Launch ;. If I choose to continue, I am able to debug the Windows Service, stopping at the specified break points.
How can I use Debugger. See code below Hi, the behavior you mentioned is due to the JIT debugging of Visual studio. It is enabled by default and you can choose a certain debugger from the available debuggers in the dialog list whenever some unhandled exceptions occur.
And you can set one of them as default debugger in the dialog. How to set Visual Studio as the default post-mortem debugger? Windows Server Developer Center.
0コメント