CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Brendan Tompkins [MVP]

Blog First. Ask Questions Later.

Windows Service Administration with .NET Part 3 - Controlling Your Service

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

and

Windows Service Administration with ASP.NET - Part 2 - Remoting to the Marshalled Object

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



Comments

Albert said:

Thanks a lot Brendan, these articles are very helpful
# June 14, 2004 11:53 AM

Fred said:

That's fine but i cannot find the reference to use VIT.Common
# June 14, 2004 12:38 PM

Brendan Tompkins said:

Ooops! That should have been your machine name...

Fixed the code.

-Brendan
# June 14, 2004 1:34 PM

Micha said:

You know what I did?
I found your page when you were at part 2. I was in search for a solution because I didn't know how to control my service. I wasn't able to make your code run but I got the idea. I checked MSDN searched for Remoting and got a very good sample which I'm using right now to control my service.
There is just one thing that makes me problems. I start the service make a communication, pause it continue it. But as I press Stop Service from my console and then Start Service I can't use the same channel again. I can do that only in case I exit my console and start it again. Strange. I need to find what the problem is.
# June 16, 2004 11:34 PM

StuartGunter said:

The only thing you need to be aware of in this situation is permissions. It's not difficult to do, but it's something to make sure you've got right.
# June 28, 2004 6:52 PM

StuartGunter said:

Having posted my previous comment, I've found myself a little stuck on this. How did you get around the permissions issue? Or did you just run the site as a local admin using impersonation?
# June 28, 2004 9:03 PM

Brendan Tompkins said:

Stuart. One, admittedly bad option to get this to work is to run as admin, but you should be able to reduce permissions and find the right set that will let you stop and start a service.
# June 29, 2004 2:11 AM

Jørn Schou-Rode said:

Great article. Thanks!
# May 21, 2006 11:01 AM

mehmet said:

hi,

but not starting windows service in asp.net on Local

for example :

ServiceController ocs=new ServiceController("ServiceName");

ocs.Start();

Error = Cannot open "ServiceName"service on computer '.'.

please help!!

metin.altuntas@gmail.com

# November 30, 2006 11:31 AM

Steve MunLeeuw said:

I ran into what I think is the same issue, I found this article helpful.

XP sp2 default is to block port required to view a remote computers services, or event logs.

at command prompt:

netsh firewall set portopening TCP 445 ENABLE

article:

technet.microsoft.com/.../bd3abaad-c6cb-2b4e-bc02-75876253e295.aspx

# May 25, 2007 10:53 AM

Andrew Cotterell said:

Great Article!

# September 13, 2007 5:20 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Brendan Tompkins

Brendan has been programming with .NET since the first public beta and is owner and operator of Port Technology Services, a consultancy company providing .NET application development services to the Maritime industry. In July, 2007, he was awarded the Microsoft MVP award for ASP.NET. He's also a proud co-founder of failed .COM startup Intrinsigo, and has had a hand in the failure of numerous other businesses. He currently runs CodeBetter.Com and Devlicio.us, and lives in Norfolk, Virgina with his wife Tiara and son Ian.

View Brendan's profile on LinkedIn

Check out Devlicio.us!

Our Sponsors

This Blog

Syndication

News

MVP
Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 License.