Windows Service Debugging

Scenario

I have a windows service written in C #. I read all google threads on how to debug it, but I still can't get it to work. I ran "PathTo.NetFramework \ InstallUtil.exe C: \ MyService.exe". He said that the installation was successful, but when I start "Services.msc", the service does not appear at all. If I went into the task manager, a process called "MyService.vshost.exe" will appear. Pretty sure that's not it, because it's a service, not a process.

Can someone explain to me?

If I should see the service when starting Services.msc? (Whereas all this is done on a local machine without AT ALL servers.

Other

I am running VS2008.

EDIT:

All this is done on my local computer, I do not have servers or do not have access to them. In addition, I don’t even know what the service does, I want to debug it so that I can skip the code and see how it all works (the code is inside the service, not the service itself - for any of you smart pants that I can offer look at the template).

EDIT 2:

UNDER ANY CIRCUMSTANCES! Every time I try to get something, I get a message about the need to use NET START or install the service.

EDIT 3:

I am running VS2008.

I typed this: C: \ WINDOWS \ Microsoft.NET \ Framework \ v2.0.50727 \ InstallUtil.exe C: \ dev \ Restarter \ bin \ Release \ Restarter.exe

I got this: Microsoft.NET Framework.NET 2.0.050727.3053 Installation Utility Copyright (c) Microsoft Corporation. All rights reserved.

Starting a transactional installation.

Beginning of the installation phase. See Log File Contents for C: \ dev \ Restarter \ bin \ Release \ Restarter.exe. The file is located in the C: \ dev \ Restarter \ bin \ Release \ EDT.Restar ter.InstallLog directory. Installing the assembly 'C: \ dev \ Restarter \ bin \ Release \ Restarter.exe'. Parameters affected are: logtoconsole = assemblypath = C: \ dev \ Restarter \ bin \ Release \ Restarter.exe logfile = C: \ dev \ Restarter \ bin \ Release \ Restarter.InstallLog

The installation phase has completed successfully and the fixing phase begins. See Log File Contents for C: \ dev \ Restarter \ bin \ Release \ Restarter.exe. The file is located in the C: \ dev \ Restarter \ bin \ Release \ Restar ter.InstallLog directory. Committing assembly 'C: \ dev \ Restarter \ bin \ Release \ Restarter.exe'. Parameters affected are: logtoconsole = assemblypath = C: \ dev \ Restarter \ bin \ Release \ Restarter.exe logfile = C: \ dev \ Restarter \ bin \ Release \ Restarter.InstallLog

Successful phase completed.

Completed transaction completed.

C: \ Program Files \ Microsoft Visual Studio 9.0 \ VC>

Then I went to RUN β†’ Services.msc I can not see anything there.

There is a process called "Restarter.vshost.exe" in the task manager.

What is it.

I just wanted to install and debug it. I know that it works (because it works and does not crash). But the code was written by a friend, and I want to understand the basic code by going through it in debug mode.

+41
debugging c # windows-services
Apr 13 '10 at 13:07 on
source share
11 answers

I recommend the following pattern for debugging:

var ServiceToRun = new SomeService(); if (Environment.UserInteractive) { // This used to run the service as a console (development phase only) ServiceToRun.Start(); Console.WriteLine("Press Enter to terminate ..."); Console.ReadLine(); ServiceToRun.DoStop(); } else { ServiceBase.Run(ServiceToRun); } 

Edit: Make sure your goal is a console application, not a Windows application, otherwise it will not work.

+114
Apr 13 '10 at 13:11
source share

You can debug it by attaching a debugger to the process. You can do this by adding a line to the launch of your program:

 Debugger.Launch (); 

after adding the using statement:

 using System.Diagnostics; 

you need to either put this in a conditional block or delete it when you finish debugging

either by starting the service and then connecting to the process manually from the IDE: Debug-> Attach to process ..

+23
Apr 13 '10 at 13:13
source share

We can debug a Windows project project by simply adding a parameter and making it look like a console application.

1) Go to the properties of the Windows service project β†’ Debugging β†’ Launch options 2) Give an argument - Console 3) Go to the Applications tab β†’ output type, change it to the Console application 4) Enter the code below in Program.cs

 static class Program { private static EventWaitHandle _waitHandle; private static Service1 _service; static void Main(string[] args) { bool runConsole = false;** foreach (string arg in args) { if (arg.ToLowerInvariant().Equals("-console")) { runConsole = true; } } _service = new Service1(); if (runConsole) { _waitHandle = new EventWaitHandle(false, EventResetMode.ManualReset); Console.WriteLine("Starting Workflow Service in Console Mode"); Console.WriteLine("Press Ctrl+C to exit Console Mode"); Console.CancelKeyPress += new ConsoleCancelEventHandler(OnCancelKeyPress); _service.InternalStart(); WaitHandle.WaitAll(new WaitHandle[] { _waitHandle }); } ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new Service1() }; ServiceBase.Run(ServicesToRun); } static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e) { _service.InternalStop(); _waitHandle.Set(); } } 
+6
Oct 17 2018-11-11T00:
source share

This helped me a lot when developing / debugging Windows services:

http://windowsservicehelper.codeplex.com/

Just press F5 to debug. Very simple.

Andrew is also very good.

+5
Aug 05 2018-11-11T00:
source share

To be able to debug my service without deploying it, I always write it as follows:

In the program.cs file:

 #if DEBUG MyService myService = new MyService(); myService.OnDebug(); System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite); #else ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new MyService() }; ServiceBase.Run(ServicesToRun); #endif 

and in the file MyService.cs:

  public void OnDebug() { OnStart(null); } 

* NOTE * . You must build in Release mode when you finally finish working with debugging and are ready to deploy the service, otherwise the service will not be considered a service.

Hope this helps.

+4
Mar 26 '14 at 5:31
source share

Assumptions:

1) You have the source code available in the solution in the VS2008 IDE

How to debug C # services:

  • Install the service using InstallUtil . (It seems you already did it)
  • (if necessary) Change the service path to the file MyService.exe, which is created in the Solution bin
  • Put something like the following at the beginning of your OnStart() service:

     while(true) { System.Threading.Thread.Sleep(500); } 
  • Place a breakpoint on System.Threading.Thread.Sleep(500)

  • Build a solution

  • Start the service using the Windows Service Utility

  • While your service starts, in VS2008 goto Debug -> Attach To Processes...

  • Make sure Show Processes From All Users and Show Processes In All Sessions

  • Find your MyService.exe in the list and click Attach

  • You should now be at the breakpoint you inserted in the infinite loop

  • Drag the control (yellow arrow) right over the infinite loop

  • Give up!

Denial of responsibility:

Remember to remove the infinite loop when you want to release the assembly, or when you just want to start the service normally.

+2
Apr 13 '10 at 1:58 p.m.
source share

Perhaps this service name is not what you expect, and why you cannot find it. The service name is defined in the ServiceInstaller properties in the .NET project and must not in any way match the name of the executable file. But if you are sure that the service is not listed after installation, here is what you can do.

Firstly, the installation of the service. There are 2 methods: InstallUtil.exe and SC.exe . The first one is specifically for .NET services, as it will run all ProjectInstaller and ServiceInstaller . Secondly, this will not be done, but it will give you more options and, as a rule, more efficiently, i.e. May succeed where InstallUtil fails. This can be when there is an exception in any installer code.

You have already tried installing using InstallUtil , so here is the SC version:

 sc create MyService binPath= "C:\Service.exe" 

Please note that MyService is the name you are providing the service at this moment, and it can be anything (within reason :-). This name will appear in the list of service consoles.

Once you have installed your service, you will need to debug it right away when OnStart is called. This can be achieved by starting and attaching to the debugger (Visual Studio) from the service:

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

Remember to create and replace the service executable after this code change. The service must be stopped, but it does not need to be removed and reinstalled.

To remove a service using SC :

 sc delete MyService 
+1
Apr 13 '12 at 12:21
source share

If your business layer is separate from the Windows service, you can test all of your business functions outside of the Windows service.

To test a Windows service, I like to create a test project, which is a console application, and I start a new thread that starts my service.

 System.Threading.Thread sftpThread = new System.Threading.Thread((ThreadStart)service1); service1.Start(); 
0
Apr 13 '10 at 13:17
source share

I recently added this to a project and it works great for me. You can debug it just like any other EXE. After adding it, go to your project properties and add the command line option (/ EXE) on the Debug tab to configure the Debug assembly.

 <MTAThread()> _ Shared Sub Main() '' '' let add a command line parameter so we can run this as a regular exe or as a service '' If Command().ToUpper() = "/EXE" Then Dim app As MyService = New MyService() app.OnStart(Nothing) Application.Run() Else Dim ServicesToRun() As System.ServiceProcess.ServiceBase ' More than one NT Service may run within the same process. To add ' another service to this process, change the following line to ' create a second service object. For example, ' ' ServicesToRun = New System.ServiceProcess.ServiceBase () {New Service1, New MySecondUserService} ' ServicesToRun = New System.ServiceProcess.ServiceBase() {New MyService} System.ServiceProcess.ServiceBase.Run(ServicesToRun) End If End Sub 
0
Apr 13 '10 at 13:18
source share

I recommend adding /test to the debug tab of project properties as a startup option. Then you can start the service without having to install it.

0
May 28 '13 at 7:44
source share

If you create your service using TopShelf, you can easily debug it from Visual Studio

0
Apr 12 '17 at 14:42 on
source share



All Articles