Brendan Tompkins [MVP]

Sponsors

The Lounge

News

Advertisement

Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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


Posted 06-14-2004 8:54 AM by Brendan Tompkins

[Advertisement]

Comments

Darrell wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-14-2004 8:19 AM
Yeah, I had to write a Windows Service, and it was *so* much easier than in old VB6. The nice part is that it allows you to focus on the problem you are trying to solve, rather than focusing on how to implement a service.

Now if only they could make it this easy for COM!
Albert wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-14-2004 11:53 AM
Thanks a lot Brendan, these articles are very helpful
Fred wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-14-2004 12:38 PM
That's fine but i cannot find the reference to use VIT.Common
Brendan Tompkins wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-14-2004 1:34 PM
Ooops! That should have been your machine name...

Fixed the code.

-Brendan
Micha wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-16-2004 11:34 PM
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.
StuartGunter wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-28-2004 6:52 PM
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.
StuartGunter wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-28-2004 9:03 PM
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?
Brendan Tompkins wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 06-29-2004 2:11 AM
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.
Jørn Schou-Rode wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 05-21-2006 11:01 AM
Great article. Thanks!
mehmet wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 11-30-2006 11:31 AM

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

Steve MunLeeuw wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 05-25-2007 10:53 AM

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

Andrew Cotterell wrote re: Windows Service Administration with .NET Part 3 - Controlling Your Service
on 09-13-2007 5:20 PM

Great Article!

Add a Comment

(required)  
(optional)
(required)  
Remember Me?