IInstanceProvider 接口

定义

声明一些方法,以便为 Windows Communication Foundation (WCF) 服务提供服务对象,或回收该服务的服务对象。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

示例

下面的代码示例演示如何实现可提供“单一实例”行为的 IInstanceProvider;它始终返回同一服务实例,且不回收该服务实例。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

下面的代码示例演示如何实现一个自定义属性,该自定义属性可实现 IContractBehavior,从而插入自定义服务实例提供程序。The following code example shows how to implement a custom attribute that implements IContractBehavior to insert the custom service instance provider. 它还实现可将其用法绑定到特定协定的 IContractBehaviorAttributeIt 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

注解

实现 IInstanceProvider 接口,以便在 InstanceContext 对象请求或释放服务对象时,控制服务对象的创建和回收。Implement the IInstanceProvider interface to control the creation and recycling of service objects when one is requested or disposed by an InstanceContext object.

实现 IInstanceProvider 接口后,您必须使用终结点行为(InstanceProvider 对象)或协定行为(IEndpointBehavior 对象)将自定义实例提供程序对象分配给 IContractBehavior 属性。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).

如果插入机制为终结点行为,则您还能实现可使用配置文件插入自定义行为的 BehaviorExtensionElement 对象。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 有两种方法:GetInstanceReleaseInstanceIInstanceProvider 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. 例如,服务对象池就属于一种自定义 IInstanceProvider 功能。Service object pooling is one example of custom IInstanceProvider functionality.

通常,在第一次创建 InstanceContext 时,GetInstance 会调用 InstanceContext,在 ReleaseInstance 关闭时,调用 InstanceContextTypically, the InstanceContext invokes the GetInstance when the InstanceContext is first created and invokes the ReleaseInstance method when the InstanceContext is closed.

有两种方法可使 InstanceContext 对象在 InstanceContext 关闭之前释放服务对象。There are two ways to cause an InstanceContext object to release a service object before the InstanceContext is closed. 第一种方法是将 ReleaseInstanceMode 设置为 AfterCallBeforeAndAfterCallThe first method is to set the ReleaseInstanceMode to AfterCall or BeforeAndAfterCall. 第二种方法是调用 ReleaseServiceInstance 方法。The second method is to call the ReleaseServiceInstance method. 如果执行此操作,InstanceContext 将对调度程序的实例提供程序调用 ReleaseInstance 方法。If this is done, the InstanceContext calls the ReleaseInstance method on the dispatcher's instance provider. 如果在释放实例后新消息到达,WCF 将使用方法创建一个新的实例 GetInstanceIf a new message arrives after the instance has been released, WCF creates a new instance using the GetInstance method.

备注

如果服务的 InstanceContextModeSingle,则系统不会调用 GetInstanceReleaseInstance 方法(即使用户未提供已知的服务对象),除非用户直接调用 ReleaseServiceInstance,再调用 GetServiceInstanceIf 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.

方法

GetInstance(InstanceContext)

如果给出指定的 InstanceContext 对象,则返回服务对象。Returns a service object given the specified InstanceContext object.

GetInstance(InstanceContext, Message)

如果给出指定的 InstanceContext 对象,则返回服务对象。Returns a service object given the specified InstanceContext object.

ReleaseInstance(InstanceContext, Object)

InstanceContext 对象回收服务对象时调用。Called when an InstanceContext object recycles a service object.

适用于