如何修复 C# protobuf-net 中的 Invalid wire-type 问题

How to fix Invalid wire-type issue in c# protobuf-net

我正在将 protobuf-net 反序列化调用从通用调用转换为指定类型。

using var memStream = new MemoryStream(bytes);
var result = Serializer.Deserialize<T>(memStream);

using var memStream = new MemoryStream(bytes);
var result = Serializer.Deserialize(memStream, type);

我得到一个例外

ProtoBuf.ProtoException: Invalid wire-type (Fixed64); this usually means you have over-written a file without truncating or setting the length; see 
   at ProtoBuf.ProtoReader.State.ThrowProtoException(String message) in /_/src/protobuf-net.Core/ProtoReader.State.ReadMethods.cs:line 764
   at ProtoBuf.ProtoReader.State.ThrowWireTypeException() in /_/src/protobuf-net.Core/ProtoReader.State.ReadMethods.cs:line 758
   at ProtoBuf.Internal.PrimaryTypeProvider.ProtoBuf.Serializers.ISerializer<System.Type>.Read(State& state, Type value) in /_/src/protobuf-net.Core/Internal/PrimaryTypeProvider.Primitives.cs:line 292
   at ProtoBuf.ProtoReader.State.<ReadAsRoot>g__ReadFieldOne|102_0[T](State& state, SerializerFeatures features, T value, ISerializer`1 serializer) in /_/src/protobuf-net.Core/ProtoReader.State.ReadMethods.cs:line 1075
   at ProtoBuf.ProtoReader.State.ReadAsRoot[T](T value, ISerializer`1 serializer) in /_/src/protobuf-net.Core/ProtoReader.State.ReadMethods.cs:line 1059
   at ProtoBuf.ProtoReader.State.DeserializeRoot[T](T value, ISerializer`1 serializer) in /_/src/protobuf-net.Core/ProtoReader.State.ReadMethods.cs:line 1036
   at ProtoBuf.Serializer.Deserialize[T](Stream source, T value, Object userState, Int64 length) in /_/src/protobuf-net/Serializer.Deserialize.cs:line 43
   at Computer.Bus.Integration.ProtoSerializer.Deserialize(Byte[] bytes, Type type) in C:\Users\squir\source\repos\Computer.Bus\source\Computer.Bus.Integration\ProtoSerializer.cs:line 21
   at Computer.Bus.RabbitMq.Client.BusClient.<>c__DisplayClass6_0.<<Subscribe>g__innerCallback|0>d.MoveNext() in C:\Users\squir\source\repos\Computer.Bus\source\Computer.Bus.RabbitMq\Client\BusClient.cs:line 52

我寻找解决方案的所有尝试通常都与序列化错误或字节管理有关。这是一个简单的应用程序。我正在尝试(反)序列化在同一源中声明的简单类型。

[ProtoContract]
public record ProtoModel
{
    [ProtoMember(1)] 
    public double fNumber { get; init; } = DateTime.Now.ToBinary();

    [ProtoMember(2)] 
    public string someString { get; init; } = "something";
    [ProtoMember(3)]
    public DateTime Timestamp { get; init; } = DateTime.Now;
}

在我做了一些挖掘之后,我发现我把参数放在了错误的顺序。反序列化应该是这样的

using var memStream = new MemoryStream(bytes);
var result = Serializer.Deserialize(type, memStream);