title: Protobuf-net with serialized byte[] member
body: <p>I have a transport application which is used in asa a pub-/sub server to relay data between clients. The pub-/sub server need only know a little about each piece of data, for example it needs the topicname to be able to relay a published topic to the correct subscribers.</p>
  
  <p>To achieve this I have thought out a scheme in which a class that is decorated as a ProtoContract includes a byte[] which in turn contains protobuf-net serialized data. This way the server does only have to deserialize a very small part of the data it relays (and does not need to know types). My type looks like this;</p>
  
  <pre><code>[ProtoContract]
  public class DataFrame
  {
      /// &lt;summary&gt;
      /// Time the data was issued or sampled. In UTC time.
      /// &lt;/summary&gt;
      [ProtoMember(1)]
      public DateTime TimeStamp = DateTime.UtcNow;
  
      /// &lt;summary&gt;
      /// The topic associated with the data
      /// &lt;/summary&gt;
      [ProtoMember(2)]
      public string TopicName = string.Empty;
  
      /// &lt;summary&gt;
      /// Command, can be either Discover, Subscribe, Unsubscribe or Publish
      /// &lt;/summary&gt;
      [ProtoMember(3)]
      public string Command = string.Empty;
  
      /// &lt;summary&gt;
      /// The fully qualified type name of the content
      /// &lt;/summary&gt;
      [ProtoMember(4)]
      private string _typeName = string.Empty;
  
      /// &lt;summary&gt;
      /// Serialized content data (if any)
      /// &lt;/summary&gt;
      [ProtoMember(5)]
      private byte[] _content;
  
      /// &lt;summary&gt;
      /// The fully qualified type name of the content
      /// &lt;/summary&gt;
      public string TypeName
      {
          get
          {
              return _typeName;
          }
      }
  
      /// &lt;summary&gt;
      /// Get the content of this DataFrame
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;Type of the content&lt;/typeparam&gt;
      /// &lt;returns&gt;The content&lt;/returns&gt;
      public T GetContent&lt;T&gt;()
      {
          MemoryStream ms = new MemoryStream(_content);
          return Serializer.DeserializeWithLengthPrefix&lt;T&gt;(ms, PrefixStyle.Base128);
      }
  
      /// &lt;summary&gt;
      /// Set the content for this DataFrame
      /// &lt;/summary&gt;
      /// &lt;param name="value"&gt;The content to set, must be serializable and decorated as a protobuf contract type&lt;/param&gt;
      public void SetContent&lt;T&gt;(T value)
      {
          MemoryStream ms = new MemoryStream();
          Serializer.SerializeWithLengthPrefix(ms, value, PrefixStyle.Base128);
          _content = ms.GetBuffer();
          _typeName = value.GetType().AssemblyQualifiedName;
      }
  
      /// &lt;summary&gt;
      /// Encode the frame to a serialized byte array suitable for tranmission over a network
      /// &lt;/summary&gt;
      /// &lt;returns&gt;The encoded byte[]&lt;/returns&gt;
      public byte[] Encode()
      {
          DataFrame frame = (DataFrame)this;
          MemoryStream ms = new MemoryStream();
          Serializer.SerializeWithLengthPrefix(ms, frame, PrefixStyle.Base128);
          return ms.GetBuffer();
      }
  
      /// &lt;summary&gt;
      /// Factory function to create a frame from a byte array that has been received
      /// &lt;/summary&gt;
      /// &lt;param name="buffer"&gt;The serialized data to decode&lt;/param&gt;
      /// &lt;returns&gt;A new dataframe decoded from the byte[]&lt;/returns&gt;
      public static DataFrame Decode(byte[] buffer)
      {
          MemoryStream ms = new MemoryStream(buffer);
          DataFrame frame = Serializer.DeserializeWithLengthPrefix&lt;DataFrame&gt;(ms, PrefixStyle.Base128);
          frame._timeStamp = DateTime.SpecifyKind(frame._timeStamp, DateTimeKind.Utc);
          return frame;
      }
  }
  </code></pre>
  
  <p>Problem is, that I am able to deserialized a DataFrame, but when deserializing the payload byte[] I get protobuf exceptions. That is, this works (server is a UdpClient);</p>
  
  <pre><code>data = server.Receive(ref remoteEP);
  DataFrame frame = DataFrame.Decode(data);
  </code></pre>
  
  <p>But this will give me a protobuf exception, even if the content is a string;</p>
  
  <pre><code>string content = frame.GetContent&lt;string&gt;();
  </code></pre>
  
  <p>Does anyone have any pointers on what I am doing wrong?</p>