[英]How to properly migrate from WCF DatacontractSerializer to Protobuf-net?
[英]how to know if a WCF service uses protobuf-net?
我試圖讓谷歌協議緩沖區序列化與 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 軟件序列化。
最后通過讓服務使用 .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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.