Bewerken

Delen via


IEndpointBehavior.ApplyDispatchBehavior Method

Definition

Implements a modification or extension of the service across an endpoint.

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

Parameters

endpoint
ServiceEndpoint

The endpoint that exposes the contract.

endpointDispatcher
EndpointDispatcher

The endpoint dispatcher to be modified or extended.

Examples

The following code example shows the implementation of an endpoint behavior that adds an System.ServiceModel.Dispatcher.IDispatchMessageInspector object in a service application. In this case, the EndpointBehaviorMessageInspector class implements System.ServiceModel.Dispatcher.IDispatchMessageInspector to inspect the inbound and outbound message, the IEndpointBehavior interface to insert the inspector class into the inspection system for all endpoints to which the behavior applies, and the System.ServiceModel.Configuration.BehaviorExtensionElement to enable the message inspector behavior using an application configuration file.

The first step is to implement the message inspector.

// IDispatchMessageInspector Members

public object AfterReceiveRequest(ref System.ServiceModel.Channels.Message request, IClientChannel channel, InstanceContext instanceContext)
{
  Console.WriteLine("AfterReceiveRequest called.");
  return null;
}

public void BeforeSendReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
{
  Console.WriteLine("BeforeSendReply called.");
}

The next code example shows the use of the ApplyDispatchBehavior method to add the message inspector to the DispatchRuntime.MessageInspectors property.

// IEndpointBehavior Members
public void AddBindingParameters(ServiceEndpoint serviceEndpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
{
  return;
}

public void ApplyClientBehavior(ServiceEndpoint serviceEndpoint, ClientRuntime behavior)
{
  behavior.MessageInspectors.Add(new EndpointBehaviorMessageInspector());
}

public void ApplyDispatchBehavior(ServiceEndpoint serviceEndpoint, EndpointDispatcher endpointDispatcher)
{
  endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new EndpointBehaviorMessageInspector());
}

public void Validate(ServiceEndpoint serviceEndpoint)
{
  return;
}

The following code example shows the implementation of System.ServiceModel.Configuration.BehaviorExtensionElement in order to enable use of the message inspector behavior from a configuration file.

// BehaviorExtensionElement members
public override Type BehaviorType
{
  get { return typeof(EndpointBehaviorMessageInspector); }
}

protected override object CreateBehavior()
{
  return new EndpointBehaviorMessageInspector();
}

Finally, the following configuration file shows how the preceding example can be used from configuration.

<configuration>
  <system.serviceModel>
    <services>
      <service 
        name="Microsoft.WCF.Documentation.SampleService"
        behaviorConfiguration="metadataSupport"
      >
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/ServiceMetadata" />
          </baseAddresses>
        </host>
        <endpoint
          address="/SampleService"
          binding="wsHttpBinding"
          behaviorConfiguration="withMessageInspector" 
          contract="Microsoft.WCF.Documentation.ISampleService"
        />
        <endpoint
           address="mex"
           binding="mexHttpBinding"
           contract="IMetadataExchange"
        />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
      <behavior name="metadataSupport">
        <serviceMetadata httpGetEnabled="true" httpGetUrl=""/>
      </behavior>
      </serviceBehaviors>
      <endpointBehaviors>
        <behavior name="withMessageInspector">
          <endpointMessageInspector />
        </behavior>
      </endpointBehaviors>
    </behaviors>
    <extensions>
      <behaviorExtensions>
        <add 
          name="endpointMessageInspector"
          type="Microsoft.WCF.Documentation.EndpointBehaviorMessageInspector, HostApplication, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null"
        />
      </behaviorExtensions>
    </extensions>
  </system.serviceModel>
</configuration>

Remarks

Implement the ApplyDispatchBehavior method to view, modify, or extend the service runtime across all messages or for specific operations in an endpoint. For details about what customizations you can do in a service application, see System.ServiceModel.Dispatcher.DispatchRuntime and System.ServiceModel.Dispatcher.DispatchOperation.

It is recommended that the ApplyDispatchBehavior method throw a NotImplementedException exception if the behavior is only intended for use in a client application.

Note that there can be two operations with the same name in the description when using a callback contract (one operation in each direction). If you are iterating through operations, you must correlate the message direction between the endpoint System.ServiceModel.Dispatcher.DispatchRuntime and what is returned by the DispatchRuntime.CallbackClientRuntime property.

In addition, because other behaviors may have already added or removed some operations from the runtime, there is no guarantee that there are the same number of operations in description as there are System.ServiceModel.Dispatcher.DispatchOperation objects in the DispatchRuntime.Operations property.

Applies to