Enhance Your Web Service

Hey there,

To have a recap about the main topic, head over here or if you want to recall the previous post, read it from here. This post will be a short one compared to the previous one. We will be focusing on how to improve your web service for better communication between your database and your website.

First of all, you need to familiar with the term of ConcurrencyMode. In a simple definition, the term is used to tell your web service in what way you want your web service to send its instance. For now, the WCF supports Single-Threaded or multi-Threaded as modes of operation.

A short explanation for each mode based from MSDN:

Member name Description

The service instance is multi-threaded. No synchronization guarantees are made. Because other threads can change your service object at any time, you must handle synchronization and state consistency at all times.


The service instance is single-threaded and accepts reentrant calls. The reentrant service accepts calls when you call another service; it is therefore your responsibility to leave your object state consistent before callouts and you must confirm that operation-local data is valid after callouts. Note that the service instance is unlocked only by calling another service over a WCF channel. In this case, the called service can reenter the first service via a callback. If the first service is not reentrant, the sequence of calls results in a deadlock. For details, seeConcurrencyMode.


The service instance is single-threaded and does not accept reentrant calls. If the InstanceContextMode property is Single, and additional messages arrive while the instance services a call, these messages must wait until the service is available or until the messages time out.


How to Apply ConcurrencyMode?

Don’t stress yourself with all the theories above, the practical is easier 😛 … Open your Service1.svc located in your project.

The codes that you are going to insert will be before defining the class which is:


You will mostly write attributes to define the properties of your web service. The following attribute will make your service a single threaded:

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single)]

Using the Single Threaded, the WCF will not call again into the object so long as any method is running. It will only be available when the service is at rest. You will need to make that applying a serviceBehaviour might decrease or increase your service performance.

Single Threaded Mode

For this tutorial, i will make my web service as using Multi-Thread might change my objects and also i have not many methods to be called in my service.

Another tweak I would like to point out is InstanceContextMode, which specifies number of service instances available for handling calls located in the incoming request.

There are three types which are available for now:

Member name Description

A new InstanceContext object is created prior to and recycled subsequent to each call. If the channel does not create a session this value behaves as if it were PerCall.


A new InstanceContext object is created for each session.


Only one InstanceContext object is used for all incoming calls and is not recycled subsequent to the calls. If a service object does not exist, one is created.

For my current web service, I will stick to create an instance per call. This can be done by adding another attribute next to the ConcurrencyMode :

InstanceContextMode = InstanceContextMode.PerCall


The aim of this post is to showcase how flexible the WCF can be. There are also many properties which i have not yet tested or worked on it such as tweaking the System.web or removing http module to make it lighter, but i will keep updating you for more properties.

Happy Coding and Time for Game of Thrones 😀 


Harshu Kumar Gaonjur

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s