Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Creating Window Service with .NET VS2008

Posted by Viral Sarvaiya on December 31, 2010

Hi,

Last post of year 2010, and wish u happy new year…

today i post the basic thing of the window service.

What is a Windows Service?

Windows Service applications are long-running applications that are ideal for use in server environments. The applications do not have a user interface or produce any visual output. Any user messages are typically written to the Windows Event Log.

Windows Services are controlled through the Service Control Manager where they can be stopped, paused, and started as needed.

Create a Windows Service

The service we will create does nothing really useful other than serve as a demonstration. When the service is started we will log an entry in a database indicating that it has started. The service will create a database entry on a specified interval during the time in which it is running. The service will create a final database entry when stopping. The service will also automatically log an entry in the Windows Application Log when it is successfully started or stopped.

1. Start a new project

2. Select Windows Service from the list of available project templates

3. The designer will open in design mode

4. Take the time control to the designer(make sure it is not windows control, it has to


public System.Timers.Timer timer1;

5. set the Enabled property to False and the Interval property to 30000 milliseconds

now come to service.cs page..

there are class that extend ServiceBase class with there are constractor and 2 mehtod already created is start and stop.

6. Create table “ServiceLog” as per blow


CREATE TABLE MyServiceLog (
Id int IDENTITY (1, 1) NOT NULL,
Status varchar (100),
Created datetime NOT NULL
)

7. Create event timer’s ElapsedEventHandler timer1_Elapsed event

in the service.cs file


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading;
using System.Data.SqlClient;

namespace WindowsServiceTest
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
this.timer1.Enabled = true;
this.LogMessage("Service Started");
}

protected override void OnStop()
{
this.timer1.Enabled = false;
this.LogMessage("Service Stopped");
}

/*
* Respond to the Elapsed event of the timer control
*/

private void timer1_Elapsed(object sender,System.Timers.ElapsedEventArgs e)
{
this.LogMessage("Service Running");
}

/*
* Log specified message to database
*/

private void LogMessage(string Message)
{
SqlConnection connection = null;
SqlCommand command = null;
try
{
connection = new SqlConnection("packet size=4096;server=ServerName;persist security info=False;database=DataBaseName;uid=Username;pwd=Password;");
command = new SqlCommand("INSERT INTO MyServiceLog (Status, Created) VALUES ('" + Message + "',getdate())", connection);
connection.Open();
int numrows = command.ExecuteNonQuery();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
finally
{
command.Dispose();
connection.Dispose();
}
}
}
}

8. Now Install the Windows Service

The Windows Service should be installed by using the InstallUtil.exe provided with the .NET Framework or through a deployment project such as a Microsoft Installer (MSI) file.

You must also add an Installer to your Windows Service so that the InstallUtil, or any other installation program, knows what configuration settings to apply to your service.

1. Switch to the design view for the service

2. Right click and select Add Installer

3. Switch to the design view of the ProjectInstaller that is added

4. Set the properties of the serviceInstaller1 component

1. ServiceName = My Sample Service

2. StartType = Automatic

5. Set the properties of the serviceProcessInstaller1 component

1. Account = LocalSystem

6. Build the Solution

The file ProjectInstaller.cs automatically made.

Install service with InstallUtil.exe

1. Open Visual Studio .NET command prompt

2. Go to the debug or release folder

3. Run command InstallUtil.exe MyWindowsService.exe

4. Open services from control panel -> administrative tools

5. Find the Service and right click on it and start the service.

6. Check the database. log will inserted to database in every 3 second

Enjoy Window service.

http://msdn.microsoft.com/en-us/library/d56de412

Advertisements

One Response to “Creating Window Service with .NET VS2008”

  1. […] To create window service please click here […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: