Instance Management in WCF Part # 1

No.of Views3928
Bookmarked0 times
By  Dhananjay Kumar   On  30 Jul 2010 11:07:30
Tag : WCF , General
This is first part of multi series articles. This article is giving introduction of Instance Management. This article will explain about Per-Call Instance management technique as well.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at



Instance Management is a technique to decide, Which SERVICE INSTANCE handles which CLIENT REQUEST and when? This is a service side implementation detail. This is useful to decide scalability, performance, durability, transactions and queued calls. 

Image Loading

There are three instance management techniques in WCF. 

Image Loading
Image Loading


Instance management is a service side activation implementation detail that should not get manifested to the client. WCF achieve this by BEHAVIOR.

  • A Behavior is a local attribute of service that does not affect its communication patterns.
  • Client does not aware of the Behavior.
  • Service does not manifest behavior in binding or metadata.
  • Practically Behaviors are WCF classes.
  • These are used at the runtime operations. 
Image Loading


  • This applies directly to service implementation class.
  • This affects all the endpoints.
  • This is used to configure service instance mode.


  • This is used to configure operation behavior.
  • This affects only implementation of a particular operation.
  • This could apply only to method that implements contract operation.
  • This is not applied to the contract definition itself.

Explanation of Service Behavior attribute

  • ServiceBehaviorAttribute class is used to configure the instance context mode.
  • As we can see in highlighted text below that InstanceContextMode is a public property.
  • Type of InstanceContextMode property is enum InstanceContextMode.
  • It is inside namespace System.ServiceModel.
Image Loading

Explanation of Instance Context Mode

  1. This is a enum type
  2. This type is defined under namespace System.ServiceModel. 
Image Loading


Image Loading

Per-Call Services

Every Client request gets a new dedicated service instance. A service instance exists only when a client call is in progress.


Image Loading
Image Loading

Steps of Per-Call Service activation

Image Loading

After step 6, WCF calls the Dispose() method, if the service is implementing IDisposable interface. Dispose method is being called on the same thread, which dispatches the original method call. Once Dispose() called , WCF disconnects service instance from rest of the WCF architecture and makes service instance a candidate for Garbage collection.

Example of Per-Call Service

Creating service

  1. Create a WCF service application.
  2. In Service Contract, create an Operation Contract. This method (Operation Contract) will return an Integer.
  3. Image Loading
  4. Create a Service class.

a. This class will have Service Behavior Attribute.
b. Service Behavior Attribute is configured for Per- Call Instance management.
c. InstanceContextMode property of Service Behavior class takes Per-Call value for enum type InstanceContextMode Type. ( See highlighted text in below code) 

Image Loading

Explanation of code

  1. I have taken one static variable because its value will persist across all service instances.
  2. In constructor of service class, I am incrementing value of this variable by one.
  3. In Operation contract, I am returning value of variable to client.
  4. On Dispose of service instance, I am again decrementing value of variable by one.

Creating client

  1. Add a Console application in same project as client.
  2. Add service reference of service. 
Image Loading


Image Loading

Explanation of output

From client, service method is being called twice. But since in Per-call Service activation, separate service instance is being created for separate method call and after returning from method dispose is being called on the same thread. So value of static variable is remaining same.

Let us take a try. Just go ahead and comment ServiceBeahavior attribute from service class

So, now service class will look like 

Image Loading

Just recompile the service and update the service reference. And run the console application. (At least 5 times) see the output. You will find each time; you are running you are getting increased value. But if service is configured for Per-Call instance management, each time you are getting the same output that is 1.


I have given introduction of Instance management is WCF. I also explained One Instance management technique.

Future Scope

In next articles of this series, I will explain about Per-Session Instance management technique and Single Instance management technique. Till then, Happy Coding.

Sign Up to vote for this article
About Author
Dhananjay Kumar
Occupation-Software Engineer
Company-Infosys Technolgies,Pune
Member Type-Gold
Joined date-20 Jul 2009
Home Page-
Blog Page-
Dhananjay Kumar is Microsoft MVP on connected system. He blogs at . You can follow him and reach him at
Other popularSectionarticles
There is no comments for this articles.
Leave a Reply
Display Name:
(not display in page for the security purphase)
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top