我试图让google协议缓冲区序列化与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>
服务接口和要序列化的对象:
[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软件序列化。
答案 0 :(得分:1)
最后通过让服务使用.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>