Export (0) Print
Expand All

BinaryMessageFormatter Class

Serializes or deserializes an object, or an entire graph of connected objects, to or from the body of a Message Queuing message, using a binary format.

System::Object
  System.Messaging::BinaryMessageFormatter

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

public ref class BinaryMessageFormatter : IMessageFormatter, 
	ICloneable

The BinaryMessageFormatter type exposes the following members.

  NameDescription
Public methodBinaryMessageFormatter()Initializes a new instance of the BinaryMessageFormatter class without specifying a type style or top object assembly style.
Public methodBinaryMessageFormatter(FormatterAssemblyStyle, FormatterTypeStyle)Initializes a new instance of the BinaryMessageFormatter class, specifying the formats of the root object and the type descriptions.
Top

  NameDescription
Public propertyTopObjectFormatGets or sets a value that defines how the top (root) object of a graph is deserialized with regards to finding and loading its assembly.
Public propertyTypeFormatGets or sets a value that defines how type descriptions are laid out in the serialized stream.
Top

  NameDescription
Public methodCanReadDetermines whether the formatter can deserialize the contents of the message.
Public methodCloneCreates an instance of the BinaryMessageFormatter class whose read/write properties (the root object and type description formats) are the same as the current BinaryMessageFormatter.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodReadReads the contents from the given message and creates an object that contains the deserialized message.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Public methodWriteSerializes an object into the body of the message.
Top

The BinaryMessageFormatter is very efficient and can be used to serialize most objects. The result is very compact and fast to parse, but does not allow for loosely coupled messaging as the XmlMessageFormatter does. Loosely coupled means that the client and the server can independently version the type that is sent and received.

When the application sends a message to the queue using an instance of the MessageQueue class, the formatter serializes the object into a stream and inserts it into the message body. When reading from a queue using a MessageQueue, the formatter deserializes the message data into the Body property of a Message.

BinaryMessageFormatter provides faster throughput than the XmlMessageFormatter. Use the BinaryMessageFormatter when pure speed rather than loosely coupled messaging is desired.

#using <system.dll>
#using <system.messaging.dll>
#using <system.drawing.dll>

using namespace System;
using namespace System::Messaging;
using namespace System::Drawing;
using namespace System::IO;

/// <summary> 
/// Provides a container class for the example. 
/// </summary> 
ref class MyNewQueue
{
public:

   //************************************************* 
   // Creates a new queue. 
   //************************************************* 
   static void CreateQueue( String^ queuePath )
   {
      try
      {
         if (  !MessageQueue::Exists( queuePath ) )
         {
            MessageQueue::Create( queuePath );
         }
         else
         {
            Console::WriteLine(  "{0} already exists.", queuePath );
         }
      }
      catch ( MessageQueueException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

   }


   //************************************************* 
   // Sends an image to a queue, using the BinaryMessageFormatter. 
   //************************************************* 
   void SendMessage()
   {
      try
      {

         // Create a new bitmap. 
         // The file must be in the \bin\debug or \bin\retail folder, or 
         // you must give a full path to its location.
         Image^ myImage = Bitmap::FromFile( "SentImage::bmp" );

         // Connect to a queue on the local computer.
         MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
         Message^ myMessage = gcnew Message( myImage,gcnew BinaryMessageFormatter );

         // Send the image to the queue.
         myQueue->Send( myMessage );
      }
      catch ( ArgumentException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

      return;
   }


   //************************************************* 
   // Receives a message containing an image. 
   //************************************************* 
   void ReceiveMessage()
   {
      try
      {

         // Connect to the a queue on the local computer.
         MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

         // Set the formatter to indicate body contains an Order.
         myQueue->Formatter = gcnew BinaryMessageFormatter;

         // Receive and format the message. 
         Message^ myMessage = myQueue->Receive();
         Bitmap^ myImage = static_cast<Bitmap^>(myMessage->Body);

         // This will be saved in the \bin\debug or \bin\retail folder.
         myImage->Save( "ReceivedImage::bmp", System::Drawing::Imaging::ImageFormat::Bmp );
      }
      catch ( MessageQueueException^ ) 
      {

         // Handle Message Queuing exceptions.
      }
      // Handle invalid serialization format. 
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }
      catch ( IOException^ e ) 
      {

         // Handle file access exceptions.
      }


      // Catch other exceptions as necessary. 
      return;
   }

};


//************************************************* 
// Provides an entry point into the application. 
//          
// This example sends and receives a message from 
// a queue. 
//************************************************* 
int main()
{

   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

   // Create a queue on the local computer.
   MyNewQueue::CreateQueue( ".\\myQueue" );

   // Send a message to a queue.
   myNewQueue->SendMessage();

   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   return 0;
}

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft