如何:检查或修改参数

可以通过实现 System.ServiceModel.Dispatcher.IParameterInspector 接口并将该接口插入客户端或服务运行时来检查或修改 Windows Communication Foundation (WCF) 客户端对象或 WCF 服务上单个操作的传入或传出消息。 通常,操作行为用于为单个操作添加参数检查器;其他行为可以用于在更大范围内提供对运行库的方便访问。 有关详细信息,请参阅扩展客户端扩展调度程序

检查或修改参数

  1. 实现 System.ServiceModel.Dispatcher.IParameterInspector 接口。

  2. 实现 System.ServiceModel.Description.IOperationBehaviorSystem.ServiceModel.Description.IEndpointBehaviorSystem.ServiceModel.Description.IServiceBehaviorSystem.ServiceModel.Description.IContractBehavior(取决于要求的范围),以便将参数检查器添加到 ClientOperation.ParameterInspectorsDispatchOperation.ParameterInspectors 属性。

  3. 在对 ClientBase<TChannel>.Open 调用 ICommunicationObject.OpenSystem.ServiceModel.ChannelFactory<TChannel> 方法前,插入行为。 有关详细信息,请参阅使用行为配置和扩展运行时

示例

下面的代码示例按顺序演示以下各项:

  • 参数检查器实现。

  • 行为实现,它使用 、 和 插入参数检查器。

  • 配置文件,它在客户端应用程序中加载和运行终结点行为,以便在客户端上插入参数检查器。

#region IParameterInspector Members
public void AfterCall(string operationName, object[] outputs, object returnValue, object correlationState)
{
  Console.WriteLine(
    "IParameterInspector.AfterCall called for {0} with return value {1}.",
    operationName,
    returnValue.ToString()
  );
}

public object BeforeCall(string operationName, object[] inputs)
{
  Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName);
  return null;
}
#Region "IParameterInspector Members"
        Public Sub AfterCall(ByVal operationName As String, ByVal outputs() As Object, ByVal returnValue As Object, _
                             ByVal correlationState As Object) Implements IParameterInspector.AfterCall
            Console.WriteLine("IParameterInspector.AfterCall called for {0} with return value {1}.", _
                              operationName, returnValue.ToString())
        End Sub

        Public Function BeforeCall(ByVal operationName As String, ByVal inputs() As Object) As Object Implements _
        IParameterInspector.BeforeCall
            Console.WriteLine("IParameterInspector.BeforeCall called for {0}.", operationName)
            Return Nothing
        End Function
using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Configuration;
using System.ServiceModel.Description;
using System.ServiceModel.Dispatcher;
using System.Text;

namespace Microsoft.WCF.Documentation
{
  public class InspectorInserter : BehaviorExtensionElement, IServiceBehavior, IEndpointBehavior, IOperationBehavior
  {
    #region IServiceBehavior Members
    public void AddBindingParameters(
      ServiceDescription serviceDescription,
      ServiceHostBase serviceHostBase,
      System.Collections.ObjectModel.Collection<ServiceEndpoint> endpoints,
      BindingParameterCollection bindingParameters
    )
    { return; }

    public void ApplyDispatchBehavior(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase)
    {
      foreach (ChannelDispatcher chDisp in serviceHostBase.ChannelDispatchers)
      {
        foreach (EndpointDispatcher epDisp in chDisp.Endpoints)
        {
          epDisp.DispatchRuntime.MessageInspectors.Add(new Inspector());
          foreach (DispatchOperation op in epDisp.DispatchRuntime.Operations)
            op.ParameterInspectors.Add(new Inspector());
        }
      }
    }

    public void Validate(ServiceDescription serviceDescription, ServiceHostBase serviceHostBase){ return; }

    #endregion
    #region IEndpointBehavior Members
    public void AddBindingParameters(
      ServiceEndpoint endpoint, BindingParameterCollection bindingParameters
    ) { return; }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, ClientRuntime clientRuntime)
    {
      clientRuntime.MessageInspectors.Add(new Inspector());
      foreach (ClientOperation op in clientRuntime.Operations)
        op.ParameterInspectors.Add(new Inspector());
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, EndpointDispatcher endpointDispatcher)
    {
      endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new Inspector());
      foreach (DispatchOperation op in endpointDispatcher.DispatchRuntime.Operations)
        op.ParameterInspectors.Add(new Inspector());
    }

    public void Validate(ServiceEndpoint endpoint){ return; }
    #endregion
    #region IOperationBehavior Members
    public void AddBindingParameters(
      OperationDescription operationDescription, BindingParameterCollection bindingParameters
    )
    { return; }

    public void ApplyClientBehavior(OperationDescription operationDescription, ClientOperation clientOperation)
    {
      clientOperation.ParameterInspectors.Add(new Inspector());
    }

    public void ApplyDispatchBehavior(OperationDescription operationDescription, DispatchOperation dispatchOperation)
    {
      dispatchOperation.ParameterInspectors.Add(new Inspector());
    }

    public void Validate(OperationDescription operationDescription){ return; }

    #endregion

    public override Type BehaviorType
    {
      get { return typeof(InspectorInserter); }
    }

    protected override object CreateBehavior()
    { return new InspectorInserter(); }
  }
}
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.ServiceModel.Configuration
Imports System.ServiceModel.Description
Imports System.ServiceModel.Dispatcher
Imports System.Text

Namespace Microsoft.WCF.Documentation
    Public Class InspectorInserter
        Inherits BehaviorExtensionElement
        Implements IServiceBehavior, IEndpointBehavior, IOperationBehavior
#Region "IServiceBehavior Members"
        Public Sub AddBindingParameters(ByVal serviceDescription As ServiceDescription, _
                       ByVal serviceHostBase As ServiceHostBase, ByVal endpoints As _
                       System.Collections.ObjectModel.Collection(Of ServiceEndpoint), _
                       ByVal bindingParameters As BindingParameterCollection) Implements IServiceBehavior.AddBindingParameters
            Return
        End Sub

        Public Sub ApplyDispatchBehavior(ByVal serviceDescription As ServiceDescription, _
                                         ByVal serviceHostBase As ServiceHostBase) Implements _
                                         IServiceBehavior.ApplyDispatchBehavior
            For Each chDisp As ChannelDispatcher In serviceHostBase.ChannelDispatchers
                For Each epDisp As EndpointDispatcher In chDisp.Endpoints
                    epDisp.DispatchRuntime.MessageInspectors.Add(New Inspector())
                    For Each op As DispatchOperation In epDisp.DispatchRuntime.Operations
                        op.ParameterInspectors.Add(New Inspector())
                    Next op
                Next epDisp
            Next chDisp
        End Sub

        Public Sub Validate(ByVal serviceDescription As ServiceDescription, ByVal serviceHostBase As ServiceHostBase) _
        Implements IServiceBehavior.Validate
            Return
        End Sub

#End Region
#Region "IEndpointBehavior Members"
        Public Sub AddBindingParameters(ByVal endpoint As ServiceEndpoint, ByVal bindingParameters _
                                        As BindingParameterCollection) Implements IEndpointBehavior.AddBindingParameters
            Return
        End Sub

        Public Sub ApplyClientBehavior(ByVal endpoint As ServiceEndpoint, ByVal clientRuntime As ClientRuntime) _
        Implements IEndpointBehavior.ApplyClientBehavior
            clientRuntime.MessageInspectors.Add(New Inspector())
            For Each op As ClientOperation In clientRuntime.Operations
                op.ParameterInspectors.Add(New Inspector())
            Next op
        End Sub

        Public Sub ApplyDispatchBehavior(ByVal endpoint As ServiceEndpoint, ByVal endpointDispatcher As _
                                         EndpointDispatcher) Implements IEndpointBehavior.ApplyDispatchBehavior
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(New Inspector())
            For Each op As DispatchOperation In endpointDispatcher.DispatchRuntime.Operations
                op.ParameterInspectors.Add(New Inspector())
            Next op
        End Sub

        Public Sub Validate(ByVal endpoint As ServiceEndpoint) Implements IEndpointBehavior.Validate
            Return
        End Sub
#End Region
#Region "IOperationBehavior Members"
        Public Sub AddBindingParameters(ByVal operationDescription As OperationDescription, _
                                        ByVal bindingParameters As BindingParameterCollection) Implements _
                                        IOperationBehavior.AddBindingParameters
            Return
        End Sub

        Public Sub ApplyClientBehavior(ByVal operationDescription As OperationDescription, ByVal _
                                       clientOperation As ClientOperation) Implements IOperationBehavior.ApplyClientBehavior
            clientOperation.ParameterInspectors.Add(New Inspector())
        End Sub

        Public Sub ApplyDispatchBehavior(ByVal operationDescription As OperationDescription, ByVal dispatchOperation As _
                                         DispatchOperation) Implements IOperationBehavior.ApplyDispatchBehavior
            dispatchOperation.ParameterInspectors.Add(New Inspector())
        End Sub

        Public Sub Validate(ByVal operationDescription As OperationDescription) Implements IOperationBehavior.Validate
            Return
        End Sub

#End Region

        Public Overrides ReadOnly Property BehaviorType() As Type
            Get
                Return GetType(InspectorInserter)
            End Get
        End Property

        Protected Overrides Function CreateBehavior() As Object
            Return New InspectorInserter()
        End Function
    End Class
End Namespace
  <client>
      <endpoint 
        address="http://localhost:8080/SampleService" 
        behaviorConfiguration="clientInspectorsAdded" 
        binding="wsHttpBinding"
        bindingConfiguration="WSHttpBinding_ISampleService" 
        contract="ISampleService"
        name="WSHttpBinding_ISampleService"
      >
      </endpoint>
  </client>
<behaviors>
  <endpointBehaviors>
    <behavior name="clientInspectorsAdded">
      <clientInterceptors />
    </behavior>
  </endpointBehaviors>
</behaviors>
<extensions>
  <behaviorExtensions>
    <add 
      name="clientInterceptors" 
      type="Microsoft.WCF.Documentation.InspectorInserter, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
  />
  </behaviorExtensions>
</extensions>

请参阅