Click here to Skip to main content
13,597,485 members
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

14K views
19 bookmarked
Posted 6 Sep 2011
Licenced LGPL3

An Easier Way to Debug Windows Services

, 6 Sep 2011
Rate this:
Please Sign up or sign in to vote.
Here is an easier way to debug windows services

Are you tired of attaching the Visual Studio debugger to the service application? I got the solution just for you! It’s a small helper class containing a static method which you need to invoke.

public static void Main(string[] argv)
{
    // just include this check, "Service1" is the name of your service class.
    if (WindowsServiceHelper.RunAsConsoleIfRequested<Service1>())
        return;

    // all other code
}

Then go to project properties, the “Debug” tab and add “-console” as Command Arguments.

Shows the debug settings under project properties

How to configure Visual Studio

That’s it! What I do is simply allocate a console using the winapi and then invoke (through reflection) the proper protected methods in your service class.

The source code for the helper class is as follows:

public static class WindowsServiceHelper
{
    [DllImport("kernel32")]
    static extern bool AllocConsole();

    public static bool RunAsConsoleIfRequested<t>() where T : ServiceBase, new()
    {
        if (!Environment.CommandLine.Contains("-console"))
            return false;

        var args = Environment.GetCommandLineArgs().Where
			(name => name != "-console").ToArray();

        AllocConsole();

        var service = new T();
        var onstart = service.GetType().GetMethod("OnStart", 
		BindingFlags.Instance | BindingFlags.NonPublic);
        onstart.Invoke(service, new object[] {args});

        Console.WriteLine("Your service named '" + service.GetType().FullName + 
			"' is up and running.\r\nPress 'ENTER' to stop it.");
        Console.ReadLine();

        var onstop = service.GetType().GetMethod("OnStop", 
		BindingFlags.Instance | BindingFlags.NonPublic);
        onstop.Invoke(service, null);
        return true;
    }
} 

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)

Share

About the Author

jgauffin
Founder 1TCompany AB
Sweden Sweden

You may also be interested in...

Pro

Comments and Discussions

 
Questionisn't simpler? Pin
P0110X27-Sep-11 5:13
memberP0110X27-Sep-11 5:13 
AnswerRe: isn't simpler? Pin
jgauffin27-Sep-11 5:52
memberjgauffin27-Sep-11 5:52 
QuestionNice :) Pin
Stuart Blackler7-Sep-11 2:50
memberStuart Blackler7-Sep-11 2:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180621.3 | Last Updated 6 Sep 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid