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 异常。

对于使用指定服务协定的每个终结点,都将对其调用一次此方法。

注意,说明中可能存在拥有相同名称的两个操作(每个方向一个);因此,如果必须循环执行协定为双向协定的操作,则您必须在终结点 ClientRuntimeCallbackDispatchRuntime 属性所返回的终结点之间关联消息方向。

原因是其他行为可能已经在运行时中添加或移除了某些操作,所以无法保证说明中具有的操作数目同 ClientOperation 属性中的 Operations 对象数目相等。

适用于