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.
There are three instance management techniques in WCF.
Instance management is a service side activation implementation detail that should not get manifested to the client. WCF achieve this by BEHAVIOR.
- A Behavior is a local attribute of service that does not affect its communication patterns.
- Client does not aware of the Behavior.
- Service does not manifest behavior in binding or metadata.
- Practically Behaviors are WCF classes.
- These are used at the runtime operations.
- This applies directly to service implementation class.
- This affects all the endpoints.
- This is used to configure service instance mode.
- This is used to configure operation behavior.
- This affects only implementation of a particular operation.
- This could apply only to method that implements contract operation.
- This is not applied to the contract definition itself.
Explanation of Service Behavior attribute
- ServiceBehaviorAttribute class is used to configure the instance context mode.
- As we can see in highlighted text below that InstanceContextMode is a public property.
- Type of InstanceContextMode property is enum InstanceContextMode.
- It is inside namespace System.ServiceModel.
Explanation of Instance Context Mode
- This is a enum type
- This type is defined under namespace System.ServiceModel.
Every Client request gets a new dedicated service instance. A service instance exists only when a client call is in progress.
Steps of Per-Call Service activation
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
- Create a WCF service application.
- In Service Contract, create an Operation Contract. This method (Operation Contract) will return an Integer.
- 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)
Explanation of code
- I have taken one static variable because its value will persist across all service instances.
- In constructor of service class, I am incrementing value of this variable by one.
- In Operation contract, I am returning value of variable to client.
- On Dispose of service instance, I am again decrementing value of variable by one.
- Add a Console application in same project as client.
- Add service reference of service.
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
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.
I have given introduction of Instance management is WCF. I also explained One Instance management technique.
In next articles of this series, I will explain about Per-Session Instance management technique and Single Instance management technique. Till then, Happy Coding.