简体   繁体   中英

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.

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?

You can use TopShelf if you want, it's exactely what you're looking for.

You can write an application that is in fact a Windows Srevice, but which can run as a Console Application.

It makes debugging a lot easier

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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