Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6665599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:44:24+00:00 2026-05-26T02:44:24+00:00

I’m having issues deserializing a Guid when the Guid is defined in a message

  • 0

I’m having issues deserializing a Guid when the Guid is defined in a message base class. When I have the following message.

 [DataContract]
public abstract class GatewayPageEvent:IDomainEvent
{
    protected GatewayPageEvent()
    {
        On = DateTime.Now;
    }
    [DataMember(Order = 1)] public Guid GatewayPageId { get; set; }
    [DataMember(Order = 2)] public DateTime On { get; set; }
}
[DataContract]
public class GatewayPageAddedToSite : GatewayPageEvent
{
    [DataMember(Order = 3)]public string Url { get; set; }
    [DataMember(Order = 4)]public string SiteCode { get; set; }
}

GatewayPageId is always deserializing as Guid.Empty. Here’s a unit test I wrote that fails. I’m kinda stumped…

    public string Serialize(object t)
    {
        var memoryStream = new MemoryStream();
        ProtoBuf.Serializer.Serialize(memoryStream, t);
        return Convert.ToBase64String(memoryStream.ToArray());
    }

    public object Deserialize(string value, Type targetType)
    {
        var bytes = Convert.FromBase64String(value);
        var stream = new MemoryStream(bytes);
        return ProtoBuf.Serializer.NonGeneric.Deserialize(targetType, stream);
    }
    [Test]
    public void protobuf_serialization_can_deserialized_guids()
    {
        var originalMessage = new GatewayPageAddedToSite
                                  {GatewayPageId = Guid.NewGuid(), SiteCode = "dls", Url = "test"};
        var serializedMessage = Serialize(originalMessage);
        var @event = (GatewayPageAddedToSite)Deserialize(serializedMessage, typeof(GatewayPageAddedToSite));
        Assert.AreEqual(@event.GatewayPageId, originalMessage.GatewayPageId);
    }

I know Guids work fine because the following unit test works just fine.

[Test]
    public void guids_work_fine()
    {
        var original = Guid.NewGuid();
        var serialized = Serialize(original);
        var deserialized = (Guid) Deserialize(serialized, typeof (Guid));
        Assert.AreEqual(original,deserialized);
    }

Am I defining my contract wrong? Or is this a problem with protobuf-net?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:44:25+00:00Added an answer on May 26, 2026 at 2:44 am

    With protobuf-net, it needs to know about inheritance ahead of time (actually, so does DataContractSerializer, usually – via [KnownType(...)]). In the case of protobuf-net, it also needs a unique key to distinguish them, something like:

    [DataContract, ProtoInclude(3, typeof(GatewayPageAddedToSite))]
    public abstract class GatewayPageEvent:IDomainEvent
    {...}
    [DataContract]
    public class GatewayPageAddedToSite : GatewayPageEvent
    {...}
    

    (i.e. the base-type needs to be told about the derived types)

    Note that the numbers only need to be unique within each type, so there is no collision between the “3” in the include vs the “3” in GatewayPageAddedToSite – and actually, GatewayPageAddedToSite could use “1” and “2” if it wants.

    If your domain model is generated, then a separate partial class file might help:

    [ProtoInclude(3, typeof(GatewayPageAddedToSite)]
    partial class GatewayPageEvent {}
    

    If you really don’t want to upset your model with these things, then in v2 you can do this at runtime instead:

    RuntimeTypeModel.Default[typeof(GatewayPageEvent)]
          .AddSubType(3, typeof(GatewayPageAddedToSite));
    

    any of that make it work?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.