Wednesday, February 24, 2010

Creating a C# Windows Service

Creating a Windows Service can be simple if you have all the right information. What I will show you today will teach you how to create a Windows Service and run that service on Windows XP.

Let's begin:
Launch Visual Studio





Step 1: Create a Skeleton Project

File > New > Project

A new project dialog box will appear giving you options for creating a new project.

Let's narrow down the project type.

From the tree view on the left side of the dialog box select Windows.

This will only display Templates for creating Windows projects. Select Windows Service





Step 2: Project Name & Settings

Give your windows project a name, then click OK to create your project.


Your project Window Window will look like this:



Set your ServiceName to your own name so that you will recognize it when you look at services in Computer Managerment under Services tree. I used LMCListener as my service name. It might be difficult to find the properties to do that. Here is what you do. When the project wizard is completely launched, the first window that appears is the Design View for Service1.cs. Your properies will most likely not display the Service Processes so, simply move your mouse to the Service1.cs tab at the top of your project winds, then in the Properties you will then see ServiceName at the bottom of the list.











Step 3: Adding code to the code view, right click on the gray area of the Service1.cs window and choose View Code

That will give you the following:







I've provided you with some sample code to help you create your first project. Here's the code below:


protected override void OnStart(string[] args)
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Started \n");
m_streamWriter.Flush();
m_streamWriter.Close();
}

protected override void OnStop()
{
FileStream fs = new FileStream(@"c:\temp\mcWindowsService.txt",
FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.BaseStream.Seek(0, SeekOrigin.End);
m_streamWriter.WriteLine(" mcWindowsService: Service Stopped \n"); m_streamWriter.Flush();
m_streamWriter.Close();
}


Now Build your application which will produce an exe file located in the debug > bin folder of your project.



Step 4: The next step would be to Install and Run the Service.


You will do that using an install utility provided as part of the .NET Framework. But before we can install the service let's add some additional features to our service.

I'm not saying that our service is nonfunctional. It will work as is, but what if we want to have the service description and other things show up in our processes panel, or Services Panel. Here's the trick.



If you are not on the Service1.cs Design window, then go there. Now right click on the gray area of the window and choose Add Installer. The Installer objects will be added to your Design window at the top left corner.

There will be two files in that location: ServiceProcessInstaller1 and ServiceInstaller1, both will need their properties configured.

ServiceProcessInstaller1 - Account, this is the user account that the service will run under, set this to LocalSystem.


ServiceInstaller1 - Description, this is the description that will appear in the services admin tool. Set this to Empty Service Description

ServiceInstaller1 - Display Name, this is the service name that will appear in the services admin tool. Set this to Empty Service

ServiceInstaller1 - ServiceName, Set this to EmptyService.

ServiceInstaller1 - StartUpType, this determines whether the service will start automatically with windows, set this to Manual



Alright, thats it, you cannot run a service as you would a windows program. The .net framework has provided a tool that will install the service for you. The InstallUtil.exe program is located in the framework folder. From the command prompt type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe C:\YourServiceName .exe

where C:\YourServiceName .exe is the path to you service, to uninstall the service type:

%Windir%\Microsoft.NET\Framework\v2.0.50727\InstallUtil.exe /u C:\YourServiceName .exe

Once your service is installed you will find it in the Services admin tool by typing Services.msc at the run prompt:

Note make sure C:\YourServiceName contains the entire path to the exe file your created.

No comments: