1

I have just started working with ProtoBuf-Net and have the following objects:

[DataContract]
public class Statistics
{
    [DataMember(Order = 1)]
    public DateTime DateTimeAsUtc { get; set; }

    [DataMember(Order = 2)]
    public IEnumerable<StatisticsRow> TopHashTags { get; set; }

    [DataMember(Order = 3)]
    public IEnumerable<StatisticsRow> TopWords { get; set; }    
}

[DataContract]
public class StatisticsRow
{
    [DataMember(Order = 1)]
    public string Key { get; set; }

    [DataMember(Order = 2)]
    public int Count { get; set; }
}


// Serialize then DeSerialize
using (var stream = new MemoryStream())
{
    Serializer.Serialize(stream, stats);

    stream.Seek(0, SeekOrigin.Begin);
    var deserialized = Serializer.Deserialize<Statistics>(stream);
}

When I serialize and try deserializing the object I get the default value for DateTimeAsUtc and null for all the other properties. Any ideas on what I am doing wrong?

Note that I have tried replacing DataContract and DataMember with ProtoContract and ProtoMember to no avail.

The issue only happens when in Release mode.

[Update]
The issue turned out to be due to existence of [MyConsoleApp].vshost.exe which apparently is a special version of the executable to aid debugging which I thought would be re-generated after a Rebuild (clearly not) so the solution was to manually delete it and now everything works just fine :-)

I am going to accept Marc's answer anyway as he was kind enough to follow it up and was very quick to reply.

3
  • The problem is not the DateTime, if I remove the DateTime I still get null for the other properties. Commented Sep 1, 2014 at 0:30
  • What is the input data? So I can repro? Commented Sep 1, 2014 at 6:24
  • Hi Marc, tnx for following this up, I will update the q later tonight (London time) as I will be at work most of the day. Awesome library BTW. Commented Sep 1, 2014 at 7:30

1 Answer 1

3

Using your example as a base, this works fine for me:

using (var stream = new MemoryStream()) {
    var stats = new Statistics {
        DateTimeAsUtc = DateTime.UtcNow,
        TopWords = new List<StatisticsRow> {
            new StatisticsRow { Count = 1, Key = "abc" }
        },
        TopHashTags = new List<StatisticsRow> {
            new StatisticsRow { Count = 2, Key = "def" }
        }
    };
    Serializer.Serialize(stream, stats);

    stream.Seek(0, SeekOrigin.Begin);
    var deserialized = Serializer.Deserialize<Statistics>(stream);
    Console.WriteLine(deserialized.DateTimeAsUtc);
    foreach(var row in deserialized.TopWords)
        Console.WriteLine("{0}: {1}", row.Key, row.Count);
    foreach (var row in deserialized.TopHashTags)
        Console.WriteLine("{0}: {1}", row.Key, row.Count);
}

So... it probably needs a complete (failing) example to be answerable. The first thing to check, however, is stream.Length; if that is 0, there was no data serialized. As an aside and for your convenience: that implementation is akin to:

var deserialized = Serializer.DeepClone(stats);
Sign up to request clarification or add additional context in comments.

1 Comment

This is bizarre! the issue only happens when in Release mode AND only in the specific project I am working on, the exact same code on a different project e.g unit test works just fine using the same nuget package for ProtoBuf. Furthermore, DeepClone works just fine and stream.Length is also greater than 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.