将 C# 服务包装在控制台应用程序中以对其进行调试 [英] Wrapping a C# service in a console app to debug it

查看:9
本文介绍了将 C# 服务包装在控制台应用程序中以对其进行调试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想调试一个用 C# 编写的服务,而老式的方法太长了.我必须停止服务,启动在调试模式下使用该服务的应用程序(Visual Studio 2008),启动该服务,附加到服务进程,然后在我的 Asp.Net 应用程序中导航以触发该服务.

I want to debug a service written in C# and the old fashioned way is just too long. I have to stop the service, start my application that uses the service in debug mode (Visual studio 2008), start the service, attach to the service process and then navigate in my Asp.Net application to trigger the service.

我基本上是在后台运行服务,等待任务.Web 应用程序将触发要由服务获取的任务.

I basically have the service running in the background, waiting for a task. The web application will trigger a task to be picked up by the service.

我想做的是有一个控制台应用程序来触发服务以便我进行调试.有没有人知道的简单演示?

What I would like to do is to have a console application that fires the service in an effort for me to debug. Is there any simple demo that anybody knows about?

推荐答案

你可以在主入口点做这样的事情:

You can do something like this in the main entry point:

static void Main()
{
#if DEBUG
    Service1 s = new Service1();
    s.Init(); // Init() is pretty much any code you would have in OnStart().
#else
    ServiceBase[] ServicesToRun;
    ServicesToRun=new ServiceBase[] 
    { 
        new Service1() 
    };
    ServiceBase.Run(ServicesToRun);
#endif
}

并在您的 OnStart 事件处理程序中:

and in your OnStart Event handler:

protected override void OnStart(string[] args)
{
    Init();
}

这篇关于将 C# 服务包装在控制台应用程序中以对其进行调试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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