简体   繁体   English

C#Windows服务和控制台应用程序合二为一

[英]C# Windows Service and Console Application in one

We are constantly making windows services, and we have a pretty good template, with nice little command line parameters for easy installing etc. 我们不断制作Windows服务,我们有一个非常好的模板,有很好的小命令行参数,便于安装等。

Here is the thing we would like to do... Write one bit of code so that the compiled code can run as a service or as a console application, depending on the way you started it. 这是我们想要做的事情...编写一段代码,以便编译的代码可以作为服务或控制台应用程序运行,具体取决于您启动它的方式。

We found that doing: 我们发现:

static void Main(string[] args)
{
    if (System.Environment.UserInteractive)
    {
        // You double clicked the exe
        Console.Write("You double clicked me")
    }
    else // Windows started me as a service.
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] { new Service_Manager() };
        ServiceBase.Run(ServicesToRun);
    }
}

Was a grate way to do things like install the service etc. However the Console is not displayed. 是安装服务等事情的方法。但是不显示控制台。 So without writing two application and sharing a DLL, is there a simple way that or something that can be included that allows the "console" to show? 因此,如果不编写两个应用程序并共享DLL,是否有一种简单的方法可以包含哪些内容,允许“控制台”显示?

You can use TopShelf if you want, it's exactely what you're looking for. 如果你愿意,你可以使用TopShelf ,这是你正在寻找的。

You can write an application that is in fact a Windows Srevice, but which can run as a Console Application. 您可以编写一个实际上是Windows Srevice的应用程序,但该应用程序可以作为控制台应用程序运行。

It makes debugging a lot easier 它使调试变得更容易

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM