【问题标题】:Protobuf exception - Object reference not set to an instance of an objectProtobuf 异常 - 对象引用未设置为对象的实例
【发布时间】:2017-07-23 09:36:42
【问题描述】:

我正在尝试使用 protobuf 序列化下面的类,但由于“对象引用”错误而失败。更多细节如下。通过查看错误详细信息,知道可能出了什么问题吗? 注意:我的用户对象太大,它有很多子对象和属性。所以不要在此处添加 User 类的详细信息。

[ProtoContract]
public class MyPrincipal
{

    public MyPrincipal(string Id, int myId)
    {
        this.Id = Id;
        this.MyId = myId;
        this.Principals = new Dictionary<MyPrincipalType, User>();         
    }

    [ProtoMember(1)]
    public string Id { get; set; }


    [ProtoMember(2)]
    public int MyId { get; set; }

    [ProtoMember(3)]
    public Dictionary<MyPrincipalType, User> Principals { get; set; }


    public MyPrincipal AddPrincipal(MyPrincipalType principalType, User principalValue)
    {
        this.Principals.Add(principalType, principalValue);
        return this;
    }
}


public enum MyPrincipalType
{
   Test = 0
}

以下是错误详情:

异常: {“对象引用未设置为对象的实例。”}

Inner StrackTrace : 在 ProtoBuf.Serializers.TagDecorator.get_ExpectedType() 在 ProtoBuf.Serializers.DefaultValueDecorator..ctor(TypeModel 模型,对象 defaultValue,IProtoSerializer 尾部) 在 ProtoBuf.Serializers.MapDecorator`3..ctor(TypeModel 模型,类型concreteType,IProtoSerializer keyTail,IProtoSerializer valueTail,Int32 fieldNumber,WireType wireType,WireType keyWireType,WireType valueWireType,Boolean overwriteList)

外部 StackTrace: 在 System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeConstructorInfo.Invoke(BindingFlags invokeAttr, Binder binder, Object [] 参数,CultureInfo 文化)在 System.Reflection.ConstructorInfo.Invoke(对象 [] 参数)在 ProtoBuf.Meta.ValueMember.BuildSerializer() 在 ProtoBuf.Meta.ValueMember.get_Serializer() 在 ProtoBuf.Meta.MetaType.BuildSerializer( ) 在 ProtoBuf.Meta.MetaType.get_Serializer() 在 ProtoBuf.Meta.RuntimeTypeModel.Serialize(Int32 key, Object value, ProtoWriter dest) 在 ProtoBuf.Meta.TypeModel.SerializeCore(ProtoWriter writer, Object value) 在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value, SerializationContext context) 在 ProtoBuf.Meta.TypeModel.Serialize(Stream dest, Object value) 在 ProtoBuf.Serializer.Serialize[T](Stream destination, T instance)

此问题仅适用于最新的 nuget 版本 2.3.0。当我使用 2.0.0.668 版本时,它工作正常。

【问题讨论】:

  • 尝试按照Is it possible to use Protobuf-Net with a class without a parameterless constructor? 中的说明添加无参数构造函数。
  • 感谢您的快速回复。试过但同样的错误。还有其他建议吗?
  • 您的代码无法编译,因为您没有包含User 的定义。如果我只是添加一个没有属性的类型,我会得到一个ProtoBuf.ProtoException: No parameterless constructor found for MyPrincipal,正如预期的那样:fiddle。如果我添加一个默认构造函数,也不例外:fiddle #2。请提供实际重现问题的minimal reproducible example
  • 当然。将粘贴示例以重现问题...并且 Principals 属性具有属性,但在发布问题时错过了。
  • 另外,当我将我的属性从 public Dictionary Principals 更改为 public Dictionary Principals 时,这适用于最新的 nuget 版本。有什么建议吗?

标签: protobuf-net proto protobuf-csharp-port


【解决方案1】:

这是一个库错误,从外观上看是一个令人讨厌的错误。我有logged it as an issue

有一个解决方法:暂时明确禁用字典上的“地图”处理 - 将 [ProtoMap(DisableMap = true)] 添加到 Principals。代码已修复,应该包含在 2.3.1 中。我将审查 2.3.1 的其他候选版本,并决定需要prompt 发布的内容 - 将其余部分移至 2.3.2(这样我们就可以尽快发布带有此修复程序的 2.3.1)。

【讨论】:

猜你喜欢
  • 2012-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-08
  • 1970-01-01
  • 2013-12-30
  • 1970-01-01
  • 2014-06-25
相关资源
最近更新 更多