共用方式為


How to: Inspect or Modify Parameters

You can inspect or modify the incoming or outgoing messages for a single operation on a Windows Communication Foundation (WCF) client object or a WCF service by implementing the System.ServiceModel.Dispatcher.IParameterInspector interface and inserting it into the client or service runtime. Typically an operation behavior is used to add parameter inspectors for a single operation; other behaviors can be used to provide easy access to the runtime at a greater scope. For more information, see Extending Clients and Extending Dispatchers.

Inspecting or Modifying Parameters

  1. Implement the System.ServiceModel.Dispatcher.IParameterInspector interface.

  2. Implement a System.ServiceModel.Description.IOperationBehavior, System.ServiceModel.Description.IEndpointBehavior, System.ServiceModel.Description.IServiceBehavior or System.ServiceModel.Description.IContractBehavior (depending upon the required scope) to add your parameter inspector to either the System.ServiceModel.Dispatcher.ClientOperation.ParameterInspectors or System.ServiceModel.Dispatcher.DispatchOperation.ParameterInspectors properties.

  3. Insert your behavior prior to calling System.ServiceModel.ClientBase.Open or the System.ServiceModel.ICommunicationObject.Open method on the System.ServiceModel.ChannelFactory. For details, see Configuring and Extending the Runtime with Behaviors.

Example

The following code examples show, in order:

  • A parameter inspector implementation.

  • The behavior implementation that inserts the parameter inspector using a System.ServiceModel.Description.IOperationBehavior, System.ServiceModel.Description.IEndpointBehavior, and an System.ServiceModel.Description.IServiceBehavior.

  • A configuration file that loads and runs the endpoint behavior in a client application to insert the parameter inspector on the client.

#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;
}
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(); }
  }
}
  <client>
      <endpoint 
        address="https://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>

See Also

Concepts

Configuring and Extending the Runtime with Behaviors


© 2007 Microsoft Corporation. All rights reserved.
Build Date: 2009-08-07