title: MSMQ + C#, receiving a message with encoded byte[] body behaves differently on Windows 7 vs. Windows XP
body: <p>I have an application that displays the contents of messages in an MSMQ message queue.  There is a problem with MSMQ on Windows 7 not preserving the true object type of the data in the message body. In the example, I send a byte[], and then later when I receive it, it's no longer a byte array, but the wrapped XML container document. In Windows XP I have never had this problem, and the Message.Body property has always correctly been set to a byte[].</p>
  
  <p>Here is the compression code:</p>
  
  <pre><code>    public void SendWithCompression(string Message)
      {
          try
          {
              // Compress the message data in memory.
              byte[] UncompressedData = Encoding.UTF8.GetBytes(Message);
              byte[] CompressedData = null;
  
              MemoryStream s = new MemoryStream();
              using (GZipStream z = new GZipStream(s, CompressionMode.Compress))
              {
                  z.Write(UncompressedData, 0, UncompressedData.Length);
              }
              CompressedData = s.ToArray();
  
              if (_Transaction != null)
              {
                  _Transaction.Begin();
                  base.Send(CompressedData, _Transaction);
                  _Transaction.Commit();
              }
              else
              {
                  base.Send(CompressedData);
              }
          }
          catch (Exception ex)
          {
              Console.WriteLine(ex.Message);
          }
      }
  </code></pre>
  
  <hr>
  
  <p>Here is the message contents for a test message.  It ends up as an XML document that wraps the encoded binary data.:
      <code>&lt;?xml version="1.0"?&gt;..&lt;base64Binary&gt;H4sIAAAAAAAEAO29B2AcSZYlJi9tynt/SvVK1+B0oQiAYBMk2JBAEOzBiM3mkuwdaUcjKasqgcplVmVdZhZAzO2dvPfee++999577733ujudTif33/8/XGZkAWz2zkrayZ4hgKrIHz9+fB8/Ih63edMefTsvy2rv8V3+4/8ByygBlxMAAAA=&lt;/base64Binary&gt;</code></p>
  
  <hr>
  
  <p>Here is the decompression code it uses:</p>
  
  <pre><code>        String Data;
          //Get message and format XML
          System.Messaging.Message m = MessagesList[ListIndex].Tag;
          m.Formatter = new XmlMessageFormatter(new Type[] 
          {
              typeof(string), // Send raw plain strings
              typeof(byte[]), // Array of binary data
              typeof(XmlNode) // Xml document
          });
  
          m.BodyStream.Position = 0;
  
          Data = m.Body.ToString();
              if (m.Body.GetType() == typeof(byte[]))
              {
                  try
                  {
                      // The message body is an array of binary data.
                      // Assume it is a GZIP stream of compressed data.
                      byte[] CompressedData = (byte[])m.Body;
                      byte[] UncompressedData = null;
  
                      // Decompress it.
                      MemoryStream s = new MemoryStream(CompressedData);
                      using (GZipStream z = new GZipStream(s, CompressionMode.Decompress))
                      {
                          UncompressedData = MyExtensions.ReadRemainingBytes(z);
                      }
  
                      // Turn the bytes back into a string.
                      Data = Encoding.UTF8.GetString(UncompressedData);
                  }
                  catch
                  {
                      Data = "Unknown binary data: " +
                      BitConverter.ToString((byte[])m.Body, 0);
                  }
              }
              if (m.Body.GetType() == typeof(XmlElement))
              {
                  XmlElement el = (XmlElement)m.Body;
                  if (el.Name == "string")
                      Data = el.InnerText;
                  else
                      Data = el.OuterXml;
              }
  </code></pre>
  
  <hr>
  
  <p>I would like to point out that I am setting the Formatter of the message, which is the first step to getting the body to automatically "box" and "unbox" in the queue.</p>
  
  <p>In Windows XP, m.Body.GetType() == byte[], like expected. But, in Windows 7, m.Body.GetType() == XmlElement, i.e. the wrapper XML.  It no longer "unboxes" the message.</p>
  
  <p>Do we need to do something differently? We have worked around it once for sending strings, as you can see at the end of the receive function, but I would like to find a real answer for why this code behaves differently on Windows 7.</p>