我正在序列化具有这种结构的对象:
[ProtoContract]
public class Foo{
// some more properties
[ProtoMember(100)]
public IEnumerable<Bar>{get;set}
}
[ProtoContract]
public class Bar{
// a lot of properties
}
我正在使用此代码编写protobuf:
var memoryStream = new MemoryStream();
await new ProtoBufFormatter().WriteToStreamAsync(typeof(T), obj, ms, null, null);
一旦对象树超过一定大小(10000&gt;以及更多项) - 我最终得到一个OutOfMemoryException。
我想知道是否有方法告诉protobuf-net进行更好的序列化(避免OutOfMemoryException)或者是否有其他方法来解决问题?
更新 我得到以下异常:
Exception of type 'System.OutOfMemoryException' was thrown. at ProtoBuf.BufferPool.ResizeAndFlushLeft(Byte[]& buffer, Int32 toFitAtLeastBytes, Int32 copyFromIndex, Int32 copyBytes) in c:\Dev\protobuf-net\protobuf-net\BufferPool.cs:line 60 at ProtoBuf.ProtoWriter.DemandSpace(Int32 required, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 501 at ProtoBuf.ProtoWriter.WriteString(String value, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 600 at proto_29(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46 at proto_27(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46 at proto_23(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46 at proto_9(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 46 at proto_6(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.ProtoWriter.WriteRecursionSafeObject(Object value, Int32 key, ProtoWriter writer) in c:\Dev\protobuf-net\protobuf-net\ProtoWriter.cs:line 75 at proto_5(Object , ProtoWriter ) at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Write(Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 53 at ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 752 at ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 186 at ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 217 at WebApiContrib.Formatting.ProtoBufFormatter.WriteToStreamAsync(Type type, Object value, Stream stream, HttpContent content, TransportContext transportContext)