Three ways to do WCF instance management

Table of contents

Introduction

Very often we would like to control the way WCF service objects are instantiated on a WCF server. You would want to control how long the WCF instances should be residing on the server.

The WCF framework has provided three ways by which we can control WCF instance creation. In this article, we will first try to understand those three ways of WCF service instance control with simple code samples of how to achieve them. Finally, we will compare when to use them and under what situations.

WCF service object instancing basics

In normal WCF request and response communication, the following sequence of actions takes place:

  • WCF client makes a request to a WCF service object.
  • WCF service object is instantiated.
  • WCF service instance serves the request and sends the response to the WCF client.

Following is a pictorial representation of how WCF requests and responses work.

Following are different ways by which you can create WCF instances:

  • Create a new WCF service instance on every WCF client method call.
  • Only one WCF service instance should be created for every WCF client session.
  • Only one global WCF service instance should be created for all WCF clients.

To meet the above scenarios, WCF has provided three ways by which you can control WCF service instances:

  • Per call
  • Per session
  • Single instance

Per call instance mode

When we configure a WCF service as per call, new service instances are created for every method call you make via a WCF proxy client. The image below shows this in a pictorial format:

  • The WCF client makes the first method call (method call 1).
  • A new WCF service instance is created on the server for this method call.
  • The WCF service serves the request and sends the response and the WCF instance is destroyed and given to the garbage collector for clean up.
  • Now let’s say the WCF client makes a second method call, a new instance is created, the request is served, and the WCF instance is destroyed.

In other words, for every WCF client method call, a WCF service instance is created, and destroyed once the request is served.

How to implement WCF per call instancing

In order to specify the instancing mode, we need to provide the InstanceContextMode value in the ServiceBehavior attribute as shown below. This attribute needs to specified on the Service class. In the below code snippet, we have specified intCounter as a class level variable and the class counter is incremented by one when the Increment method is called.

How to implement per session instancing

To configure service as per session, we need to configure the ServiceBehavior attribute with a PerSession value in the InstanceContextMode object.

How to implement single instance mode

In order to create a single instance of a WCF service, we need to specify InstanceContextMode as Single.

 

When should you use per call, per session, and single mode?

Per call

  • You want a stateless services.
  • Your service holds intensive resources like connection objects and huge memory objects.
  • Scalability is a prime requirement. You would like to have a scaled out architecture.
  • Your WCF functions are called in a single threaded model.

Per session

  • You want to maintain states between WCF calls.
  • You a scaled up architecture.
  • Light resource references.

Single

  • You want share global data through your WCF service.
  • Scalability is not a concern.

References

Source code

You can download the source code for this tutorial from here.

Necessary instructions

This is a very clearn and logical article posted on CodeProject By Shivprasad koirala ,8 Jun 2010.As it's very easy to understand ,so I'm not plan to translate it into Chinese.In order to Show Respect to the author,I decide to give the link to the orginal article below-click here.

Other articles written by Shivprasad on WCF

You can refer to some of his other articles written on WCF:

posted @ 2012-12-26 14:07  DebugLZQ  阅读(629)  评论(2编辑  收藏  举报