Deserializzazione sicuro dopo il passaggio PrefixStyle a Fixed32
-
16-09-2019 - |
Domanda
Ho una suite di test per una libreria di comunicazione che sto sviluppando con protobuf-net
che esegue bene. Tutti i test passano. Ma se cambio il PrefixStyle da Base128 al Fixed32, la deserializzazione sicuro.
L'eccezione che ricevo dalla funzione TryDeserializeWithLengthPrefix
è:
System.ArgumentNullException was caught
Message="Value cannot be null.\r\nParameter name: type"
Source="protobuf-net"
ParamName="type"
Tutto funziona solo se mi tengo la semplice PrefixStyle.Base128 durante la serializzazione e deserializzazione il messaggio.
Qualcuno sa cosa può accadere?
Soluzione
Hohum, Yup si presenta come un insetto ( ora registrati ); esempio ripetibile sotto. Vedrò se riesco a risolvere il problema sul treno (a breve). Sorry 'bout che:
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);
}
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow