Learning along the way . . . .

Understanding Instance Management in WCF

instance management is the technique WCF uses to bind client requests to service instances, governing which service instance handles which client request, and when. WCF Supports 3 types of instance activation

Per Call : per call services allocate and destroy a new service instance for each client request.

Per Session: session full services allocate a service instance for each client connection.

Singleton: singleton services share the same service instance for all clients, across all connections and activations.

by and large the service instance mode is strictly a server side implementation detail, hat should not manifest itself on the client side in any way.

now let’s define a service, that demonstrates the instance management in real life.

Creating the Class Library

let’s create a service, create a new project, take a Class Library template, name it InstanceLib, let’s do some house keeping with the newly created project.

Delete the file Class1.cs from the project workspace.

Add a new Interface named ISingleCallService to the project, a new file ISingleCallService.cs will be added to the project.

Add a new Class named SingleCallService, to the project. that will implement the ISingleCallService interface, a new file SingleCallService.cs will be added to the project.

Add a new Interface named ISessionService to the project, a new file ISessionService.cs will be added to the project.

Add a new Class named SessionService, to the project. that will implement the ISessionService interface, a new file SessionService.cs will be added to the project.

Add a new Interface named ISingletonService to the project, a new file ISingletonService.cs will be added to the project.

Add a new Class named SingletonService, to the project. that will implement the ISingletonService interface, a new file SingletonService.cs will be added to the project.

to keep the things simple, let’s assume that each interface defines only one method, which simply adds a passed value, to its member variable.

Important

You can notice ServiceBehavior attribute of the class, which has been defined as InstanceContextMode.PerCall, it specifies that a new InstanceContext object is created prior to and recycled subsequent to each calll. if you omit this attribute or do not specify, then InstanceContextMode.PerCall is assumed by default.

Important

You can notice ServiceBehavior attribute of the class, which has been defined as InstanceContextMode.Singlel, it specifies that Only one InstanceContext object is used for all incoming calls and is not recycled to the calls. if the service object does not exist, one is created.

if you notice the all 3 service implementation, you will notice the difference in ServiceBehavior class attribute of each class, apart from that, all interfaces has been implemented has the same implementation. they simply add a passed value to a member variable of the class.

Build the project and your Class Library is ready.

Hosting the Service

for the sake of simplicity, i have have hosted this service as a self hosted service, to create the Host application, create a new Console based application.

Before we write any code for the Host application, let’s define the configuration file for the Service(s).