Is it possible to debug the Windows services in Visual Studio?
I used code like
System.Diagnostics.Debugger.Break();
but it is givi
In the OnStart
method, do the following.
protected override void OnStart(string[] args)
{
try
{
RequestAdditionalTime(600000);
System.Diagnostics.Debugger.Launch(); // Put breakpoint here.
.... Your code
}
catch (Exception ex)
{
.... Your exception code
}
}
Then run a command prompt as administrator and put in the following:
c:\> sc create test-xyzService binPath= <ProjectPath>\bin\debug\service.exe type= own start= demand
The above line will create test-xyzService in the service list.
To start the service, this will prompt you to attach to debut in Visual Studio or not.
c:\> sc start text-xyzService
To stop the service:
c:\> sc stop test-xyzService
To delete or uninstall:
c:\> sc delete text-xyzService
A Microsoft article explains how to debug a Windows service here and what part anyone can miss if they debug it by attaching to a process.
Below is my working code. I have followed the approach suggested by Microsoft.
Add this code to program.cs
:
static void Main(string[] args)
{
// 'If' block will execute when launched through Visual Studio
if (Environment.UserInteractive)
{
ServiceMonitor serviceRequest = new ServiceMonitor();
serviceRequest.TestOnStartAndOnStop(args);
}
else // This block will execute when code is compiled as a Windows application
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new ServiceMonitor()
};
ServiceBase.Run(ServicesToRun);
}
}
Add this code to the ServiceMonitor class.
internal void TestOnStartAndOnStop(string[] args)
{
this.OnStart(args);
Console.ReadLine();
this.OnStop();
}
Now go to Project Properties, select tab "Application" and select Output Type as "Console Application" when debugging, or "Windows Application" when done with debugging, recompile and install your service.
I use a great Nuget package called ServiceProcess.Helpers.
And I quote...
It helps windows services debugging by creating a play/stop/pause UI when running with a debugger attached, but also allows the service to be installed and run by the windows server environment.
All this with one line of code.
http://windowsservicehelper.codeplex.com/
Once installed and wired in all you need to do is set your windows service project as the start-up project and click start on your debugger.
You can also try System.Diagnostics.Debugger.Launch() method. It helps in taking the debugger pointer to the specified location and you can then debug you code.
Before this step please install your service.exe using the command line of Visual Studio command prompt - installutil projectservice.exe
Then start your service from the Control Panel -> Administrative Tools -> Computer Management ->Service and Application -> Services -> Your Service Name
I found this question, but I think a clear and simple answer is missing.
I don't want to attach my debugger to a process, but I still want to be able to call the service OnStart
and OnStop
methods. I also want it to run as a console application so that I can log information from NLog to a console.
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.cs
to look like this:
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main()
{
// Startup as service.
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
if (Environment.UserInteractive)
{
RunInteractive(ServicesToRun);
}
else
{
ServiceBase.Run(ServicesToRun);
}
}
}
Then add the following method to allow services running in interactive mode.
static void RunInteractive(ServiceBase[] servicesToRun)
{
Console.WriteLine("Services running in interactive mode.");
Console.WriteLine();
MethodInfo onStartMethod = typeof(ServiceBase).GetMethod("OnStart",
BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Starting {0}...", service.ServiceName);
onStartMethod.Invoke(service, new object[] { new string[] { } });
Console.Write("Started");
}
Console.WriteLine();
Console.WriteLine();
Console.WriteLine(
"Press any key to stop the services and end the process...");
Console.ReadKey();
Console.WriteLine();
MethodInfo onStopMethod = typeof(ServiceBase).GetMethod("OnStop",
BindingFlags.Instance | BindingFlags.NonPublic);
foreach (ServiceBase service in servicesToRun)
{
Console.Write("Stopping {0}...", service.ServiceName);
onStopMethod.Invoke(service, null);
Console.WriteLine("Stopped");
}
Console.WriteLine("All services stopped.");
// Keep the console alive for a second to allow the user to see the message.
Thread.Sleep(1000);
}