共用方式為


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 物件回收服務物件時呼叫。

適用於