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 176891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T13:57:28+00:00 2026-05-11T13:57:28+00:00

I have 2 networked apps that should send serialized protobuf-net messages to each other.

  • 0

I have 2 networked apps that should send serialized protobuf-net messages to each other. I can serialize the objects and send them, however, I cannot figure out how to deserialize the received bytes.

I tried to deserialize with this and it failed with a NullReferenceException.

// Where 'ms' is a memorystream containing the serialized // byte array from the network. Messages.BaseMessage message =   ProtoBuf.Serializer.Deserialize<Messages.BaseMessage>(ms); 

I am passing a header before the serialized bytes that contains message type ID, which I can use in a giant switch statement to return the expected sublcass Type. With the block below, I receive the error: System.Reflection.TargetInvocationException —> System.NullReferenceException.

//Where 'ms' is a memorystream and 'messageType' is a //Uint16. Type t = Messages.Helper.GetMessageType(messageType); System.Reflection.MethodInfo method =   typeof(ProtoBuf.Serializer).GetMethod('Deserialize').MakeGenericMethod(t); message = method.Invoke(null, new object[] { ms }) as Messages.BaseMessage; 

Here’s the function I use to send a message over the network:

internal void Send(Messages.BaseMessage message){   using (System.IO.MemoryStream ms = new System.IO.MemoryStream()){     ProtoBuf.Serializer.Serialize(ms, message);     byte[] messageTypeAndLength = new byte[4];     Buffer.BlockCopy(BitConverter.GetBytes(message.messageType), 0, messageTypeAndLength, 0, 2);     Buffer.BlockCopy(BitConverter.GetBytes((UInt16)ms.Length), 0, messageTypeAndLength, 2, 2);     this.networkStream.Write(messageTypeAndLength);     this.networkStream.Write(ms.ToArray());   } } 

This the class, with base class, I’m serializing:

[Serializable, ProtoContract, ProtoInclude(50, typeof(BeginRequest))] abstract internal class BaseMessage {   [ProtoMember(1)]   abstract public UInt16 messageType { get; } }  [Serializable, ProtoContract] internal class BeginRequest : BaseMessage {     [ProtoMember(1)]     public override UInt16 messageType     {         get { return 1; }     } } 

Fixed using Marc Gravell’s suggestion. I removed the ProtoMember attribute from the readonly properties. Also switched to using SerializeWithLengthPrefix. Here’s what I have now:

[Serializable, ProtoContract, ProtoInclude(50, typeof(BeginRequest))] abstract internal class BaseMessage {   abstract public UInt16 messageType { get; } }  [Serializable, ProtoContract] internal class BeginRequest : BaseMessage {     public override UInt16 messageType     {         get { return 1; }     } } 

To receive an object:

//where 'this.Ssl' is an SslStream. BaseMessage message =   ProtoBuf.Serializer.DeserializeWithLengthPrefix<BaseMessage>(     this.Ssl, ProtoBuf.PrefixStyle.Base128); 

To send an object:

//where 'this.Ssl' is an SslStream and 'message' can be anything that // inherits from BaseMessage. ProtoBuf.Serializer.SerializeWithLengthPrefix<BaseMessage>(   this.Ssl, message, ProtoBuf.PrefixStyle.Base128); 
  • 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. 2026-05-11T13:57:28+00:00Added an answer on May 11, 2026 at 1:57 pm

    First; for network usage, there is SerializeWithLengthPrefix and DeserializeWithLengthPrefix which handle length for you (optionally with a tag). The MakeGenericMethod looks OK at first glance; and this actually ties in very closely to the pending commit of the work I’ve been doing to implement an RPC stack: the pending code has an override of DeserializeWithLengthPrefix that takes (essentially) a Func<int,Type>, to resolve a tag to a type to make it easier to deserialize unexpected data on the fly.

    If the message type actually relates to the inheritance between BaseMessage and BeginRequest, then you don’t need this; it always goes to the top-most contract type in the hierarchy and works its way down (due to some wire details).

    Also – I haven’t had chance to test it, but the following might be upsetting it:

    [ProtoMember(1)] public override UInt16 messageType {     get { return 1; } } 

    It is marked for serialization, but has no mechanism for setting the value. Maybe this is the issue? Try removing the [ProtoMember] here, since I don’t this is useful – it is (as far as serialization is concerned), largely a duplicate of the [ProtoInclude(...)] marker.

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

Sidebar

Related Questions

I have two apps that are in the app store with which a couple
We have cloud-hosted (RackSpace cloud) Ruby and Java apps that will interact as follows:
I have multiple app processes that each connect to servers and receive data from
I know that for native apps, after I download them, I can run them
I have just started including networking functionality into my apps. I know that you
I have a simple client server apps that uses WCF (netTcpBinding) when i'm launching
We have a small .NET web application (aspx & ascx) that is used inside
Bckground I have a networked application written in C#. my server program has a
My current class is planning on creating a basic networked game, but we have
I have a network C++ program in Windows that I'd like to test for

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.