Click here to Skip to main content
15,890,579 members
Articles / WCF

3 Techniques for Instance Management in WCF

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
26 May 2014CPOL2 min read 13.2K   5   1
This post discusses 3 techniques for instance management in WCF.

Instance management basically defines the binding of service instance to a request received from a client. In case of WCF, the following three techniques for service instantiation are available:

  • PerCall
  • PerSession
  • Single

As we know that application requirements vary from one to another with respect to scalability, durability, performance, transactions, etc., so in order to meet respective needs of the application, we choose between different WCF service instance modes.

"PerSession" is the default instance mode for WCF Service. In order to use any specific techniques mentioned above, we need to configure serviceBehavior attribute for a service as follows:

C++
[ServiceContract()] public interface IInstanceManagDemoService 
{ [OperationContract] string Method1(); } 
[ServiceBehavior(InstanceContextMode=InstanceContextMode.PerCall)] 
public class InstanceManagDemoService : IInstanceManagDemoService 
{ public string Method1() { //method details here..... } } 

PerCall

In case of PerCall Instance mode, a new instance is created against each request coming from client and later disposed off when response is sent back from service as shown in the following diagram:

The above diagram shows that all requests coming from one or more clients are served by separate instance on server. The key points about this approach are follows:

  • We will prefer to use this approach when we don't need to maintain state between requests.
  • Scalability is a major concern and service holds expensive resources like communication ports, files or database connections, etc.

PerSession

For PerSession Instance mode, a new instance is maintained on server for one session. For separate session, another instance is created that serves the request. Normally, we use this technique when we need to maintain session between multiple requests.

The above diagram shows that for all requests from "WCF Client 1" are served by "Service Instance 1". Same is the case for client 2 and client 3 respectively. In this technique, the service instance is disposed of when a particular client completes all its method calls to perform a specific functionality.

The key points about this approach are as follows:

  • Approach is preferable when we need to maintain state.
  • Scalability is a concern but not the biggest concern.

Single

In case of Single/Singleton Instance mode, only one instance is created to serve all the requests coming from all clients.

In this technique, service instance is not disposed of and it stays on server to serve all incoming requests.

The key points about this approach are as follows:

  • We want to share global data using service.
  • Scalability is not a concern at all.

WCF Service Tutorials

License

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


Written By
Software Developer (Senior) Emaratech
United Arab Emirates United Arab Emirates
Imran Abdul Ghani has more than 10 years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET(MCSD.NET) since 2005. You can reach his blogging at WCF Tutorials, Web Development, SharePoint for Dummies.

Comments and Discussions

 
QuestionHello Pin
José Amílcar Casimiro23-Oct-13 6:26
José Amílcar Casimiro23-Oct-13 6:26 

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.