Last active
June 3, 2023 01:16
-
-
Save jhlee8804/c67b95d1c9d413018c385a98d6cae7be to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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