AspNet4YouTop 10sDevelopers WorldForums
Home
About
ASPNet Books
ASPNet Sites
ASPNet Hosters
View Articles
Search Articles
Authors
View Forums
 
Quick Menus
HOME
About
AspNet4You Forums
Articles
Authors
Search
Articles Summary
Articles(RSSFeed)
AspNet Books
AspNet Sites
AspNet Hosters
 Top 10 ASP.NET Books 
Professional ASP.NET 1.1
Essential ASP.NET With Examples in C#
ASP.NET Unleashed
Programming Data Driven Web Applications with ASP.NET
Professional ASP.NET Web Services
Beginning ASP.NET 1.1 with Visual C# .NET 2003
Programming Microsoft ASP.NET
Beginning ASP.NET Databases Using VB.NET
ASP.NET Security
Developing Microsoft ASP.NET Server Controls and Components
More...
 Top 10 ASP.NET Hosters 
WebHost4Life
DiscountASP.NET
MaximumASP
Brinkster
ORCS Web
myhosting.com
ISQSolutions
ASPwebhosting.com, LLC
Active ISP
Aquest Hosting
More...
 Top 10 ASP.NET Sites 
Asp.Net
GotDotNet
4GuysFromRolla.com
123aspx.com
EggHeadCafe.com
CShrp.Net
.NET 247
DevelopersDex.com
Csharp-Corner.com
dotnetspider
More...
Search Articles
Google
ASPNET4YOU      
Category:   Search Type:   Match Type:  
Running Two Instances of same Windows Service?
Author: Saha, ProdipPosted: 4/20/2006 9:59:08 PM

Running Two Instances of same Windows Service?

Recently I was given a project to cut down the queue processing time by 50 percent. Queue processing application is a Single Threaded Windows Service written in C#.Net to batch update data in Sybase database. Depending on the queue size the current process takes anywhere from 4 hours to 8 hours.

Visual Studio 2005: Download the complete source code ServiceFactory.zip(243KB)

Your first and quick thought would be running multiple instances of the windows service or make the windows service multi-threaded to cut down the processing time. However, it would not work since most of the processing work is performed on the database server through stored procedures (SP) and SP may lock each other while updating the same table.

So, my first objective was to reduce the processing time on the database server without running into dead lock situations. I know this would not work since the database locking is set to Page Level Lock. I will sure run into dead lock situation if I try to run the stored procedures to update same table concurrently even though updatable datasets are different. Thanks to Row Level Locking in Sybase 12.5. We did not have the row level locking in Sybase 12.0 or less. Changing the locking schema from page level to row level will allows us to run same stored procedure concurrently given SP will always work with different set of rows in the table.

My second object was to create two instances of the same service. This can be achieved easily. However, we have to make sure both service don�t try to pickup the same queue at the same time.

To accomplish both the objectives, I have come up with the following design requirements-

  • Change the database locking schema to row locking specially for the tables we want to update. 
  • Modify the Windows Service to run two instances of the same service.
  • Create a Singleton object with locking to pick up the queue entry.

In this article I will not spend time on explaining how to change database locking schema. I assume you get the change done through your DBA group. So, let�s focus to how to create two instances of the same service.

If you are not familiar with creating windows service project or deployment project I would suggest you to check out the Distributed Data Access (.Net Remoting) at http://www.aspnet4you.com/Articles.aspx?ArticleID=5029.

Creating two instance of a service is almost same as creating single instance of the service with few exceptions. You can find the complete source code in download but I will highlight the following points-

Service Entry point: Initialize the ServiceBase with two instances of the same service but each with different name. The service name must match with the name specified in the ServiceInstaller.

// The main entry point for the process
static void Main()
{
	System.ServiceProcess.ServiceBase[] ServicesToRun;

	// More than one user Service may run within the same process. To add
	// another service to this process, change the following line to
	// create a second service object. For example,
	//
	//   ServicesToRun = new System.ServiceProcess.ServiceBase[] {new Service1(), new MySecondUserService()};
	//
	
	//service name must match with the serviceInstaller
	ServiceFactory sf1=new ServiceFactory();
	sf1.ServiceName="ServiceFactory1"; 
	ServiceFactory sf2=new ServiceFactory();
	sf2.ServiceName="ServiceFactory2";

	ServicesToRun = new System.ServiceProcess.ServiceBase[] { sf1,sf2 };

	System.ServiceProcess.ServiceBase.Run(ServicesToRun);
}
					

ServiceInstaller: To create the 2nd service you must add an additional ServiceInstaller to the Service. In this article we have created two installers-ServiceFactory1, ServiceFactory2.

Service Initialization: Each of the service should be initialized in OnStart event. This event gets fired when you start the service from Control Panel.

/// 
/// Set things in motion so your service can do its work.
/// 
protected override void OnStart(string[] args)
{
	// TODO: Add code here to start your service.
	EventLog.WriteEntry(this.ServiceName,this.ServiceName +" Started at " +DateTime.Now.ToString());
	InitializeOnStart();
}
					

TaskManager: This is a Singleton helper class used to retrieve queue entry. This class guarantees the integrity of queue retrieval process through locking mechanism.

Concurrency Demo:

  • Download the ServiceFactory.zip file and extract the contents.
  • Run the MSI package from \ServiceFactory\ServiceFactorySetup\Debug\ServiceFactorySetup.exe. This will install the services- ServiceFactory1 and ServiceFactory2.
  • Start the services from control panel. You can run only one service (if you prefer). 
  • To demonstrate the concurrent services we write the queue numbers to a log file in the same folder of the ServiceFactory.exe.

Have a question or comment? Please log them in Questions & Answers Forum .


Prodip K. Saha
The Architect of WWW.ASPNET4YOU.COM.

Terms and Conditions