Instance Management in WCF Part # 1

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.

Instancemanagement1.gif

There are three instance management techniques in WCF.

Instancemanagement2.gif

Instancemanagement3.gif

BEHAVIOR

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

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

    Instancemanagement4.gif

SERVICE BEHAVIOR

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

OPERATION BEHAVIOR

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

Explanation of Service Behavior attribute

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

    Instancemanagement5.gif

Explanation of Instance Context Mode

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

    Instancemanagement6.gif

    Instancemanagement7.gif

Per-Call Services

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

Instancemanagement8.gif

Instancemanagement9.gif

Steps of Per-Call Service activation

Instancemanagement10.gif

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.

    Instancemanagement11.gif
     
  3. 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)

    Instancemanagement12.gif

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.

    Instancemanagement13.gif

Output

Instancemanagement14.gif

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

Instancemanagement15.gif

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.


Conclusion

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.

Up Next
    Ebook Download
    View all
    Learn
    View all