Easier way to debug a Windows service
If I want to quickly debug the service, I just drop in a Debugger.Break()
in there. When that line is reached, it will drop me back to VS. Don't forget to remove that line when you are done.
UPDATE: As an alternative to #if DEBUG
pragmas, you can also use Conditional("DEBUG_SERVICE")
attribute.
[Conditional("DEBUG_SERVICE")]
private static void DebugMode()
{
Debugger.Break();
}
On your OnStart
, just call this method:
public override void OnStart()
{
DebugMode();
/* ... do the rest */
}
There, the code will only be enabled during Debug builds. While you're at it, it might be useful to create a separate Build Configuration for service debugging.
How do I debug Windows services in Visual Studio?
You can also try this.
- Create your Windows service and install and start…. That is, Windows services must be running in your system.
- 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.
- Click the Attach button
- Click OK
- Click Close
- Set a break point to your desirable location and wait for execute. 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".)
How do you debug a Windows Service?
You could use a parameter to let your application decide whether to start as service or regular app (i.e. in this case show a Form or start the service):
static void Main(string[] args)
{
if ((1 == args.Length) && ("-runAsApp" == args[0]))
{
Application.Run(new application_form());
}
else
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[] { new MyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
Now if you pass the parameter "-runAsApp" you can debug the application normally - the SCM won't pass this parameter, so you can also use it as service w/o any code change (provided you derive from ServiceBase
)
Edit:
The other difference with windows services is identity (this might be especially important with InterOp) - you want to make sure you are testing under the same identity in "app" mode as well as service mode.
To do so you can use impersonation (I can post a C# wrapper if it helps, but this can be easily googled) in app mode to use the same identity your windows service will be running under i.e. usually LocalService or NetworkService.
If another identity is required you can add settings to the app.config that allow you to decide whether to use credentials, and if so which user to impersonate - these settings would be active when running as app, but turned off for the windows service (since the service is already running under the desired identity):
<appSettings>
<add key="useCredentials" value="false"/>
<add key="user" value="Foo"/>
<add key="password" value="Bar"/>
</appSettings>
Debug Windows Service
I recommend following pattern for debug:
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 that your target is Console Application, not Windows Application, otherwise it will not work.
Unable to debug windows service - The breakpoint will not be currently hit
Or you could specify the path to your Symbols for Visual Studio to find.
Tools > Options > Debugging > Symbols > Add in the directory of your pdb files.
Related Topics
C# - Which Is the Best Alternative to 'Switch on Type'
What Are the Differences Between the Xmlserializer and Binaryformatter
Is There a C# Type for Representing an Integer Range
Copying Free Hand Drawing from Panel in Visual Studio 2013
Best Practice for Exception Handling in a Windows Forms Application
Windows 7 Progress Bar in Taskbar in C#
How to Render an ASP.NET MVC View in PDF Format
How to Test Private Methods with Nunit
Drawing on Top of Controls Inside a Panel (C# Winforms)
Trim Last Character from a String
Prevent C# App from Process Kill
Why Does System.Threading.Timer Stop on Its Own
Returning a String from a C# Dll with Unmanaged Exports to Inno Setup Script
The Requested Resource Does Not Support Http Method 'Get'
Using Iconfiguration in C# Class Library
How to Debug into My Nuget Package Deployed from Teamcity
Determine Client's Computer Name
Error: "The Node to Be Inserted Is from a Different Document Context"