Powered by Blogger.

Sunday, March 2, 2014

Instance Provider in WCF ?



Instance providers can be applied to services.
Service:Here are the steps for applying an instance provider to a service.

Declare: Implement the System.ServiceModel.Dispatcher.IInstanceProvider interface:

public class RajInstanceProvider :

IInstanceProvider
{

public object GetInstance(
InstanceContext instanceContext,
Message message)

{

//create the service instance here.

}

public object GetInstance(

InstanceContext instanceContext)

{

//create the service instance here.

}

public void ReleaseInstance(
InstanceContext instanceContext,
object instance)

{

//dispose of the service instance.

}

}
Attach: Implement the ApplyDispatchBehavior() method of the System.ServiceModel.

Description.IEndpointBehavior interface:

public class RajInstanceProvider :

IInstanceProvider,
IEndpointBehavior
{

public void ApplyDispatchBehavior(
ServiceEndpoint serviceEndpoint,
EndpointDispatcher endpointDispatcher)
{
endpointDispatcher.DispatchRuntime.InstanceProvider = this;
}
........
}

Inform In the code for a Windows Communication Foundation service host, add the instance context provider to the Behaviors collection of a System.ServiceModel.Description.EndpointDescription object:

ServiceHost host = new ServiceHost(typeof(Service));
host.Description.Endpoints[0].Behaviors.Add(
new RajInstanceProvider());



0 comments

Post a Comment