Skip to main content
Email Password   helpLost your password?

Test service in Service Manager

Introduction

The Windows service code that ships with the .NET framework and Visual Studio works just fine usually. However, sometimes it's just annoying to have to create an installer project just for a simple service you're writing. Furthermore, Microsoft tends to hide away the Win32 service infrastructure from us. I'm not saying that's a bad thing, but sometimes you just need something a little better. There're some advanced issues with services that make working with the API difficult and so wouldn't it be nice to have something that encapsulates all the functionality, but exposes it to those who need it? The code provides a base class and an attribute for you to use to define your own service.

Background

A thorough discussion of the intricacies of Win32 services is another subject. This code focuses on bringing it all together and then exposing it via an easy to use class and custom attribute.

Using the code

Using the code is as simple as deriving from the base class:

using System;
using HoytSoft.ServiceProcess.Attributes;

namespace MyServices {
    public class TestService : HoytSoft.ServiceProcess.ServiceBase {

        protected override bool Initialize(string[] Arguments) {
            this.Log("Test service initialized correctly, starting up...");
            return true;
        }
    }
}

And then defining an attribute:

using System;
using HoytSoft.ServiceProcess.Attributes;

namespace MyServices {
    [Service(
    "HoytSoft_TestService", 
    DisplayName         = "HoytSoft Test Service", 
    Description         = "Isn't this just absolutely amazing?",
    ServiceType         = ServiceType.Default,
    ServiceAccessType   = ServiceAccessType.AllAccess,
    ServiceStartType    = ServiceStartType.AutoStart,
    ServiceErrorControl = ServiceErrorControl.Normal,
    ServiceControls     = ServiceControls.Default
    )]
    public class TestService : HoytSoft.ServiceProcess.ServiceBase {

        protected override bool Initialize(string[] Arguments) {
            this.Log("Test service initialized correctly, starting up...");
            return true;
        }
    }
}

Test service in Service Manager

All the attributes correspond to their equivalent Win32 service descriptions. A lot of the code has been commented, so a glance at the intellisense should give you an idea of what each option is and how it modifies your Windows service.

It is important that you give a name to your service. It is a required parameter for the Service attribute. The attribute values specified here are used in the ServiceBase's Main() method. I suppose I should also note that your service should not define its own Main() method since ServiceBase uses its own that reads in Service attributes and then creates objects automatically for you. If the service has not been installed, it will auto-install it for you. To uninstall a service, simply pass in "u" or "uninstall" to the program. To manually do this, go to a DOS prompt and type in: sc delete "MyServiceName".

An interesting feature that hasn't been tested at all is the ability to use multiple services in a single application. To do this, be sure to set the ServiceType to ServiceType.ShareProcess or ServiceType.InteractiveProcessShare.

Now of most importance to you is probably developing, testing, and debugging your service. There is a property on the Service attribute named "Debug". Set this to true and the base class will automatically treat your program like a normal console program so you can develop the rest of the program. When you're ready to test it out as an actual service running on the machine, just switch this to false or take it out and it will work like a service. The program will install the service whether or not you're in debug mode when it's first run. If you try to run the program when it was compiled in Debug mode, you will get an error saying the process couldn't be started. This is by design since to gain all of the debug mode capabilities in Visual Studio, you can't have it start up as a real service. Simply switch the Debug mode, recompile, and it will run as normal.

You can write to the system event log by making a call to this.Log("My message here."):

The main meat of your code should be the overridden methods:

All of these should be fairly straightforward. Any questions?

Updates

Points of Interest

History

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionRunning a Process from within the service Pin
Krischu
7:22 9 Nov '09  
GeneralHow can this example be neutralized or renamed to another service than hoytsoft...? Pin
Krischu
9:36 7 Nov '09  
GeneralRequestAdditionalTime Pin
ventcorp
6:30 11 Aug '09  
GeneralRe: RequestAdditionalTime Pin
David Hoyt
18:07 12 Aug '09  
GeneralRe: RequestAdditionalTime Pin
David Hoyt
7:03 13 Aug '09  
GeneralRe: RequestAdditionalTime Pin
David Hoyt
9:39 13 Aug '09  
GeneralWebsite dead where the latest release is. Pin
Burthold
17:49 4 Mar '09  
AnswerRe: Website dead where the latest release is. Pin
David Hoyt
19:26 4 Mar '09  
GeneralRe: Website dead where the latest release is. Pin
David Hoyt
19:30 4 Mar '09  
GeneralNEW RELEASE! [modified] Pin
David Hoyt
17:50 11 Jul '08  
NewsRe: NEW RELEASE! Pin
David Hoyt
22:22 12 Jul '08  
NewsServiceProcess invoked from another assembly Pin
pijy
22:47 29 May '08  
GeneralRe: ServiceProcess invoked from another assembly Pin
David Hoyt
17:55 11 Jul '08  
AnswerRe: ServiceProcess invoked from another assembly Pin
DavidHoyt
11:55 13 Jul '08  
GeneralOnCustomCommand Pin
kim.david.hauser
23:07 16 Aug '07  
AnswerRe: OnCustomCommand Pin
kim.david.hauser
0:03 17 Aug '07  
GeneralRe: OnCustomCommand Pin
David Hoyt
17:56 11 Jul '08  
QuestionProblem when running server with non System user account Pin
Tom Weilenmann
5:59 7 Aug '07  
AnswerRe: Problem when running server with non System user account Pin
David Hoyt
17:59 11 Jul '08  
QuestionPossible to set CanStop attribute? Pin
MangeA
10:48 4 Jun '07  
AnswerRe: Possible to set CanStop attribute? Pin
David Hoyt
10:01 5 Jun '07  
AnswerRe: Possible to set CanStop attribute? Pin
David Hoyt
18:01 11 Jul '08  
GeneralVista Compatible Pin
agates29
9:28 2 Apr '07  
GeneralRe: Vista Compatible Pin
David Hoyt
18:02 11 Jul '08  
GeneralOnCustomCommand ? Pin
bacgeek
6:24 20 Oct '06  


Last Updated 28 Oct 2005 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009