【问题标题】:Exception: Sub-message not read correctly例外:子消息未正确读取
【发布时间】:2013-06-01 08:33:32
【问题描述】:

在使用 protobuf-net 在 c# 中反序列化一个大文件 (20GB) 期间,我得到一个 "Sub-message not read correctly" Exception。读取 2 GB 的 20 GB 后发生异常。 相同的数据结构适用于较小的实例。

20 MB 数据的序列化工作正常。

下面是一些序列化的示例代码:

            if (File.Exists(filename))
                File.Delete(filename);

            using (FileStream stream = new FileStream(filename, FileMode.Create))
            {
                Serializer.Serialize<HubLabelingData>(stream, data);
                stream.Close();
            }

下面是一些反序列化的示例代码:

            using (FileStream stream = new FileStream(filename, FileMode.Open))
            {
                data = Serializer.Deserialize<HubLabelingData>(stream);
                stream.Close();
            }

这是数据结构(适用于小型实例):

[ProtoContract]
public class HubLabelingData
{
    [ProtoMember(1)]
    public HL[] hlf;

    [ProtoMember(2)]
    public HL[] hlb;

    [ProtoMember(3)]
    public NG g;

    [ProtoMember(4)]
    public List<PL> plf;

    [ProtoMember(5)]
    public List<PL> plb;

    [ProtoMember(6)]
    public PHL[] pihlf;

    [ProtoMember(7)]
    public PHL[] pihlb;
}

[ProtoContract]
public class HL
{
    [ProtoMember(1)]
    public int[] l;

    [ProtoMember(2)]
    public double[] d;
}

[ProtoContract]
public class PL
{
    [ProtoMember(1)]
    public Dictionary<int, List<GP>> p;
}

[ProtoContract]
public class PHL
{
    [ProtoMember(1)]
    public short[] l;
}

[ProtoContract]
public class NG
{
    [ProtoMember(1)]
    public NA[] e;

    [ProtoMember(2)]
    public NA[] tne { get; set; }

    [ProtoMember(3)]
    public float[] a;

    [ProtoMember(4)]
    public float[] o;

    [ProtoMember(5)]
    public int num = 0;

}

[ProtoContract]
public class NA
{
    [ProtoMember(1)]
    public int one { get; set; }

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

    [ProtoMember(3)]
    public double tree { get; set; }

    [ProtoMember(4)]
    public int four { get; set; }

}

[ProtoContract]
public class NN
{
    [ProtoMember(1)]
    public List<NA> nas;

}

[ProtoContract]
public class GP
{
    [ProtoMember(1)]
    public float one { get; set; }

    [ProtoMember(2)]
    public float two { get; set; }

}

【问题讨论】:

  • 请贴一些示例代码
  • 您需要更多示例代码吗?
  • @ditzel 您应该显示“HubLabelingData”对象的代码。
  • 我不认为你有我可以用来重现的场景或数据文件?我可以尝试一些随机的东西来重复,但最好有一个实际的复制案例。
  • 我有一个数据文件。但它是 20 GB。问题是将其设置在下载的地方。另一个序列化器也可以解决我的问题。但是 BinaryFormatter 例如反序列化花了几个小时。

标签: c# .net exception deserialization protocol-buffers


【解决方案1】:

对于有同样问题的每个人:

Protbuf.net 不支持反序列化大于 2.048 GB 的文件(分别为对象)。

我将数据拆分为多条消息。工作量很大,但效果很好。

【讨论】:

  • 如果可以的话,我总是建议使用多条分隔消息而不是一条巨大的消息
  • 您的意思是 2,048 MB ?
【解决方案2】:

protobuf-net 从 v2.2.0 开始支持反序列化大于 2GB 的流,但有一些注意事项。

从 v2.0.0.668 迁移到 v2.3.13 为我解决了这个问题,之前我遇到了 "Sub-message not read correctly" 异常。

Release notes

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-07-28
    • 1970-01-01
    • 2011-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多