Archive for category Services

Creating a new Windows Service .Net

Microsoft has made it a bit painful to develop a windows service IMO.  If you have the right version of Visual Studio it does make it easier, apparently the ‘regular’ version doesn’t come with the Windows Service template, and if you use VS Express you’re out of luck.

However, regardless of Visual Studio version you ARE able to create Windows Services, it’s just with some versions you have to do the entire thing by hand while with others it generates you a template.

In case you’re one of the many that don’t have the template here is the bare minimum for a windows service:

using System.Threading;

class Program : System.ServiceProcess.ServiceBase
{
private Timer serviceTimer;
TimerCallback timerDelegate;
private int _interval = 500000;

public Program(){
{
this.ServiceName = “My name”;
}
protected override void OnStart(string[] args)
{
//Code here to repeat the call, DoWork() will be our main entry point:
timerDelegate = new TimerCallback(DoWork);
// create timer and attach our method delegate to it
serviceTimer = new Timer(timerDelegate, null, 10000, _interval);
}
}
protected override void OnStop(){
//Terminate code here…
}
private void DoWork(object state){
//Main program code here:
//after Main program run again:
timerDelegate = new TimerCallback(DoWork);
// create timer and attach our method delegate to it
serviceTimer = new Timer(timerDelegate, null, 10000, _interval);
}

Now, that’s great and all but there are still a few gotcha’s.  For example, you can’t debug / step through this code.  In order to do that you’ll need an entry point that’s different than what a windows service needs.  In my project I used this:

static void Main(string[] args)
{

Program p = new Program();
p.OnStart(args);

while (true)
{
System.Threading.Thread.Sleep(5000);
}
}

Note this code only gets called while you’re debugging, so the infinite loop isn’t really an issue, and is actually similar to how a service works (never exits unless forced to somehow).

So you’ve written your service but don’t know how to actually create the service?  Yeah, there’s no right-click->add new in the admin interface.  In order to create a new service you’ll need to run sc.exe.  This command line is a good example:

sc create “ATestService” binpath= “c:\TheFullPath\AndExeName.exe”
NOTE: the space after the = sign is required!

Here’s some more info on sc.exe:
Using SC.EXE to Develop Windows NT Services

REMEMBER: this service is defaulted to run as system.  You will probably want it to run as someone in particular (certainly someone who has the privilege necessary to run anything using the current user for authentication.  eg: You may not need to worry about databases depending on how you’ve set them up, but writing to the file system you’ll need an appropriate user for).  To configure this service to run as someone else just open the Services admin panel->r-click on the service->Properties->Log On(tab)->This account: and now choose the appropriate id/pwd.

Leave a comment