Endpoint behavior configuration WCF with Protobuf-net

杀马特。学长 韩版系。学妹 提交于 2019-12-21 12:02:50

问题


I have a WCF service (.NET 4) that exposes 4 endpoints of which one endpoint is configured with protobuf-net (V1.0.0.280) behavior extension. However, I noticed that protobuf-net behavior kicks in for ALL defined endpoints including the ones protbuf-net is not configured for! I have pasted my config below. Am I missing something? Any help is greatly appreciated .. 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:


That is odd, but (checks the code) I only ever apply changes within an endpoint supplied to me by 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));
        }
    }

i.e. "given an endpoint (by WCF), loop over each operation (method) in that endpoint, and change the serializer from DCS to PB"

This raises the intriguing possibility that the contract definitions (and hence the operation definitions) are themselves shared between all the endpoints - but I am honestly not sure about that. If that is the case, I don't see that it would ever be possible to have different processors per endpoint. I am not, however, a WCF-guru. This is... perplexing.



来源:https://stackoverflow.com/questions/7036209/endpoint-behavior-configuration-wcf-with-protobuf-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!