Skip to content

Instantly share code, notes, and snippets.

@morbidcamel101
Last active August 29, 2015 14:10
Show Gist options
  • Save morbidcamel101/a8080b33692de1dbe521 to your computer and use it in GitHub Desktop.
Save morbidcamel101/a8080b33692de1dbe521 to your computer and use it in GitHub Desktop.
Interchangeable Service / Console app boiler plate code.
/// <summary>
/// Main entry point of the service.
/// </summary>
/// <param name="args">Arguments specified for the service.</param>
static void Main(string[] args)
{
if (args.Length > 0)
{
if (args[0].ToLower() == "/install")
{
ServiceInstallUtil si = new ServiceInstallUtil();
si.InstallService(
Assembly.GetExecutingAssembly().Location,
"MyService",
"My Service Pretty Name");
}
else if (args[0].ToLower() == "/uninstall")
{
ServiceInstallUtil si = new ServiceInstallUtil();
si.UnInstallService("MyService");
}
else if (args[0].ToLower() == "/appmode")
{
Console.WriteLine("Starting My Service Ver. 1.0 in application mode. Press Ctrl+C to stop.");
Console.WriteLine(" o Startup Parameter: {0}", Param);
Console.WriteLine("Listening...");
Console.WriteLine("");
// TODO - Run in "Console" mode
var logic = new MyService(); // Inherits from System.ServiceProcess.ServiceBase ()
logic.Run();
}
else if ((args[0].ToLower() == "/help") || (args[0].ToLower() == "/?"))
{
WriteHelp();
}
}
else
{
// Service startup
WriteHelp();
Console.WriteLine("Cannot start a windows service from the command line. Use 'net start MyService'");
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new MyService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment