使用Protobuf-net的端点行为配置WCF

时间:2011-08-12 06:10:13

标签: wcf protobuf-net

我有一个WCF服务(.NET 4),它公开了4个端点,其中一个端点配置了protobuf-net(V1.0.0.280)行为扩展。但是,我注意到protobuf-net行为开始用于所有定义的端点,包括未配置的protbuf-net!我在下面粘贴了我的配置。我错过了什么吗?非常感谢任何帮助.. thx

    <service name="MyService" behaviorConfiguration="MyServiceBehavior">
    <endpoint address="Http.Basic" binding="basicHttpBinding" bindingConfiguration="Http.Basic.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
    <endpoint address="Http.Binary" binding="customBinding" bindingConfiguration="Http.Binary.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
    <endpoint address="Tcp.Binary" binding="customBinding" bindingConfiguration="Tcp.Binary.Config" contract="IMyService" behaviorConfiguration="DefaultBehavior" />
    <endpoint address="Http.ProtoBuf" binding="basicHttpBinding" bindingConfiguration="Http.Basic.Config" contract="IMyService" behaviorConfiguration="ProtoBufBehavior" />
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost:8085/MyService"/>
        <add baseAddress="net.tcp://localhost:8086/MyService"/>
      </baseAddresses>
    </host>
  </service>

  <behaviors>
    <serviceBehaviors>
      <behavior name="MyServiceBehavior">
        <serviceMetadata httpGetEnabled="true"/>
        <serviceDebug includeExceptionDetailInFaults="true"/>
      </behavior>
    </serviceBehaviors>
    <endpointBehaviors>
      <behavior name="DefaultBehavior">
        <dataContractSerializer maxItemsInObjectGraph="2147483647" />
      </behavior>
      <behavior name="ProtoBufBehavior">
        <ProtoBufSerialization />
      </behavior>
    </endpointBehaviors>
  </behaviors>

  <bindings>
    <basicHttpBinding>
      <binding name="Http.Basic.Config" closeTimeout="00:01:00" openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00" />
      <security mode="None">
        <transport clientCredentialType="None" proxyCredentialType="None" realm="" />
        <message clientCredentialType="UserName" algorithmSuite="Default" />
      </security>
      </binding>
    </basicHttpBinding>
    <customBinding>
      <binding name="Http.Binary.Config" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
        <binaryMessageEncoding />
        <httpTransport allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" />
      </binding>
      <binding name="Tcp.Binary.Config" closeTimeout="00:10:00" openTimeout="00:10:00" receiveTimeout="00:10:00" sendTimeout="00:10:00">
        <binaryMessageEncoding />
        <tcpTransport hostNameComparisonMode="StrongWildcard" />
      </binding>
    </customBinding>
  </bindings>

1 个答案:

答案 0 :(得分:1)

这很奇怪,但是(检查代码)我只在在WCF提供给我的端点中应用的更改:

    void IEndpointBehavior.ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        ReplaceDataContractSerializerOperationBehavior(endpoint);
    }

    void IEndpointBehavior.ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
        ReplaceDataContractSerializerOperationBehavior(endpoint);
    }

    private static void ReplaceDataContractSerializerOperationBehavior(ServiceEndpoint serviceEndpoint)
    {
        foreach (OperationDescription operationDescription in serviceEndpoint.Contract.Operations)
        {
            ReplaceDataContractSerializerOperationBehavior(operationDescription);
        }
    }


    private static void ReplaceDataContractSerializerOperationBehavior(OperationDescription description)
    {
        DataContractSerializerOperationBehavior dcsOperationBehavior = description.Behaviors.Find<DataContractSerializerOperationBehavior>();
        if (dcsOperationBehavior != null)
        {
            description.Behaviors.Remove(dcsOperationBehavior);
            description.Behaviors.Add(new ProtoOperationBehavior(description));
        }
    }

即。 “给定一个端点(通过WCF),遍历该端点中的每个操作(方法),并将串行器从DCS更改为PB”

这提出了一个有趣的可能性,即合同定义(以及操作定义)本身在所有端点之间共享 - 但我真的不确定。如果的情况,我不认为每个端点可能有不同的处理器。然而,我不是WCF大师。这......令人困惑。