如何从visual studio运行(F5)windows服务 [英] how to run(F5) windows service from visual studio

查看:46
本文介绍了如何从visual studio运行(F5)windows服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从 Visual Studio 运行 Windows 服务项目.

How to run a windows service project from visual studio.

我正在 Visual Studio 2008 中构建 Windows 服务,我必须始终从控制面板运行服务,然后将调试器附加到正在运行的服务实例.这有点烦人,因为我正在清理大量代码,并且在开发过程中需要多次重启我的服务.

I am building a windows serivce in visual studio 2008, I have to always run the service from control panel and then attach the debugger to running instance of the service. Its kind of annoying since I am cleaning a lot of code and need to restart my service many times during development.

我想设置我的项目,以便能够按 F5 并运行服务并直接进入调试模式.关于如何实现这一目标的一些提示会很棒.

I want to setup my project so as to be able to hit F5 and run the service and directly enter the debug mode. Some tips on how to achieve this would be great.

提前致谢!!!

推荐答案

复制自 这里.

static void Main(string[] args)  
{  
    DemoService service = new DemoService();  

    if (Environment.UserInteractive)  
    {  
        service.OnStart(args);  
        Console.WriteLine("Press any key to stop program");  
        Console.Read();  
        service.OnStop();  
    }  
    else 
    {  
        ServiceBase.Run(service);  
    }  
}  

这应该允许您从 Visual Studio 中运行.

This should allow you to run from within Visual Studio.

另一种方法是通过调用 System.Diagnostics.Debugger.Break() 在您的代码中嵌入一个编程断点.当你把它放在你的服务的 OnStart() 回调中并从服务控制台启动你的服务时,编程断点将触发一个对话框,允许你附加到 Visual Studio 的现有实例或启动一个新的实例.这实际上是我用来调试服务的机制.

Another way would be to embed a programmatic breakpoint in your code by calling System.Diagnostics.Debugger.Break(). When you place this in, say, the OnStart() callback of your service and start your service from the Services console, the programmatic breakpoint will trigger a dialog box that allows you to attach to an existing instance of Visual Studio or to start a new instance. This is actually the mechanism I use to debug my service.

这篇关于如何从visual studio运行(F5)windows服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆