Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make windows service application so it can run as a standalone program as well?

I'll start with an example: Apache web server (under Windows) has a nice feature: it can be both run as a standalone application (with current users privileges), and that it can be installed and run as a windows service directly (as local system account), using same executable.

In order for application to be run as a standalone app, all it needs to do is along the lines of having static public Main() in some public class.

In order for application to be installable and runnable as service, it has to implement ServiceBase and Installer classes in certain way. But, if application like this is run as standalone app, it will show message box.

How can this Apache-like mode of operation be achieved? I believe solution is simple, but I don't really have an idea where to start.

Piece of code that follows is used to invoke service. Can it be modified to allow standalone usage?

static class Program
{
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    static void Main()
    {
        ServiceBase[] ServicesToRun;
        ServicesToRun = new ServiceBase[] 
        { 
            new Service() // defined elsewhere as Service : ServiceBase
        };
        ServiceBase.Run(ServicesToRun);
    }
}

My language of choice is C#.

Edit: Currently, I have abstracted common code into separate assembly (let's call it Library.dll), and I have two executables: Console.exe and Service.exe, which are standalone and windows service applications, respectively, and both are just means to invoking Library.dll.

My goal is to merge those two executables into one, that will still call to Library.dll.

like image 615
mr.b Avatar asked Jun 27 '10 23:06

mr.b


People also ask

What is the difference between worker service and Windows service?

Both are real services. Windows Services have existed for over 20 years. They start most often at system startup and run permanently. A Worker Service is also a real process, but is intended as a background service for a front-end application; it starts with the application and stops with the application.


2 Answers

In C#, an easy way to do it is to require a command line argument to run it as a service. If the argument isn't there, then run your form/console app. Then just have your installer include the argument in the executable path when installing the service so it looks like so:

C:\MyApp\MyApp.exe -service

It would look something like this:

static void Main(string[] args)
{
    foreach (string arg in args)
    {
        //Run as a service if our argument is there
        if (arg.ToLower() == "-service")
        {
            ServiceBase[] servicesToRun = new ServiceBase[] { new Service1() };
            ServiceBase.Run(servicesToRun);
            return;
        }
    }

    //Run the main form if the argument isn't present, like when a user opens the app from Explorer.
    Application.Run(new Form1());
}

This is just an example to give you an idea, there are probably cleaner ways to write this code.

like image 143
Joe Doyle Avatar answered Oct 05 '22 09:10

Joe Doyle


After some digging, I have finally looked under .NET hood (System.ServiceProcess.ServiceBase.Run method), only to find that it checks Environment.UserInteractive bool to make sure that executable is NOT run interactively.

Oversimplified solution that works for me:

class Program
{
    static void Main(string[] args)
    {
        if (!Environment.UserInteractive)
        {
            ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[] 
            { 
                // Service.OnStart() creates instance of MainLib() 
                // and then calls its MainLib.Start() method
                new Service()
            };
            ServiceBase.Run(ServicesToRun);
            return;
        }

        // Run in a console window
        MainLib lib = new MainLib();
        lib.Start();
        // ...
    }
}
like image 39
mr.b Avatar answered Oct 05 '22 10:10

mr.b