IInstanceProvider 接口

定义

声明一些方法,以便为 Windows Communication Foundation (WCF) 服务提供服务对象,或回收该服务的服务对象。

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

示例

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

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,从而插入自定义服务实例提供程序。 它还实现可将其用法绑定到特定协定的 IContractBehaviorAttribute

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 对象请求或释放服务对象时,控制服务对象的创建和回收。

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

如果插入机制为终结点行为,则您还能实现可使用配置文件插入自定义行为的 BehaviorExtensionElement 对象。 如果插入机制为协定行为,您可以在打开服务主机之前以编程方式插入行为,也可以实现自定义属性。 (有关此协定行为的示例,请参见“示例”部分。)

IInstanceProvider 有两种方法:GetInstanceReleaseInstance。 这些方法通常实现为使用非无参数构造函数创建服务对象,或者初始化或释放与对象的生存期相关的某些状态。 例如,服务对象池就属于一种自定义 IInstanceProvider 功能。

通常,在第一次创建 InstanceContext 时,GetInstance 会调用 InstanceContext,在 ReleaseInstance 关闭时,调用 InstanceContext

有两种方法可使 InstanceContext 对象在 InstanceContext 关闭之前释放服务对象。 第一种方法是将 ReleaseInstanceMode 设置为 AfterCallBeforeAndAfterCall。 第二种方法是调用 ReleaseServiceInstance 方法。 如果执行此操作,InstanceContext 将对调度程序的实例提供程序调用 ReleaseInstance 方法。 如果新消息在实例发布后到达,WCF 将使用该方法 GetInstance 创建一个新实例。

备注

如果服务的 InstanceContextModeSingle,则系统不会调用 GetInstanceReleaseInstance 方法(即使用户未提供已知的服务对象),除非用户直接调用 ReleaseServiceInstance,再调用 GetServiceInstance

方法

GetInstance(InstanceContext)

如果给出指定的 InstanceContext 对象,则返回服务对象。

GetInstance(InstanceContext, Message)

如果给出指定的 InstanceContext 对象,则返回服务对象。

ReleaseInstance(InstanceContext, Object)

InstanceContext 对象回收服务对象时调用。

适用于