Skip to content

Instantly share code, notes, and snippets.

@jhlee8804
Last active June 3, 2023 01:16
Show Gist options
  • Save jhlee8804/c67b95d1c9d413018c385a98d6cae7be to your computer and use it in GitHub Desktop.
Save jhlee8804/c67b95d1c9d413018c385a98d6cae7be to your computer and use it in GitHub Desktop.
static class Program
{
static void Main()
{
var servicesToRun = new ServiceBase[] { new YourService() };
// Run as service
if (!Environment.UserInteractive)
{
ServiceBase.Run(servicesToRun);
}
// Run as console
else
{
Type type = typeof(ServiceBase);
var onStartMethod = type.GetMethod("OnStart", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var service in servicesToRun)
{
onStartMethod.Invoke(service, new object[] { null });
}
Console.ReadLine();
var onStopMethod = type.GetMethod("OnStop", BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var service in servicesToRun)
{
onStartMethod.Invoke(service, new object[] { null });
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment