Modifica

Condividi tramite


IInstanceProvider Interface

Definition

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

public interface class IInstanceProvider
public interface IInstanceProvider
type IInstanceProvider = interface
Public Interface IInstanceProvider

Examples

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
}
Public Class ObjectProviderBehavior
    Implements IInstanceProvider

  Private message As String
  Private service As SampleService = Nothing

  Public Sub New(ByVal msg As String)
    Console.WriteLine("The non-default constructor has been called.")
    Me.message = msg
    Me.service = New SampleService("Singleton sample service.")
  End Sub

  #Region "IInstanceProvider Members"

  Public Function GetInstance(ByVal instanceContext As InstanceContext, ByVal message As System.ServiceModel.Channels.Message) As Object Implements IInstanceProvider.GetInstance
    Console.WriteLine("GetInstance is called:")
    Return Me.service
  End Function

  Public Function GetInstance(ByVal instanceContext As InstanceContext) As Object Implements IInstanceProvider.GetInstance
    Console.WriteLine("GetInstance is called:")
    Return Me.service
  End Function

  Public Sub ReleaseInstance(ByVal instanceContext As InstanceContext, ByVal instance As Object) Implements IInstanceProvider.ReleaseInstance
    Console.WriteLine("ReleaseInstance is called. The SingletonBehaviorAttribute never releases.")
  End Sub

  #End Region
End Class

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
}
Public Class SingletonBehaviorAttribute
    Inherits Attribute
    Implements IContractBehaviorAttribute, IContractBehavior

  #Region "IContractBehaviorAttribute Members"

  Public ReadOnly Property TargetContract() As Type Implements IContractBehaviorAttribute.TargetContract
    Get
        Return GetType(ISampleService)
    End Get
  End Property

  #End Region

  #Region "IContractBehavior Members"

  Public Sub AddBindingParameters(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal parameters As System.ServiceModel.Channels.BindingParameterCollection) Implements IContractBehavior.AddBindingParameters
    Return
  End Sub

  Public Sub ApplyClientBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) Implements IContractBehavior.ApplyClientBehavior
    Return
  End Sub

  Public Sub ApplyDispatchBehavior(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint, ByVal dispatch As DispatchRuntime) Implements IContractBehavior.ApplyDispatchBehavior
    dispatch.InstanceProvider = New ObjectProviderBehavior("Custom ObjectProviderBehavior constructor.")
  End Sub

  Public Sub Validate(ByVal description As ContractDescription, ByVal endpoint As ServiceEndpoint) Implements IContractBehavior.Validate
    Return
  End Sub

  #End Region
End Class

Remarks

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 is 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 BehaviorExtensionElement 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 contract behavior approach, see the Example section.)

IInstanceProvider has two methods, GetInstance and ReleaseInstance. These methods are typically implemented to create service objects using a non-parameterless 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.

Note

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.

Methods

GetInstance(InstanceContext, Message)

Returns a service object given the specified InstanceContext object.

GetInstance(InstanceContext)

Returns a service object given the specified InstanceContext object.

ReleaseInstance(InstanceContext, Object)

Called when an InstanceContext object recycles a service object.

Applies to