Starting a Windows service in the console

What is the best way to start a windows service as a console?

My current idea is to pass the argument "/ exe" and execute the Windows service, and then call Application.Run ().

The reason I do this is to better debug the Windows service and simplify code profiling. The service mainly hosts remote .NET objects.

+3
source share
3 answers

This is how I do it. Give me the same .exe for the console application and service. To run as a console application, it needs the -c command-line option.

private static ManualResetEvent m_daemonUp = new ManualResetEvent(false);

[STAThread]
static void Main(string[] args)
{
    bool isConsole = false;

    if (args != null && args.Length == 1 && args[0].StartsWith("-c")) {
        isConsole = true;
        Console.WriteLine("Daemon starting");

        MyDaemon daemon = new MyDaemon();

        Thread daemonThread = new Thread(new ThreadStart(daemon.Start));
        daemonThread.Start();
        m_daemonUp.WaitOne();
    }
    else {
        System.ServiceProcess.ServiceBase[] ServicesToRun;
        ServicesToRun = new System.ServiceProcess.ServiceBase[] { new Service() };
        System.ServiceProcess.ServiceBase.Run(ServicesToRun);
    }
}
+6
source
+4

Or

C:\> MyWindowsService.exe /?
MyWindowsService.exe /console
MyWindowsService.exe -console
+2
source

Source: https://habr.com/ru/post/1706427/


All Articles