Share via


IContractBehavior.ApplyClientBehavior 方法

定義

實作合約上用戶端的修改或延伸。

public:
 void ApplyClientBehavior(System::ServiceModel::Description::ContractDescription ^ contractDescription, System::ServiceModel::Description::ServiceEndpoint ^ endpoint, System::ServiceModel::Dispatcher::ClientRuntime ^ clientRuntime);
public void ApplyClientBehavior (System.ServiceModel.Description.ContractDescription contractDescription, System.ServiceModel.Description.ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime);
abstract member ApplyClientBehavior : System.ServiceModel.Description.ContractDescription * System.ServiceModel.Description.ServiceEndpoint * System.ServiceModel.Dispatcher.ClientRuntime -> unit
Public Sub ApplyClientBehavior (contractDescription As ContractDescription, endpoint As ServiceEndpoint, clientRuntime As ClientRuntime)

參數

contractDescription
ContractDescription

延伸所適用的合約描述。

endpoint
ServiceEndpoint

端點。

clientRuntime
ClientRuntime

用戶端執行階段。

範例

下列程式碼範例假設自訂的 IInstanceProvider 實作呼叫提供「單一」行為的 ObjectProviderBehavior,並一律傳回相同的服務執行個體,而且不會加以回收。

如果要插入執行個體提供者自訂,此範例會示範如何實作自訂屬性 (SingletonBehaviorAttribute),該屬性會實作 IContractBehavior 以插入自訂服務執行個體提供者。 它也會實作 IContractBehaviorAttribute,繫結其用法與 ISampleService 合約。

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

備註

您可以實作 ApplyClientBehavior 方法以檢視、修改或加入自訂延伸至所有訊息或一個特定作業的用戶端執行階段。 如需有關您可以使用用戶端執行階段物件進行何種自訂的詳細資訊,請參閱 ClientRuntimeClientOperation

如果行為只適合在服務應用程式中使用,ApplyClientBehavior 方法可以擲回 NotImplementedException 例外狀況。

使用指定之服務合約的各個端點會呼叫這個方法一次。

請注意,在描述中可以有兩個作業使用相同的名稱 (每個方向一個),因此如果您必須逐一查看合約是雙工合約的作業時,必須聯結端點 ClientRuntime 以及由 CallbackDispatchRuntime 屬性傳回的端點之間的訊息方向。

由於其他行為可能已經加入或從執行階段移除某些作業,因此不保證描述中的作業數目和 ClientOperation 屬性中的 Operations 物件相同。

適用於