Im使用ProtoBuf.Net作爲NetDataContractSerializer(或BinarySerializer)POC'ing, 我已在此發佈代碼code。 這是試驗和錯誤,仍然不能用列表,字典等。 它看起來即將在錯誤的方向, 是否可行?ProtoBuf.Net - 使用Proto作爲TypeFormatter
您的意見將不勝感激。
Im使用ProtoBuf.Net作爲NetDataContractSerializer(或BinarySerializer)POC'ing, 我已在此發佈代碼code。 這是試驗和錯誤,仍然不能用列表,字典等。 它看起來即將在錯誤的方向, 是否可行?ProtoBuf.Net - 使用Proto作爲TypeFormatter
您的意見將不勝感激。
我使用Protobuf.net,我通過標記像這樣我的課做到了:
namespace music
{
[ProtoContract]
public class Album
{
[ProtoMember(1)]
public string Name { get; set; }
[ProtoMember(2)]
public List<string> TrackList { get; set; }
}
}
這適用於字典和列表了。
相同的編號約定適用於作爲屬性使用.proto消息文件,但可以包括在基類這樣的屬性:
[ProtoContract]
[ProtoInclude(10, typeof(TypeInheritingFromPerson))]
[ProtoInclude(11, typeof(AnotherTypeInheritingFromPerson))]
public abstract class Person
{
[DataMember]
[ProtoMember(1)]
public string Name { get; set; }
...
,然後使用這行代碼序列化:
MemoryStream stream = new MemoryStream();
ProtoBuf.Serializer.Serialize<Album>(stream, album);
當然你也可以使用文件流,而不是內存流:)的
如果您使用WCF,你可以交換的DataContractSerializer爲Protobuf序列化程序直接在配置文件中(如從protobuf.net文檔粘貼的副本),因此您不需要手動調用任何序列化代碼:
將以下內容添加到服務器和客戶端app.config中在system.serviceModel部分:
<behaviors>
<endpointBehaviors>
<behavior name="ProtoBufBehaviorConfig">
<ProtoBufSerialization/>
</behavior>
</endpointBehaviors>
</behaviors>
<extensions>
<behaviorExtensions>
<add name="ProtoBufSerialization" type="ProtoBuf.ServiceModel.ProtoBehaviorExtension, protobuf-net, Version=1.0.0.255, Culture=neutral, PublicKeyToken=257b51d87d2e4d67"/>
</behaviorExtensions>
</extensions>
配置您的終端有一個behaviorConfiguration如下:
<service name="TK.Framework.Samples.ServiceModel.Contract.SampleService">
<endpoint address="http://myhost:9003/SampleService" binding="basicHttpBinding" behaviorConfiguration="ProtoBufBehaviorConfig"
bindingConfiguration="basicHttpBindingConfig" name="basicHttpProtoBuf" contract="ISampleServiceContract" />
</service>
<client>
<endpoint address="http://myhost:9003/SampleService" binding="basicHttpBinding"
bindingConfiguration="basicHttpBindingConfig" contract="ISampleServiceContract"
name="BasicHttpProtoBufEndpoint" behaviorConfiguration="ProtoBufBehaviorConfig"/>
</client>
我希望這是對你有幫助,但讓我知道,如果您有任何疑問,或任何目前還不清楚:)
感謝您的回覆,我使用Protobuf.NetV2和使用運行時間註冊(代碼附加)。我可能會失去一些東西,因爲即時通訊做一個遞歸的所有成員和他們的成員註冊圖表中的所有需要的對象。當添加List <>或Dictionary時,我無法序列化List/Dictionary成員的鋸齒狀/嵌套數組。我猜想我缺少Protobuf用於序列化List/Dictionary的原因,其成員沒有標記爲Proto Attributes。 – eyan 2012-03-27 12:31:44
你的數據/類結構是什麼樣的?您是否使用了protobuf屬性無法修飾的類型? – Franchesca 2012-03-27 15:03:30
嗯......你看看ProtoBuf.Meta命名空間中有什麼可用的東西嗎? – Franchesca 2012-03-27 15:08:12
提前知道候選根類型的全套嗎?或者是你打算序列化完全未知的類型? – 2012-03-25 16:34:05
提前知道,類型是已知的兩側序列化和反序列化 – eyan 2012-03-25 19:20:55