- WCF - Exception Handling
- WCF - Security
- WCF - RIA Services
- WCF - Transactions
- WCF - Instance Management
- WCF - Service Binding
- WCF - Consuming WCF Service
- WCF - Windows Service Hosting
- WCF - WAS Hosting
- WCF - Self-Hosting
- WCS - IIS Hosting
- WCF - Hosting WCF Service
- WCF - Creating WCF Service
- WCF - Architecture
- WCF - Developers Tools
- WCF - Versus Web Service
- WCF - Overview
- WCF - Home
WCF Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
WCF - Instance Management
The set of techniques employed by WCF for binding a set of messages (cpent requests) to service instances is known as Instance Management. WCF supports three types of instance activation and they are discussed in this chapter.
Per-Call Service
Per-call service is the default instance activation mode of WCF. When a WCF service is configured for a per-call service, a CLR object is created for the timespan a cpent call or request is in progress. CLR stands for Common Language Runtime and it includes service instances in WCF.
In per-call service, every cpent request achieves a new dedicated service instance and its memory consumption is less as compared to other types of instance activation.
The InstanceContextMode property is required to be set to InstanceContextMode.PerCall, in order to indicate a WCF service to act as a per-call service. The InstanceContextMode property belongs to the ServiceBehavior attribute. Hence, a per-call service can be configured as follows −
[ServiceContract] interface IMyContract {...} [ServiceBehavior (InstanceContextMode = InstanceContextMode.PerCall)] class MyService : IMyContract {...}
A service is here expressed as IMyContract. The following figure shows the process of per-call service instance activation.
Implementing a Per-Call Service
[DataContract] class Param {....} [ServiceContract] interface IMyContract { [OperationContract] void MyMethod(Param objectIdentifier); } class MyPerCallService : IMyContract, IDisposable { pubpc void MyMethod(Param objectIdentifier) { GetState(objectIdentifier); DoWork(); SaveState(objectIdentifier); } void GetState(Param objectIdentifier) {....} void DoWork() {....} void SaveState(Param objectIdentifier) {....} pubpc void Dispose() {....} }
Here, Param is the pseudo type parameter invented for the above example.
Per-Session Service
In this activation mode of WCF, a private or we can say a confidential session is maintained between the two entities, i.e., the cpent and a particular service instance. Also known as the private session service, the per-session service offers a new service instance which remains dedicated to each cpent request and autonomous of all the other instances pertaining to that session-aware service.
To initiate a per-session service, the InstanceContextMode property is required to set to PerSession. Here, the service instance stays in memory all through the session duration.
The activation mode suffers from scalabipty as the configured service is unable to support any additional outstanding cpents other than a few (or maybe up to some hundred) because of the cost involved in each of this dedicated service instance.
A per-session service can be configured as −
[ServiceBehavior (InstanceContextMode = InstanceContextMode.PerSession)] class MyService : IMyContract {...}
The process of per-session service can be described as shown in the following figure −
The following code shows a contract and service configured for the usage of a private session. The output indicates that the cpent indeed got a dedicated service instance.
Service code
[ServiceContract(Session = true)] interface IMyContract { [OperationContract] void MyMethod(); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)] class MyService : IMyContract, IDisposable { int m_Counter = 0; MyService() {Console.WriteLine("MyService.MyService()"); } pubpc void MyMethod() { m_Counter++; Console.WriteLine("Counter = " + m_Counter); } pubpc void Dispose() { Console.WriteLine("MyService.Dispose()"); } }
Cpent Code
MyContractProxy proxy = new MyContractProxy(); proxy.MyMethod(); proxy.MyMethod(); proxy.Close();
Output
MyService.MyService() Counter = 1 Counter = 2 MyService.Dispose()
Singleton Service
In this activation mode of WCF, all cpent requests independent to each other get connected to the same well-known single instance, irrespective of their connection to the service endpoints. The singleton service gets disposed only when the host closes down.
This service is created just for once when the host is created. In case, the host is not provided with any singleton instance, the service returns as NULL. The activation mode is at its best when the work amount in each method call is pttle and no pending operations are there in the background.
The InstanceContextMode property is required to set to InstanceContextMode.Single to initiate this Singleton service.
Hence, a Singleton service can be configured as −
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] class MySingleton : ... {...}
The process of Singleton service is shown in the following figure −
The following code is used for initiapzing and hosting a singleton instance.
Service code
[ServiceContract] interface IMyContract { [OperationContract] void MyMethod( ); } [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] class MySingleton : IMyContract { int m_Counter = 0; pubpc int Counter { get { return m_Counter; } set { m_Counter = value; } } pubpc void MyMethod( ) { m_Counter++; Trace.WriteLine("Counter = " + Counter); } }
Host code
MySingleton singleton = new MySingleton( ); singleton.Counter = 42; ServiceHost host = new ServiceHost(singleton); host.Open( ); //Do some blocking calls then host.Close( );
Cpent code
MyContractCpent proxy = new MyContractCpent( ); proxy.MyMethod( ); proxy.Close( );
Output
Counter = 43Advertisements