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.

 



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

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.
     

Explanation of Instance Context Mode

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

 

 

Per–Call Services

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
Creating service 

  1. Create a WCF service application
  2. In Service Contract, create an Operation Contract. This method (Operation Contract) will return an Integer.


using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace PerCallsample

{

[ServiceContract]


public
interface
IService1

{

[OperationContract]


int MyMethod();

}

}

Create a Service class.

  1. This class will have Service Behavior Attribute.
  2. Service Behavior Attribute is configured for Per- Call Instance management.
  3. InstanceContextMode property of Service Behavior class takes Per-Call value for enum type InstanceContextMode Type. ( See highlighted text in below code

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace PerCallsample

{

[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]


public
class
Service1 : IService1,IDisposable

{


static
int _callCounter = 0;


public Service1()

{

_callCounter = _callCounter + 1;

}


public
int MyMethod()

{


return _callCounter;

}


public
void Dispose()

{

_callCounter = _callCounter – 1;

}

}

}

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. 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using PerCallClient.ServiceReference1;

namespace PerCallClient

{


class
Program

{


static
void Main(string[] args)

{


Service1Client proxy = new
Service1Client();


int x= proxy.MyMethod();

proxy.MyMethod();


Console.WriteLine(x.ToString());


Console.Read();


int y = proxy.MyMethod();


Console.WriteLine(y.ToString());


Console.Read();

proxy.Close();

}

}

} 

Output

 

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 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Runtime.Serialization;

using System.ServiceModel;

using System.Text;

namespace PerCallsample

{


//[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]


public
class
Service1 : IService1,IDisposable

{


static
int _callCounter = 0;


public Service1()

{

_callCounter = _callCounter + 1;

}


public
int MyMethod()

{


return _callCounter;

}


public
void Dispose()

{

_callCounter = _callCounter – 1;

}

}

}

 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.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s