Tag: protobuf net

如何配置protobuf-net的RuntimeModel.Default以支持序列化/反序列化SessionSecurityToken?

BinaryFormatter能够简单地处理序列化: private byte[] TokenToBytes(SessionSecurityToken token) { if (token == null) { return null; } using (var memoryStream = new MemoryStream()) { var binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, token); return memoryStream.ToArray(); } } 当我尝试用protobuf-net替换BinaryFormatter时: using (var memoryStream = new MemoryStream()) { Serializer.Serialize(memoryStream, token); return memoryStream.ToArray(); } 我得到以下exception: 不期望类型,也不能推断出合同:System.IdentityModel.Tokens.SessionSecurityToken 我尝试添加: RuntimeTypeModel.Default.Add(typeof(SessionSecurityToken), true); 哪个超过exception,但我现在得到一个零字节数组。 如何正确配置protobuf-net以序列化SessionSecurityToken? 另一方面,SessionSecurityToken没有无参数构造函数。 using (var […]

如何使用protobuf-net嵌入类型信息以进行去序列化/序列化?

我希望能够以这样的方式序列化IMessage的具体实例,以便保留/嵌入类型信息(类似于例如Json.NET中可用的类型),以便在反序列化时可以使用类型信息来实现那些类型信息。具体实例。 我很清楚下面的de / / serialization方法不起作用。 如何更改它们以便它们起作用,我们将不胜感激。 public interface IMessage {} public interface IEvent : IMessage {} [ProtoContract] public class DogBarkedEvent : IEvent { [ProtoMember(0)] public string NameOfDog { get; set; } [ProtoMember(1)] public int Times { get; set; } } //Somewhere in a class far, far away public byte[] Serialize(IMessage message) { using(var stream = […]

使用ProtoBuf-Net处理父子关系的最佳方法是什么?

我的测试应用程序抛出此exception以使用ProtoBuf进行序列化。 SceneryFile:temp.ad2 ProtoBuf.ProtoException: Possible recursion detected (offset: 2 level(s): SceneryFile: temp.ad2 at ProtoBuf.ProtoWriter.CheckRecursionStackAndPush(Object instance( in c:\protbuf-net\protobuf-net\ProtoWriter.sc:line321 我发现了其他这些问题 protobuf-net:检测到可能的递归 检测到Protobuf-net可能的递归:序列化儿童和父母 我的树的深度只有4.我看看是否可以更改ProtoBuf.ProtoWriter.RecursionCheckDepth但我找不到在我的代码中执行此操作的方法。 基于Marc对最后一个问题的回答,我认为通过引用意味着再次引用相同的对象实例 – 所以在我的情况下,SceneryFile引用自身或被引用到其他地方(实际上它是基类)。 从某种意义上说,所有直接的孩子都会提到它。 这里的答案似乎表明这可能是问题所在: 根据上面第二个问题的结果,我删除了父级序列化,这确实阻止了这个问题。 是否有某种方法允许我保持对序列化中父级的引用,或者我是否需要执行诸如使用父级索引然后在反编译时重构图形的操作? 谢谢

在C#中解析原始协议缓冲区字节流

给定协议缓冲区编码的Stream或byte[]但不知道对象类型本身,我们如何打印消息的骨架? 该用例用于调试基于protobuf的IO,用于根本原因分析。 如果现有工具可以解析二进制文件中的原始协议缓冲区字节流 – 这将是很棒的! 另一种方法是使用ProtoBuf.NET类ProtoReader()来保持一致,直到我们遇到错误但ProtoReader()的使用不清楚。 我从下面开始,但找不到关于如何使用ProtoReader类实际执行它的好文档。 该项目的源代码也不是很容易遵循……所以会很感激一些提示/帮助 using (var fs = File.OpenRead(filePath)) { using (var pr = new ProtoReader(fs, TypeModel.Create(), null)) { // Use ProtoReader to march through the bytes // Printing field number, type, size and payload values/bytes } }

protobuf-net的没有用

我正在使用protobuf-net v2 beta r431用于C#.net 4.0应用程序。 在我的应用程序中,我有一个Dictionary ,我需要序列化。 一个类MyClass实现了IMyClass接口。 根据protobuf的文档,我编写了以下代码: [ProtoContract] [ProtoInclude(1, typeof(MyClass))] public interface IMyClass { int GetId(); string GetName(); } [ProtoContract] [Serializable] public class MyClass : IMyClass { [ProtoMember(1)] private int m_id = 0; [ProtoMember(2)] private string m_name = string.Empty; public MyClass(int id, string name) { m_id = id; m_name = name; } public […]

使用ProtoBuf-Net反序列化嵌套动态类型的问题

我正在尝试使用ProtoBuf-Net r668对包含在DynamicType = true的几个图层中的对象进行反序列化。 使用较旧版本的ProtoBuf-Net(v1),它将反复序列化而不会出现问题。 然而,使用最新版本,它失败了 ProtoBuf.ProtoException:内部错误; 发生了密钥不匹配 [ProtoContract] private class A { [ProtoMember(1, DynamicType = true)] public object Body { get; set; } } [ProtoContract] private class B { [ProtoMember(1, DynamicType = true)] public object Body { get; set; } } [ProtoContract] private class C { [ProtoMember(1)] public string Name { get; set; } […]

ProtoBuf-net AsReference需要Activator.CreateInstance中的公共构造函数吗?

在我的两个类中工作时看起来像这样(最小) using System; using System.Collections.Generic; using System.Collections; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using System.IO; using ProtoBuf; namespace Sandbox { public partial class Form1 : Form { public Form1() { Family family = new Family(); Child child1 = new Child(1); Child child2 = new Child(2); Parent parent = new Parent(new List() { child1, child2 }); […]

使用Protobuf-net序列化object

我想序列化和反序列化存储在对象数组中的一些值。 public class Sample { public object[] Data; } 我在运行时知道在数组中期望什么类型。 实际上,我想将Sample内部的Data视为消息流。 单独地,我知道我可以使用Serializer.NonGeneric.SerializeWithLengthPrefix使用PrefixStyle.Base128写出每个对象,并为Deserialise Type-Resolver创建一个Prefix-> Type映射。 请参阅什么是protobuf-net SerializeWithLengthPrefix标记参数? 我正在努力的实际上是让protobuf-net自动为我做这件事,然后将这种行为嵌套在一个包含的消息中。 据我所知,流中生成的消息将完全有效。 例如; 假设我有这个对象数组: new object[] { “Hello”, 42, 0.3529321, true } 和标签图 var typeToTag = new Dictionary() { {typeof (string), 1}, {typeof (int), 2}, {typeof (double), 3}, {typeof (bool), 4}, }; 我可以使用非genericsSerializeWithLengthPrefix将它们写入流中 foreach (var value in data) Serializer.NonGeneric.SerializeWithLengthPrefix( […]

异步protobuf序列化

protobuf-net实现的一个限制是它同步调用底层流。 通过不提供异步API,例如BeginSerialize / EndSerialize或TPL等价物,我们被迫绑定一个等待同步流I / O的线程。 有没有计划在protobuf-net中提供异步方法,或者有任何创造性的方法来解决这个问题?

使用Protobuf-net和Monotouch for IOS序列化IEnumerable槽WCF

我正在尝试在Monotouch / Monodevelop上为IOS编写WCF服务。 我使用[DataMember] / [DataContract]等标准属性作为我的可序列化对象,使用[ServiceContract] / [OperationContract]作为我的界面。 一切正常,但是当我尝试在接口实现(服务器端)上实现返回IEnumerable的方法时,它没有用。 所以为了解决我的问题,我尝试使用最新版本的protobuf-net protobuf-net v2 beta r404。 但是我仍然从Protobuf-net那里得到序列化错误。 请注意,“MyObject”中的IEnumerable序列化没有问题。 以下是我的代码现在的样子: 为MyObject: [ProtoContract] public class MyObject { public MyObject () { } [ProtoMember(1)] public int Id {get;set;} [ProtoMember(2)] public IEnumerable myObjects {get;set;} } 我的界面(Windows上的服务器端): [ServiceContract] public interface ITouchService { [OperationContract, ProtoBehavior] MyObject Execute(); [OperationContract, ProtoBehavior] IEnumerable ExecuteENUM (); } […]