Skip to content

Instantly share code, notes, and snippets.

@stephen-james
Created March 15, 2013 12:35
Show Gist options
  • Save stephen-james/5169593 to your computer and use it in GitHub Desktop.
Save stephen-james/5169593 to your computer and use it in GitHub Desktop.
Service that can run in command line
/// <summary>
/// The main entry point for the application.
/// </summary>
static void Main(string[] args)
{
if (System.Environment.UserInteractive)
{
if (args.Length > 0)
{
switch (args[0])
{
case "-install":
{
ManagedInstallerClass.InstallHelper(new string[] {
Assembly.GetExecutingAssembly().Location
});
break;
}
case "-uninstall":
{
ManagedInstallerClass.InstallHelper(new string[] { "/u",
Assembly.GetExecutingAssembly().Location
});
break;
}
}
}
else
{
// just run the action as a once off if this was invoked User Interactive
// (ServiceAction is just a class that implements whatever the service should do to abstract the
// actual action from the guts of the service lifecycle. ie the service plumbing code)
(new ServiceAction()).PerformAction();
}
}
else
{
var servicesToRun = new ServiceBase[] { new WhateverService() };
ServiceBase.Run(servicesToRun);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment