Monday, June 27, 2005

Create and manipulate a Windows service.

Write code that is executed when a Windows service is started or stopped.

 

Looking at the “Skills Being Measured”  matrix section of the 70-320 exam website. The first topic is “Write code that is executed when a Windows service is started or stopped. This is the first article in a series that is going to help me study for the exam and maybe help out someone else who is studying for the exam.

 

 

A windows service is an application that runs in its own Windows session and does not have an interface. Some are automatically started when Windows boots and some can be manually started. Some good examples of programs that might be a candidate for a Windows service include workflow engines that need to run in the background, print spoolers, anti-virus programs or event logging applications. You may already know that the ASP.NET session state service is a service and without it session state in ASP.NET will not work.

 

There are a number of ways to view what Windows services you have running on your machine:

 

Ways to get to services:

 

- Start->Control Panel->Administrative Tools->Services

  

 

 

 

- Start->Run->Cmd->NET START

 cmdwindow

 

From the command line there are a number of ways to manipulate different services; the following is a list of NET commands and what they do:

 

2        >NET START

o Lists all services in the service control database.

3        >NET START [servicename]

o Attempts to start the service an example of this is: NET START EVENTLOG

o If the service name is two words or more the must be entered in quotation marks.

4        >NET STOP [servicename]

o Stops the specified service name. Stopping a service closes any connections that that service may be using, it also stops any other services that depend on it to be running. An example of this is: NET STOP IISADMIN.

5        >NET PAUSE [servicename]

o Pauses the specified service. The service must be able to pause for this to work. An example of this is: NET PAUSE IISADMIN

6        >NET CONTINUE [servicename]

o Reactivates a Windows service that has been paused. An example of this is: NET CONTINUE IISADMIN

 

Knowing this we can now create and manipulate a Windows Service.

 

1. Open Visual Studio and create a new C# project. Select Visual C# .NET Windows Service project and name it WindowsServiceLogger.

 

 

2. You should now see the design view of your Windows service, the first thing that we need to do to make a functional Windows service is to set the ServiceName property of the Windows service. To do this right click anywhere in the design Window of your service and change the ServiceName property to WindowsServiceLogger. Also while we are setting properties lets set the CanPauseAndContinue property to True.

 

 

3. Now we must override the OnStart and OnStop methods of the System.ServiceProcess.ServiceBase base class. When you create a project using the Windows service template this is automatically done for you:

 

protected override void OnStart(string[] args)
{
}

protected override void OnStop()
{
}


3. We must also change the Main() method of the Windows service from Service1 to our Windows service name:

static void Main()
{

System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[] { new WindowserviceLogger() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);

}

4. Since we set the CanPauseAndContinue poperty to True, we should override the OnPause() and OnContinue() methods of the System.ServiceProcess.ServiceBase. To see what code we need to implement, right click System.ServiceProcess.ServiceBase in the class definition and choose Go To Definition from the menu. This takes us to the object browser.If we click on the OnPause() and OnContinue() method in the members column we see the following at the bottom of the object browser:

 

protected virtual new void OnPause (  )

    Member of System.ServiceProcess.ServiceBase

 

Summary:

 When implemented in a derived class, executes when a Pause command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service pauses. 

 

protected virtual new void OnContinue (  )

    Member of System.ServiceProcess.ServiceBase

 

Summary:

 When implemented in a derived class, System.ServiceProcess.ServiceBase.OnContinue runs when a Continue command is sent to the service by the Service Control Manager (SCM). Specifies actions to take when a service resumes normal functioning after being paused. 

 

From this we can see what the method signatures that we need to override need to be. Lets add it to our Windows service:

 protected override void OnPause()
{
}

protected override void OnContinue()
{
}

 

Even though we haven't really coded anything, we can see how a Windows service works. The AutoLog property of the Windows service is automatically set to True. This property automatically logs events to the Event Viewer that occur within our Windows service. Once our service is installed we will be able to see them by going to

 

            - Start->Control Panel->Administrative Tools->Event Viewer

 

Our entries will appear under the Application node.

 

 

 

To stay in scope of the this article. We have created a Windows service and now we need to write code that is executed when a services is started and when a service is stopped. Lets add code that writes to a file when the service is started and wehn the service is stopped

 

1: Add the following namespace using System.IO;

 

2: Add the following code to the OnStart() and OnStop() methods:

 protected override void OnStart(string[] args)
{

string path = @"c:\temp\WindowsServiceLogger.txt";

if (!File.Exists(path))
{
using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("WindowsServiceLogger service started: "
+ DateTime.Now.ToString());
}

}
else
{

using (StreamWriter sw = File.AppendText(path))
{
sw.WriteLine("WindowsServiceLogger service started: "
+ DateTime.Now.ToString());

}

}

}

protected override void OnStop()
{

string path = @"c:\temp\WindowsServiceLogger.txt";

if (!File.Exists(path))
{

using (StreamWriter sw = File.CreateText(path))
{
sw.WriteLine("WindowsServiceLogger service stopped: "
+ DateTime.Now.ToString());
}

}
else
{

using (StreamWriter sw = File.AppendText(path))
{

sw.WriteLine("WindowsServiceLogger service stopped: "
+ DateTime.Now.ToString());

}

}

}

 

3. Build the project and the code will execute once we have installed our Windows service. We have now completed the first objective.

Monday, June 27, 2005 4:46:57 AM (GMT Standard Time, UTC+00:00)  #    Comments [1]  | 
Monday, June 27, 2005 7:34:09 PM (GMT Standard Time, UTC+00:00)
Huh?
CJ
Comments are closed.

Theme design by Jelle Druyts

Pick a theme: