Working on a POC of transitioning from BinaryFormater to Protobuf for Serialization and deserialization inorder to reduce the deserialization time. While trying to deserialize using the protobuf library I get the following error "Invalid wire-type; this usually means you have over-written a file without truncating or setting the length" while deserializing a file in a rest web API project but the same code runs fine in another web job project with same .Net version.
protobuf-net Version: 3.1.26
.NET version: .NET framework 4.6.2
Seems to be maybe an internal package dependency version issue or issue if the deserialization happens in a w3 process.
Has anyone faced such issues with the protobuf-net package for a REST service.
Below is the code where the ProtoDeserialize function throws a exception Serializer.Deserialize<T>(stream) is called
[ProtoContract]
public class Temp
{
[ProtoMember(1)]
public string name;
[ProtoMember(2)]
public int no;
}
[HttpGet]
public HttpResponseMessage DerserializeProtoBuf()
{
try
{
var x1 = new Temp();
x1.name = "testData";
x1.no = 10;
var data1 = ProtoSerialize<Temp>(x1);
var y = ProtoDeserialize<Temp>(data1); // throws exception
}
catch
{
}
}
public static T ProtoDeserialize<T>(byte[] data) where T : class
{
if (null == data) return null;
try
{
using (var stream = new MemoryStream(data))
{
using (var decompressor = new GZipStream(stream, CompressionMode.Decompress))
{
return Serializer.Deserialize<T>(stream); // throws Invalid wire-type error here
}
}
}
catch(Exception ex)
{
throw new InvalidDataException(String.Format("Invalid data format when proto deserializing {0}", typeof(T).Name), ex);
}
}
public static byte[] ProtoSerialize<T>(T record) where T : class
{
if (null == record) return null;
try
{
using (var stream = new MemoryStream())
{
using (var gZipStream = new GZipStream(stream, CompressionMode.Compress))
{
Serializer.Serialize(gZipStream, record);
}
return stream.ToArray();
}
}
catch(Exception ex)
{
throw new InvalidDataException(String.Format("Invalid data format when proto serializing {0}", typeof(T).Name), ex);
}
}
- I have tried adding the package dependencies versions explicilty by adding bindingRedirects.
- Have tried updating and degrading the version of protobuf to 2.3.7 and other before versions
Pass
decompressor
instead ofstream
toDeserialize
. You're passing it the compressed gzip data instead of the decompressed protobuf payload.