将 PrefixStyle 切换为 Fix32 后反序列化失败

发布于 2024-08-05 07:57:13 字数 417 浏览 10 评论 0原文

我有一个正在使用 protobuf-net 开发的通信库的测试套件,运行正常。所有测试均通过。但如果我将 PrefixStyle 从 Base128 更改为 Fix32,反序列化会失败。

我从 TryDeserializeWithLengthPrefix 函数收到的异常是:


System.ArgumentNullException was caught
  Message="Value cannot be null.\r\nParameter name: type"
  Source="protobuf-net"
  ParamName="type"

如果我在序列化和反序列化消息时简单地保留 PrefixStyle.Base128 ,那么一切都会正常工作。

有谁知道可能会发生什么?

I have a test suite for a communication library I'm developing using protobuf-net that is running okay. All tests pass. But if I change the PrefixStyle from Base128 to Fixed32, the deserialization fail.

The exception I receive from the TryDeserializeWithLengthPrefix function is:


System.ArgumentNullException was caught
  Message="Value cannot be null.\r\nParameter name: type"
  Source="protobuf-net"
  ParamName="type"

Everything just works if I simple keep the PrefixStyle.Base128 when serializing and deserializing the message.

Does anyone know what may be happening?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无言温柔 2024-08-12 07:57:13

咳咳,是的,看起来像是一个错误(现已记录 );下面的可重复示例。我会看看能否在火车上修好它(很快)。抱歉:

using System;
using System.IO;
using ProtoBuf;
[ProtoContract]
public class Strange // test entity
{
    [ProtoMember(1)]
    public string Foo { get; set; } // test prop
    [ProtoMember(2)]
    public int Bar { get; set; } // test prop

    static void Main() {
        var original = new Strange { Foo = "abc", Bar = 123 };
        // serialize and deserialize with base-128
        using (MemoryStream ms = new MemoryStream()) {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Base128,1);
            ms.Position = 0;
            object obj;
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Base128, i => typeof(Strange),out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Base128: " + clone.Foo); // works fine
            Console.WriteLine("Bar via Base128: " + clone.Bar);
        }
        // serialize and deserialize with fixed-32
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Fixed32,1);
            ms.Position = 0;
            object obj;
            // BOOM here; oh how embarrassing
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Fixed32, i => typeof(Strange), out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Fixed32: " + clone.Foo);
            Console.WriteLine("Bar via Fixed32: " + clone.Bar);
        }
    }
}

Hohum, yup looks like a bug (now logged); repeatable example below. I'll see if I can fix it on the train (shortly). Sorry 'bout that:

using System;
using System.IO;
using ProtoBuf;
[ProtoContract]
public class Strange // test entity
{
    [ProtoMember(1)]
    public string Foo { get; set; } // test prop
    [ProtoMember(2)]
    public int Bar { get; set; } // test prop

    static void Main() {
        var original = new Strange { Foo = "abc", Bar = 123 };
        // serialize and deserialize with base-128
        using (MemoryStream ms = new MemoryStream()) {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Base128,1);
            ms.Position = 0;
            object obj;
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Base128, i => typeof(Strange),out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Base128: " + clone.Foo); // works fine
            Console.WriteLine("Bar via Base128: " + clone.Bar);
        }
        // serialize and deserialize with fixed-32
        using (MemoryStream ms = new MemoryStream())
        {
            Serializer.SerializeWithLengthPrefix(ms, original, PrefixStyle.Fixed32,1);
            ms.Position = 0;
            object obj;
            // BOOM here; oh how embarrassing
            Serializer.NonGeneric.TryDeserializeWithLengthPrefix(ms,
                PrefixStyle.Fixed32, i => typeof(Strange), out obj);
            var clone = (Strange)obj;
            Console.WriteLine("Foo via Fixed32: " + clone.Foo);
            Console.WriteLine("Bar via Fixed32: " + clone.Bar);
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文