Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

IInstanceProvider Interface

Declares methods that provide a service object or recycle a service object for a Windows Communication Foundation (WCF) service.

Namespace: System.ServiceModel.Dispatcher
Assembly: System.ServiceModel (in system.servicemodel.dll)

public interface IInstanceProvider
public interface IInstanceProvider
public interface IInstanceProvider
Not applicable.

Implement the IInstanceProvider interface to control the creation and recycling of service objects when one is requested or disposed by an InstanceContext object.

Once the IInstanceProvider interface has been implemented, you must assign your custom instance provider object to the InstanceProvider property using either an endpoint behavior (a IEndpointBehavior object) or a contract behavior (a IContractBehavior object).

If the insertion mechanism is an endpoint behavior you can also implement a BehaviorExtensionSection object that can insert your custom behavior using a configuration file. If the insertion mechanism is a contract behavior, you can insert the behavior programmatically prior to the opening of the service host or you can implement a custom attribute. (For an example of the latter approach, see the Example section.)

IInstanceProvider has two methods, GetInstance and ReleaseInstance. These methods are typically implemented to create service objects using a non-default constructor or to initialize or dispose of some state related to the lifetime of the object. Service object pooling is one example of custom IInstanceProvider functionality.

Typically, the InstanceContext invokes the GetInstance when the InstanceContext is first created and invokes the ReleaseInstance method when the InstanceContext is closed.

There are two ways to cause an InstanceContext object to release a service object before the InstanceContext is closed. The first method is to set the ReleaseInstanceMode to AfterCall or BeforeAndAfterCall. The second method is to call the ReleaseServiceInstance method. If this is done, the InstanceContext calls the ReleaseInstance method on the dispatcher's instance provider. If a new message arrives after the instance has been released, WCF creates a new instance using the GetInstance method.

NoteNote:

If the InstanceContextMode of the service is Single, the system does not call the GetInstance or ReleaseInstance methods even when the user did not provide a well-known service object unless the user directly calls ReleaseServiceInstance and then calls GetServiceInstance.

The following code example shows how to implement IInstanceProvider that provides "singleton" behavior; it always returns the same service instance and does not recycle it.

public class ObjectProviderBehavior : IInstanceProvider
{

  string message;
  SampleService service = null;

  public ObjectProviderBehavior(string msg)
  {
    Console.WriteLine("The non-default constructor has been called.");
    this.message = msg;
    this.service = new SampleService("Singleton sample service.");
  }

  #region IInstanceProvider Members

  public object GetInstance(InstanceContext instanceContext, System.ServiceModel.Channels.Message message)
  {
    Console.WriteLine("GetInstance is called:");
    return this.service;
  }

  public object GetInstance(InstanceContext instanceContext)
  {
    Console.WriteLine("GetInstance is called:");
    return this.service;
  }

  public void ReleaseInstance(InstanceContext instanceContext, object instance)
  {
    Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.");
  }

  #endregion
}

The following code example shows how to implement a custom attribute that implements IContractBehavior to insert the custom service instance provider. It also implements IContractBehaviorAttribute, which binds its use to a specific contract.

public class SingletonBehaviorAttribute : Attribute, IContractBehaviorAttribute, IContractBehavior
{

  #region IContractBehaviorAttribute Members

  public Type TargetContract
  {
    get { return typeof(ISampleService); }
  }

  #endregion

  #region IContractBehavior Members

  public void AddBindingParameters(ContractDescription description, ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection parameters)
  {
    return;
  }

  public void ApplyClientBehavior(ContractDescription description, ServiceEndpoint endpoint, ClientRuntime clientRuntime)
  {
    return;
  }

  public void ApplyDispatchBehavior(ContractDescription description, ServiceEndpoint endpoint, DispatchRuntime dispatch)
  {
    dispatch.InstanceProvider = new ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.");
  }

  public void Validate(ContractDescription description, ServiceEndpoint endpoint)
  {
    return;
  }

  #endregion
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft