Create a combo command line / Windows service app
Asked Answered
C

4

14

What's the best way in C# to set up a utility app that can be run from the command line and produce some output (or write to a file), but that could be run as a Windows service as well to do its job in the background (e.g. monitoring a directory, or whatever).

I would like to write the code once and be able to either call it interactively from PowerShell or some other CLI, but at the same time also find a way to install the same EXE file as a Windows service and have it run unattended.

Can I do this? And if so: how can I do this?

Corrida answered 30/4, 2009 at 21:6 Comment(0)
R
21

Yes you can.

One way to do it would be to use a command line param, say "/console", to tell the console version apart from the run as a service version:

  • create a Windows Console App and then
  • in the Program.cs, more precisely in the Main function you can test for the presence of the "/console" param
  • if the "/console" is there, start the program normally
  • if the param is not there, invoke your Service class from a ServiceBase


// Class that represents the Service version of your app
public class serviceSample : ServiceBase
{
    protected override void OnStart(string[] args)
    {
        // Run the service version here 
        //  NOTE: If you're task is long running as is with most 
        //  services you should be invoking it on Worker Thread 
        //  !!! don't take too long in this function !!!
        base.OnStart(args);
    }
    protected override void OnStop()
    {
        // stop service code goes here
        base.OnStop();
    }
}

...

Then in Program.cs:


static class Program
{
    // The main entry point for the application.
    static void Main(string[] args)
    {
        ServiceBase[] ServicesToRun;

    if ((args.Length > 0) && (args[0] == "/console"))
    {
        // Run the console version here
    }
    else
    {
        ServicesToRun = new ServiceBase[] { new serviceSample () };
        ServiceBase.Run(ServicesToRun);
    }
}

}

Raynell answered 30/4, 2009 at 21:13 Comment(1)
Instead of args check, you can check Environment.UserInteractive to see if we are in interactive mode or not.Creeper
J
4

The best way to accomplish this from a design standpoint is to implement all your functionality in a library project and build separate wrapper projects around it to execute the way you want (ie a windows service, a command line program, an asp.net web service, a wcf service etc.)

Jemine answered 30/4, 2009 at 21:28 Comment(0)
I
3

Yes it can be done.

Your startup class must extend ServiceBase.

You could use your static void Main(string[] args) startup method to parse a command line switch to run in console mode.

Something like:

static void Main(string[] args)
{
   if ( args == "blah") 
   {
      MyService();
   } 
   else 
   {
      System.ServiceProcess.ServiceBase[] ServicesToRun;
      ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
      System.ServiceProcess.ServiceBase.Run(ServicesToRun);
   }
Incredulity answered 30/4, 2009 at 21:16 Comment(0)
H
1

A Windows Service is quite different from a normal Windows program; you're better off not trying to do two things at once.

Have you considered making it a scheduled task instead?

windows service vs scheduled task

Harbard answered 30/4, 2009 at 21:15 Comment(2)
I've been thinking about that, too, yes - but then I have a web app, a bunch of SQL jobs, a bunch of Windows service, a bunch of command line tools, and now a bunch of those being used as scheduled tasks.... I'm actually trying to REDUCE the number of different types of things I need to take care of and manage. Thanks for the input anyway!Corrida
I have successfully created applications that can be run from a command line and also installed and run as windows services but I agree with @Mark Ransom in at least the fact that they are very different beasts and that you have to be careful with the implementation - especially with the Service. Like I mentioned in the code comments in my example, don't run any blocking tasks from the OnStart event handler. Rather, start your service on a separate thread or some similar asynchronous construct!Raynell

© 2022 - 2024 — McMap. All rights reserved.