c# - 如何知道 WCF 服务是否使用 protobuf-net?

标签 c# .net wcf protocol-buffers

我正在尝试让 google protocol buffer 序列化与 WCF 服务一起工作。服务和客户端确实同时启动并且对象返回给客户端,但我不确定现在使用的是什么序列化。将“behaviorExtensions”中的名称更改为不存在的名称没有任何区别,而且该服务根本没有 protobuf 配置,这就是我怀疑的原因。

客户端相关配置如下:

<system.serviceModel>
  <behaviors>
<endpointBehaviors>
        <behavior name="protoEndpointBehavior">
          <!--<protobuf/>-->
         </behavior>
</endpointBehaviors>
   </behaviors>

<bindings>
    <netTcpBinding>
      <binding name="netTcpIoService">
     <reliableSession inactivityTimeout="infinite" enabled="true" />
    </binding>
    </netTcpBinding>
</bindings>
<client>
    <endpoint address="net.tcp://localhost:9001/IoService/IoService"                     binding="netTcpBinding" bindingConfiguration="netTcpIoService"
                contract="IoService.IIoService" name="netTcpIoService" behaviorConfiguration="protoEndpointBehavior">
            <identity>
            <userPrincipalName value="a@b.nl" />
            </identity>
    </endpoint>

</client>
<extensions>
      <behaviorExtensions>
        <add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.480, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
      </behaviorExtensions>
</extensions>
</system.serviceModel> 

以及服务上的配置:

<system.serviceModel>
    <behaviors>
    <endpointBehaviors>
        <behavior name="protoEndpointBehavior">
          <!--<protobuf/>-->
        </behavior>
    </endpointBehaviors>
    </behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
    <bindings>
        <netTcpBinding>
          <binding name="netTcpIoService">
             <reliableSession inactivityTimeout="infinite" enabled="true" />
            </binding>
        </netTcpBinding>
    </bindings>
    <services>
        <service behaviorConfiguration="serviceBehaviour" name="ioService.IoService">
            <endpoint address="IoService" binding="netTcpBinding" bindingConfiguration="netTcpIoService" name="netTcpIoService" contract="ioService.IIoService" />
            <host>
                <baseAddresses>
                    <add baseAddress="net.tcp://localhost:9001/IoService" />
                </baseAddresses>
            </host>
        </service>
    </services>
</system.serviceModel>

服务接口(interface)和要序列化的对象:

[ServiceContract(CallbackContract = typeof(IIoServiceCallback)), ProtoBuf.ProtoContract]
public interface IIoService {
    [OperationContract]
    Article GetArticle(string number);
}

    [Serializable]
    [ProtoContract]
    public class Article : EntityBase
    {
        [ProtoMember(1)]
        public string Id;

        [ProtoMember(2)]
        public string Number;

        [ProtoMember(3)]
        public string Description;

        [ProtoMember(4)]
        public string Name;
}

编辑:

在意识到行为不会在客户端和服务器上共享并且它们必须在两点都配置行为后,我将其添加到服务配置中:

<extensions>
      <behaviorExtensions>
        <add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.480, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
      </behaviorExtensions>
</extensions>

然后该服务将无法运行,因为它无法使用版本 2.0.0.480 进行组装,因此必须将其更改为 .668。然后它又起作用了。但是我没有看到没有扩展行为的版本的性能改进,所以我仍然怀疑。

我确实添加了

ProtoBuf.Serializer.PrepareSerializer<Article>(); 

到服务,但它没有帮助。

仍然不确定如何检查 Article 对象是否通过 protobuf-net.dll 软件序列化。

最佳答案

最终通过让服务使用 .NET 序列化和客户端 protobuf 解决了所有问题。那么 null 将被返回。然后使服务也使用 protobuf 序列化并返回有效对象。

这些是正确的配置设置,客户端和服务的结构相同:

 <system.serviceModel>     
    <behaviors>
        <endpointBehaviors>
            <behavior name="protobuf">
                <protobuf />
            </behavior>
        </endpointBehaviors>
    </behaviors>
    <client_or_services/service>
        <endpoint address="net.tcp://localhost:9001/Service/Service" 
            binding="netTcpBinding" bindingConfiguration="netTcpService" behaviorConfiguration="protobuf"
            contract="IoService.IIoService" name="netTcpService">
        </endpoint>
    </client_or_services/service>
    <extensions>
          <behaviorExtensions>
            <add name="protobuf" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=2.0.0.668, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
          </behaviorExtensions>
    </extensions>   
  </system.serviceModel> 

关于c# - 如何知道 WCF 服务是否使用 protobuf-net?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30798689/

相关文章:

c# - 以编程方式使用免费代理服务器连接到网站

.net - 如何在执行大型 SQLCommand VB.Net 时显示进度条

c# - Fiddler - ReadResponse 失败 : The server did not return a response for this request

c# - WCF + EF 4.1 预加载和序列化问题

c# - 用于 32 位 dll 访问的自托管 WCF 服务

c# - 自定义 ServiceStack 验证响应

c# - 如何在 NSubstitute 中伪造一个对象并忽略其方法的内部实现?

c# - 在 C# 中使用正则表达式对列表元素进行编号

.net - 将数据集中所有已识别的重叠日期分组

c# - 处理迭代时出现时间跳跃问题