Click here to Skip to main content
15,881,967 members
Articles / Web Development / IIS

Using IIS for Creating Scalable Windows Services

Rate me:
Please Sign up or sign in to vote.
3.12/5 (7 votes)
25 Oct 2009CPOL3 min read 36.3K   15   9
This article discusses a mechanism for creating scalable Windows services using IIS.

Introduction

Sometime back we had to develop a rather scalable enterprise level Windows service. Creating a simple service is a cakewalk, deploying it for scalability is not. This article walks through how we combined Windows service and IIS together for a really good scalable deployment.

IIS gives us a large number of features which allow scalability, for example:

  1. Usage of application pools and their recycling
  2. Out of box scale out and load balancing
  3. Features like when the IIS worker process is using too much memory, a new worker process starts up leading to better performance

I could go on, but the point is that IIS has better scalability features than Windows services, simple things like multithreading and partitioning of execution space is provided by default. With little effort, we can develop solutions which work harmoniously across a server farm.

The trouble is that an IIS feature works when requested using an HTTP post. If we combine the best from both these worlds, we get a really great solution. That is what we try to do in this article.

For learning about how IIS provides scalability, please go to this link.

Windows services essentially execute some business logic over and over again after every time interval. The solution keeps this recurring logic in the Windows service while the business logic should be in a web or WCF service. Now this service can be called from the Windows service to do the job.

The Architecture of the Solution

Arch.png

Windows Server executing the service - This is a very simple Windows service whose sole purpose is to call a web service or WCF service at periodic intervals.

Load balancer - This may be a hardware or software based load balancer whose job is to distribute the request.

Servers executing business logic - These are servers which expose the service as a web / WCF service. The code is written in these to ensure scalability, some examples can be if we want to process records in a database or files on a server then this business logic will lock on a few records/files so that other competing servers may participate in the task. These servers should also be designed in such a manner that if the previous request is not completed, then new requests should not interfere with the previous request.

Some Sample Code

This is a very simple example in which every minute, we request the latest document dropped in the folder to be processed.

C#
Timer timer = null; 
protected override void OnStart(string[] args) 
{ 
    timer = new Timer(60 * 1000); 
    timer.Elapsed += new ElapsedEventHandler(timer_Elapsed); 
    timer.Enabled = true; 
} 

The OnStart event handler for the service is kicked in when the service starts. All we are doing is setting up our timer here.

When the timer is elapsed, we want to call the web service as in this code:

C#
void timer_Elapsed(object sender, ElapsedEventArgs e) 
{ 
    ServiceReferenceProxy.ServiceSoapClient webServiceProxy = 
			new ServiceReferenceProxy.ServiceSoapClient(); 
    webServiceProxy.DoSomeTaskCompleted += 
	new EventHandler<AsyncCompletedEventArgs>(webServiceProxy_DoSomeTaskCompleted); 
    webServiceProxy.DoSomeTaskAsync(); 
} 

It should be noted that to do this, we have used async operation on the web service method. This has been done so that if the task is not completed and the timer elapses again we get better scalability.

It should also be noted that we are not doing anything specific to get scalability on the Windows service end. The network load balancer will do it for us.

When the task of the web service is completed, we may choose to log or do some task this is done by the Completed event handler.

C#
void webServiceProxy_DoSomeTaskCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    //Log Something 
} 

Setting Up Scalability Options

This section points out some reference material out on the net for setting up scalability:

History

  • 25th October, 2009: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect Imfinity
India India
Hi I have been working on enterprise applications for last six years.

Comments and Discussions

 
GeneralMy vote of 2 Pin
JeremyRemington29-Oct-09 18:55
JeremyRemington29-Oct-09 18:55 
GeneralMy vote of 1 Pin
António Barroso25-Oct-09 11:50
António Barroso25-Oct-09 11:50 
GeneralRe: My vote of 1 Pin
gaurav_verma_mca25-Oct-09 17:16
gaurav_verma_mca25-Oct-09 17:16 
GeneralMy vote of 1 Pin
Ruchit S.25-Oct-09 6:53
Ruchit S.25-Oct-09 6:53 
GeneralRe: My vote of 1 Pin
gaurav_verma_mca25-Oct-09 7:27
gaurav_verma_mca25-Oct-09 7:27 
GeneralServers executing business logic. Pin
Paulo Zemek25-Oct-09 3:40
mvaPaulo Zemek25-Oct-09 3:40 
GeneralRe: Servers executing business logic. Pin
gaurav_verma_mca25-Oct-09 3:49
gaurav_verma_mca25-Oct-09 3:49 
GeneralRe: Servers executing business logic. Pin
Paulo Zemek26-Oct-09 4:26
mvaPaulo Zemek26-Oct-09 4:26 
GeneralRe: Servers executing business logic. Pin
gaurav_verma_mca26-Oct-09 5:58
gaurav_verma_mca26-Oct-09 5:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.