开发者

How to start a windows service in Visual Studio 2008?

IN Visual Studio when I try to start a windows service project it tells me I cant because I have to use "NET Start" and so forth.

I remember in VS 2003 that when I pressed play it started the service and stop stopped it. Is there any way that when I press play or start for that windows service project I can have this same functionality.

What I currently do is install them using installutil and I put 开发者_如何学Ca pre-processor command with System.Diagnostics.Debug.Launch() when I have a compilation variable defined and when I use the service manager it shows me the window to select the debugger. Still this method is somewhat cumbersome.

For anyone else reading this, remember to try to debug ONE thread at a time.


I usually allow for a command line switch that I can pass to my service using the command line argument settings in the IDE. When this switch is on I can run my service as a regular app. The only issue here is that you need to remember that services usually run under accounts with restricted permissions, so debugging as an app in your user context may behave differently when accessing secured resources. Here is example code:

static void Main()
{
    if (IsDebugMode())
    {
        MyService svc = new MyService();
        svc.DebugStart();

        bool bContinue = true;
        MSG msg = new MSG();

            // process the message loop so that any Windows messages related to
            // COM or hidden windows get processed.
        while (bContinue && GetMessage(out msg, IntPtr.Zero, 0, 0) > 0)
        {
            if (msg.message != WM_QUIT)
                DispatchMessage(ref msg);
            else
                bContinue = false;
        }
    }
    else
    {
        ServiceBase.Run(new MyService());
    }
}

public void DebugStart()
{
this.OnStart(null);
}

static bool IsDebugMode()
{
    return (System.Environment.CommandLine.IndexOf("debug") > -1);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜