语言

IContractBehavior.ApplyDispatchBehavior 方法

定义

跨协定实现客户端的修改或扩展。

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

参数

contractDescription
ContractDescription

要修改的协定说明。

endpoint
ServiceEndpoint

公开协定的终结点。

dispatchRuntime
DispatchRuntime

控制服务执行的调度运行时。

示例

下面的代码示例假定调用的自定义 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
}

注解

ApplyDispatchBehavior实现查看、修改或添加特定协定中所有消息的服务运行时的自定义扩展,或针对该协定中的一个特定操作。 有关可在服务应用程序中执行的自定义的详细信息,请参阅 DispatchRuntime 和 DispatchOperation。

如果行为仅用于客户端应用程序,则 ApplyDispatchBehavior 该方法可能会引发 NotImplementedException 异常。

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

请注意,说明中可以有两个具有相同名称的操作(每个方向都有一个),因此,如果必须循环访问协定是双工协定的操作,则必须关联终结点 DispatchRuntime 与属性返回 CallbackClientRuntime 的消息方向。

此外,由于其他行为可能已经从运行时添加或删除了某些操作,因此无法保证说明中的操作数与 DispatchOperation 属性中的 Operations 对象数相同。

适用于