开发者

Windows service Debug Issue

I have created windows service i have try to debug for that i use Under debug Tab click on attach to process select Myservice.exe it wont go through the break point.

In service on start event i was write following code

     protected override void OnStart(string[] args)
    {

        Console.WriteLine("Press Enter to terminate ...");
    }

Please help me h开发者_开发百科ow resolve this issue....


Use following method.On your code. This is by far the easiest method to set a breakpoint on service library.

Debugger.Break();

protected override void OnStart(string[] args)
    {
        Debugger.Break();
        Console.WriteLine("Press Enter to terminate ...");
    }


You could also use something like this to prompt to attach a debugger in debug mode:

#if DEBUG    
if (!Debugger.IsAttached)
{
    Debugger.Launch();
}    
#endif

You could even use this code in your main method, it will run the service as a normal application in debug mode:

public static void Main()
{
    var service = new YourService();
#if DEBUG
    service.Start();
    Console.ReadLine();
    service.Stop();
#else
    var ServicesToRun = new System.ServiceProcess.ServiceBase[] { service };
    System.ServiceProcess.ServiceBase.Run(ServicesToRun);
#endif
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜