IEndpointBehavior 接口

定义

实现可用于扩展服务或客户端应用程序中终结点的运行时行为的方法。Implements methods that can be used to extend run-time behavior for an endpoint in either a service or client application.

public interface class IEndpointBehavior
public interface IEndpointBehavior
type IEndpointBehavior = interface
Public Interface IEndpointBehavior
派生

示例

下面的代码示例演示了在服务应用程序中添加 System.ServiceModel.Dispatcher.IDispatchMessageInspector 对象的终结点行为的实现。The following code example shows the implementation of an endpoint behavior that adds an System.ServiceModel.Dispatcher.IDispatchMessageInspector object in a service application. 在此情况下,EndpointBehaviorMessageInspector 类将通过使用应用程序配置文件,实现 System.ServiceModel.Dispatcher.IDispatchMessageInspector 以检查入站和出站消息,实现 IEndpointBehavior 接口以将检查器类插入该行为应用到的所有终结点的检查系统,以及实现 System.ServiceModel.Configuration.BehaviorExtensionElement 以启用消息检查器行为。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.");
}

下面的代码示例演示了如何使用 ApplyDispatchBehavior 方法将消息检查器添加到 DispatchRuntime.MessageInspectors 属性。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;
}

下面的代码示例演示了如何实现 System.ServiceModel.Configuration.BehaviorExtensionElement,以便从配置文件使用消息检查器行为。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>

注解

实现 IEndpointBehavior 接口可以在应用程序级别,为客户端或服务应用程序修改、检查或扩展终结点范围内的某些执行方面。Implement the IEndpointBehavior interface to modify, examine, or extend some aspect of endpoint-wide execution at the application level for either client or service applications.

  • 使用 AddBindingParameters 方法可以在运行时传递自定义数据,从而使绑定支持自定义行为。Use the AddBindingParameters method to pass custom data at runtime to enable bindings to support custom behavior.

  • 使用 ApplyClientBehavior 方法可以在客户端应用程序中修改、检查或插入对终结点中的扩展。Use the ApplyClientBehavior method to modify, examine, or insert extensions to an endpoint in a client application.

  • 使用 ApplyDispatchBehavior 方法可以在服务应用程序中修改、检查或插入对终结点范围执行的扩展。Use the ApplyDispatchBehavior method to modify, examine, or insert extensions to endpoint-wide execution in a service application.

  • 使用 Validate 方法可以确认 ServiceEndpoint 是否满足特定需求。Use the Validate method to confirm that a ServiceEndpoint meets specific requirements. 这可用于确保终结点启用某一配置设置、支持特定功能和其他要求。This can be used to ensure that an endpoint has a certain configuration setting enabled, supports a particular feature and other requirements.

IEndpointBehavior 对象可以利用这些方法中的任何一种,但通常只有一种方法是重要的;在这种情况下,未使用的方法可以返回,而不执行任何操作。IEndpointBehavior objects can make use of any of these methods, but often only one is important; in such cases, the unused methods can return, performing no action.

备注

所有 IEndpointBehavior 方法会将 ServiceEndpoint 对象作为参数传递。All of the IEndpointBehavior methods pass a ServiceEndpoint object as a parameter. 此参数只用于检查;如果您修改了 ServiceEndpoint 对象,则执行行为将是不确定的。This parameter is for examination only; if you modify the ServiceEndpoint object the execution behavior is undefined.

通常,IEndpointBehavior 对象用于访问服务应用程序中的 System.ServiceModel.Dispatcher.DispatchRuntimeSystem.ServiceModel.Dispatcher.DispatchOperationSystem.ServiceModel.Dispatcher.EndpointDispatcherSystem.ServiceModel.Dispatcher.ChannelDispatcher 对象的各个属性,以及客户端应用程序中的 System.ServiceModel.Dispatcher.ClientRuntimeSystem.ServiceModel.Dispatcher.ClientOperation 对象的各个属性。IEndpointBehavior objects are typically used to access the various properties of the System.ServiceModel.Dispatcher.DispatchRuntime, System.ServiceModel.Dispatcher.DispatchOperation, System.ServiceModel.Dispatcher.EndpointDispatcher, and System.ServiceModel.Dispatcher.ChannelDispatcher objects in a service application and the System.ServiceModel.Dispatcher.ClientRuntime and System.ServiceModel.Dispatcher.ClientOperation in a client application. 此外,您还可分别通过使用 ClientRuntime.CallbackDispatchRuntimeDispatchRuntime.CallbackClientRuntime 属性来访问双向客户端和服务的属性。In addition, you can access the properties of duplex clients and services using the ClientRuntime.CallbackDispatchRuntime and DispatchRuntime.CallbackClientRuntime properties, respectively.

有关可用的各种属性和自定义的说明,请参阅扩展 ServiceHost 和服务模型层For a description of the various properties and customizations available, see Extending ServiceHost and the Service Model Layer.

确定自定义后(并在必要时实现的自定义接口),并且 IEndpointBehavior 确定为适当的自定义范围后,必须通过实现 IEndpointBehavior 并将终结点行为添加到运行时,将自定义插入到 Windows Communication Foundation (WCF)运行时。Once a customization has been decided upon (and the customization interface implemented if necessary) and the IEndpointBehavior has been decided is the appropriate scope of customization, the customization must be inserted into the Windows Communication Foundation (WCF) runtime by implementing IEndpointBehavior and adding the endpoint behavior to the runtime.

有两种方法可以将该行为添加到运行时:There are two ways to add the behavior to the runtime:

  • 以编程方式,先将自定义终结点行为添加到 Behaviors 属性,然后打开服务主机(位于服务应用程序中)或通道工厂(位于客户端应用程序中)。Programmatically add the custom endpoint behavior to the Behaviors property prior to the opening of the service host (in a service application) or the channel factory (in a client application).

  • 使用应用程序配置文件配置该行为。Configure the behavior using an application configuration file. 有关详细信息,请参阅<behaviorExtensions>For details, see <behaviorExtensions>.

若要执行服务自定义计划任务,则必须先将 IEndpointBehavior 对象添加到 ServiceEndpoint.Behaviors 属性,然后构造服务运行时,该构造操作是在对 ICommunicationObject.Open 调用 System.ServiceModel.ServiceHost 方法时执行的。To perform the service customization task for which it is intended, the IEndpointBehavior object must be added to the ServiceEndpoint.Behaviors property prior to the construction of the service runtime, which occurs when ICommunicationObject.Open method is called on System.ServiceModel.ServiceHost. 若要执行客户端自定义任务,则必须对 IEndpointBehavior 调用 ServiceEndpoint.Behaviors 方法或 ChannelFactory<TChannel>.CreateChannel 方法之前,先将 ICommunicationObject.Open 对象添加到 ChannelFactory<TChannel> 属性。To perform a client customization task, the IEndpointBehavior object must be added to the ServiceEndpoint.Behaviors property before calling the ChannelFactory<TChannel>.CreateChannel method or the ICommunicationObject.Open method on ChannelFactory<TChannel>.

方法

AddBindingParameters(ServiceEndpoint, BindingParameterCollection)

实现此方法可以在运行时将数据传递给绑定,从而支持自定义行为。Implement to pass data at runtime to bindings to support custom behavior.

ApplyClientBehavior(ServiceEndpoint, ClientRuntime)

在终结点范围内实现客户端的修改或扩展。Implements a modification or extension of the client across an endpoint.

ApplyDispatchBehavior(ServiceEndpoint, EndpointDispatcher)

在终结点范围内实现服务的修改或扩展。Implements a modification or extension of the service across an endpoint.

Validate(ServiceEndpoint)

实现此方法可以确认终结点是否满足某些设定条件。Implement to confirm that the endpoint meets some intended criteria.

适用于