每个服务端点的多个Message Inspector

时间:2014-06-11 08:08:22

标签: wcf

如果WCF服务公开三个端点,那么我们可以为每个端点使用不同的消息检查器吗?如果是,那么我们如何在每个端点上应用?

1 个答案:

答案 0 :(得分:0)

首先,您应该创建端点行为以应用所需的格式化程序。

class CustomInspectorEndpointBehavior : IEndpointBehavior
{
    public void ApplyDispatchBehavior(ServiceEndpoint endpoint,
        EndpointDispatcher endpointDispatcher)
    {
        endpointDispatcher.DispatchRuntime.MessageInspectors.Add(new CustomFormatter());
    }

    ...
}

或者,如果存在一般逻辑,则可以使用服务行为类为每个端点应用行为:

class MultipleInspectorsServiceBehavior : IServiceBehavior
{
    public void ApplyDispatchBehavior(ServiceDescription serviceDescription,
        ServiceHostBase serviceHostBase)
    {
            foreach (var endpoint in serviceDescription.Endpoints)
                endpoint.EndpointBehaviors.Add(new CustomInspectorEndpointBehavior());
    }

    ...
}

最后,您可以从代码中应用端点行为......

var host = new ServiceHost(typeof(TheService));

foreach (var endpoint in host.Description.Endpoints)
    endpoint.EndpointBehaviors.Add(new CustomInspectorEndpointBehavior(c));

(如果是服务行为)

var host = new ServiceHost(typeof(TheService));

host.Description.Behaviors.Add(new MultipleInspectorsServiceBehavior());

...或者来自config:

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="customInspectorEndpointBehavior"
           type="CustomInspectorEndpointBehaviorExtensionElement, MyAssembly" />
    </behaviorExtensions>
  </extensions>
  <behaviors>
    <endpointBehaviors>
      <behavior name="behavior1">
        <customInspectorEndpointBehavior />
        <!--maybe some other behaviors-->
      </behavior>
      <behavior name="behavior2">
        <customInspectorEndpointBehavior />
        <!--maybe some other behaviors-->
      </behavior>
    </endpointBehaviors>
  </behaviors>
  <services>
    <service name="TheService">
      <endpoint address="address1" ... contract="..." behaviorConfiguration="behavior1" />
      <endpoint address="address2" ... contract="..." behaviorConfiguration="behavior1" />
      <endpoint address="address3" ... contract="..." behaviorConfiguration="behavior2" />
    </service>
  </services>
</system.serviceModel>

对于服务行为:

<system.serviceModel>
  <extensions>
    <behaviorExtensions>
      <add name="multipleInspectorsServiceBehavior"
           type="MultipleInspectorsServiceBehaviorExtensionElement, MyAssembly" />
    </behaviorExtensions>
  </extensions>
  <behaviors>
    <serviceBehaviors>
      <behavior name="CustomServiceBehavior">
        <multipleInspectorsServiceBehavior />
      <!--maybe some others-->
      </behavior>
    </serviceBehaviors>
  </behaviors>
  <services>
    <service name="TheService" behaviorConfiguration="ServiceBehavior">
      <endpoint address="address1" ... contract="..." />
      <endpoint address="address2" ... contract="..." />
      <endpoint address="address3" ... contract="..." />
    </service>
  </services>
</system.serviceModel>

在这里,你应该创建一个BehaviorExtensionElement来引用配置中的行为:

class CustomInspectorEndpointBehaviorExtensionElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get
        {
            return typeof(CustomInspectorEndpointBehavior);
        }
    }

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

class MultipleInspectorsServiceBehaviorExtensionElement : BehaviorExtensionElement
{
    public override Type BehaviorType
    {
        get
        {
            return typeof(MultipleInspectorsServiceBehavior);
        }
    }

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