A while back I posted the following two articles about administering a Windows service using ASP.NET:
Windows Service Administration with ASP.NET – Part 1 – Marshalling Status Information
I promised to write part 3, how to start and stop a
service over the web, and a few people have been asking me,
“What Gives?, Where’s Part 3?” It turned out to be so
simple, using the ServiceController class. So here’s part three,
two static methods you can use in your ASP.NET code that will stop and
start a service:
public static void StopService(string strServiceName)
{
System.ServiceProcess.ServiceController sc2 =
new System.ServiceProcess.ServiceController(strServiceName, [YOUR SERVER NAME] );
if (sc2.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Running))
{
sc2.Stop();
}
}
public static void StartService(string strServiceName)
{
System.ServiceProcess.ServiceController sc2 =
new System.ServiceProcess.ServiceController(strServiceName, [YOUR SERVER NAME]);
if (sc2.Status.Equals(System.ServiceProcess.ServiceControllerStatus.Stopped))
{
sc2.Start();
}
}
-Brendan