Creating a Windows Service with C#, continued - Creating the Windows Service Project
(Page 2 of 4 )
Add a new project to the TimeApp solution called “TimeService” and rename the “Service1” class “TimeServerService.” Be sure to use the find and replace tool to rename “Service1” to TimeServerService in the class file itself, because it appears in several places: the class name, the constructor, and the ServicesToRun array in the Main() method. Next you need to change a few properties of the TimeServerService.cs in design view. Change the ServiceName property to “Time Server Service” and change “CanShutdown” to “True.”
Once you have changed those properties, click the “Add Installer” link. This will add a file named “ProjectInstaller.cs” to your project. In the “ProjectInstaller.cs” design view, you need to set the “StartType” property to “Manual” for the ServiceInstaller and set the “Account” property to “LocalSystem” for the ServiceProcessInstaller. Save and close the project installer files; they are finished.
Now that the project is set up, we need to open up the TimeServer.cs file in the TimeApp project and get ready to move it over to the TimeServerService.cs class.
First we need to add the System.Net, System.Net.Sockets, and System.Text namespaces to the TimeServerService class. Then copy over the listener field and the readonly fields ip and port from TimeServer. Copy the entire Start() method from TimeServer to TimeServerService next. Add a call to listener.Start() in the OnStart() method, which is called implicitly when a Windows Service is started, and in the TimeServerService constructor paste the line that instantiates the TcpListener from TimeServer.Main().
Your service class should look like this when you’re done (note that I have removed the massive comment blocks that come with the Windows Service class):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.ServiceProcess;
using System.Net;
using System.Net.Sockets;
using System.Text;
namespace TimeService
{
public class TimeServerService :
System.ServiceProcess.ServiceBase
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private readonly int port = 48888;
private readonly IPAddress ip = IPAddress.Parse
("127.0.0.1");
private TcpListener listener;
public TimeServerService()
{
InitializeComponent();
this.listener = new TcpListener(port, ip);
}
// The main entry point for the process
static void Main()
{
System.ServiceProcess.ServiceBase[] ServicesToRun;
ServicesToRun = new System.ServiceProcess.ServiceBase[]
{ new TimeServerService() };
System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//
// TimeServerService
//
this.CanShutdown = true;
this.ServiceName = "Time Server Service";
}
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
protected override void OnStart(string[] args)
{
this.Start();
}
protected override void OnStop()
{
}
public void Start()
{
this.listener.Start();
Socket s;
Byte[] incomingBuffer;
Byte[] time;
int bytesRead;
while (true)
{
s = this.listener.AcceptSocket();
incomingBuffer = new Byte[100];
bytesRead = s.Receive(incomingBuffer);
time = Encoding.ASCII.GetBytes(
System.DateTime.Now.ToString().ToCharArray());
s.Send(time);
}
}
}
}
Look at the Main() method. You will notice that it’s quite different from the Main() method of a console or form application. With a Windows Service, the Main() method creates an array of services to run and runs them. Of course, “running” simply means creating an instance of the class and calling the OnStart() method.
Next: Coding the Service >>
More C# Articles
More By David Fells