我正在使用 protobuf-net 进行序列化和反序列化。
我遵循的方法是创建一个 RuntimeTypeModel 并添加所有需要序列化或支持序列化的类型。添加所有类型后,我使用 TypeModel.Compile() 以获得性能优势。
当一个类实例被序列化并作为 blob 保存到数据库中时,我能够在从数据库中获取时反序列化它。但是,当我更换机器并尝试反序列化时,我得到了类型为 token 的 ArgumentException
at ProtoBuf.ProtoReader.EndSubItem(SubItemToken token, ProtoReader reader) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 584
at ProtoBuf.ProtoReader.ReadTypedObject(Object value, Int32 key, ProtoReader reader, Type type) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 567
at ProtoBuf.ProtoReader.ReadObject(Object value, Int32 key, ProtoReader reader) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 543
at proto_157(Object , ProtoReader )
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 57
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 783
at ProtoBuf.ProtoReader.ReadTypedObject(Object value, Int32 key, ProtoReader reader, Type type) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 556
at ProtoBuf.ProtoReader.ReadObject(Object value, Int32 key, ProtoReader reader) in c:\Dev\protobuf-net\protobuf-net\ProtoReader.cs:line 543
at proto_190(Object , ProtoReader )
at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Serializers\CompiledSerializer.cs:line 57
at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in c:\Dev\protobuf-net\protobuf-net\Meta\RuntimeTypeModel.cs:line 783
at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 683
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 582
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type) in c:\Dev\protobuf-net\protobuf-net\Meta\TypeModel.cs:line 561
代码库是相同的,但是从我遇到这个问题的另一台机器上运行。
参与序列化的类非常简单
public class NetworkData
{
}
/// <summary>
/// Class for representing dashboard URL item
/// </summary>
public class DashboardURLItem : NetworkData
{
/// <summary>
/// Gets and Sets the URL
/// </summary>
public string URL { get; set; }
/// <summary>
/// Gets and Sets the Header for the URL Item
/// </summary>
public string Header { get; set; }
}
对于序列化,我使用以下代码
public byte[] SerializeData(T piclObjectData)
{
try
{
using (MemoryStream loclStream = new MemoryStream())
{
NWTypeModel.Serialize(loclStream, piclObjectData);
byte[] resultbuffer = new byte[loclStream.Length + 4]; // Those many bytes were generated in serialization + Message Length
Array.Copy(BitConverter.GetBytes(resultbuffer.Length), resultbuffer, 4);
Array.Copy(loclStream.GetBuffer(), 0, resultbuffer, 4, loclStream.Position);
return resultbuffer;
}
}
catch (Exception E)
{
Logger.WriteLog(ToString() + ": Exception while serializing - " + E.Message);
throw;
}
}
反序列化代码
public T DeSerializeData(byte[] piarBuffer)
{
try
{
using (MemoryStream loclStream = new MemoryStream())
{
loclStream.Seek(0, SeekOrigin.Begin);
loclStream.Write(piarBuffer, 4, piarBuffer.Length - 4); // 4 Bytes for Message length
loclStream.Seek(0, SeekOrigin.Begin);
return (T)NWTypeModel.Deserialize(loclStream, null, f_GenericType);
}
}
catch (Exception E)
{
Logger.WriteLog(ToString() + ": Exception while de-serializing - " + E.Message);
}
return null;
}
序列化和反序列化在数据库的同一台机器上工作正常,但是当从另一台机器上的数据库中获取值时,它无法反序列化。
如果我做错了什么或需要处理更多事情,请给我一些启发。
谢谢