我有时在尝试使用 protobuf.net 反序列化对象时收到以下异常。我很惊讶,因为我从来没有超过一个线程同时反序列化同一个对象,而且 protobuf.net 源似乎没有使用任何静态对象进行反序列化。该异常确实提出了一个解决方案,但我不确定如何实现,因此欢迎举个例子。
Base Exception Type:
System.TimeoutException: Timeout while inspecting metadata; this may indicate a deadlock. This can often be avoided by preparing necessary serializers during application initialization, rather than allowing multiple threads to perform the initial metadata inspection
at ProtoBuf.Meta.RuntimeTypeModel.TakeLock(Boolean& lockTaken)
at ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto(Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled)
at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey)
Inner Exception Type:
System.TimeoutException: Timeout while inspecting metadata; this may indicate a deadlock. This can often be avoided by preparing necessary serializers during application initialization, rather than allowing multiple threads to perform the initial metadata inspection
at ProtoBuf.Meta.RuntimeTypeModel.TakeLock(Boolean& lockTaken)
at ProtoBuf.Meta.RuntimeTypeModel.FindOrAddAuto(Type type, Boolean demand, Boolean addWithContractOnly, Boolean addEvenIfAutoDisabled)
at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey)
Stack Trace:
at ProtoBuf.Meta.RuntimeTypeModel.GetKey(Type type, Boolean demand, Boolean getBaseKey)
at ProtoBuf.Meta.TypeModel.GetKey(Type& type)
at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type)
问候, 马克
编辑添加:我这样定义我的可序列化对象:
[ProtoContract]
public class Job
{
[ProtoMember(1)]
public long JobId { get; private set; }
}
我很难在每个可序列化对象上轻松调用 PrepareSerialiser,因为我在不同的命名空间中有很多对象。但是想想如果 protobuf 被要求同时反序列化两个相同类型的对象,一个它以前从未见过的类型,会发生什么?
最佳答案
老问题,但如果有人遇到这个错误,请检查您使用的 DLL 版本。此异常出现在可移植版本中的可能性非常高。
有几个 PR 与可移植版本的此问题相关,它们是 https://github.com/mgravell/protobuf-net/pull/98和 https://github.com/mgravell/protobuf-net/pull/114 .
关于c# - Protobuf.net 异常 - 检查元数据时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7372585/