Sponsored By Aspose - File Format APIs for .NET

Aspose are the market leader of .NET APIs for file business formats – natively work with DOCX, XLSX, PPT, PDF, MSG, MPP, images formats and many more!

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

About Brendan Tompkins

Brendan runs CodeBetter.Com. He was twice awarded MVP for Microsoft .NET, and is a founder and the CTO of Quick180.Com More about Brendan at https://www.linkedin.com/codebetter
This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • DevWatcher

    That dosent work to the common average developer because its missing elevated credentials. Please redo!

  • Andrew Cotterell

    Great Article!

  • Steve MunLeeuw

    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:
    http://technet.microsoft.com/en-us/library/bd3abaad-c6cb-2b4e-bc02-75876253e295.aspx

  • mehmet

    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

  • http://malamute.dk/ Jørn Schou-Rode

    Great article. Thanks!

  • Brendan Tompkins

    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.

  • StuartGunter

    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?

  • StuartGunter

    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.

  • Micha

    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.

  • Brendan Tompkins

    Ooops! That should have been your machine name…

    Fixed the code.

    -Brendan

  • Fred

    That’s fine but i cannot find the reference to use VIT.Common

  • Albert

    Thanks a lot Brendan, these articles are very helpful

  • Darrell

    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!