Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

Message Constructor (Object, IMessageFormatter)

Initializes a new instance of the Message class using the specified formatter to serialize the specified object into the body of the message.

Namespace:  System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)
public Message(
	Object body,
	IMessageFormatter formatter
)

Parameters

body
Type: System.Object

The object to be serialized into the body of the message.

formatter
Type: System.Messaging.IMessageFormatter

A IMessageFormatter that specifies the formatter with which to serialize the message body.

Use this overload to create a new instance of the Message class that contains the Body specified by the body parameter and that uses any valid formatter to serialize the body. The body parameter is any object that can be serialized, such as a text string, a structure object, a class instance, or an embedded object. If you change the Body or Formatter property at any time before calling Send, the message will be serialized according to the new property value.

The XmlMessageFormatter is loosely coupled, so it is not necessary to have the same object type on the sender and receiver when using this format. The ActiveXMessageFormatter and BinaryMessageFormatter serialize the data into binary representation. The ActiveXMessageFormatter is used when sending or receiving COM components.

The following table shows initial property values for an instance of Message.

Property

Initial value

AcknowledgeType

AcknowledgeType.None

AdministrationQueue

null

AppSpecific

0

AttachSenderId

true

AuthenticationProviderName

Microsoft Base Cryptographic Provider version 1.0

AuthenticationProviderType

CryptoProviderType.RSA_FULL

Body

The body parameter.

BodyStream

Stream.null

BodyType

0

ConnectorType

Guid.Empty

CorrelationId

An empty string ("")

DestinationSymmetricKey

A zero-length array of bytes

DigitalSignature

A zero-length array of bytes

EncryptionAlgorithm

EncryptionAlgorithm.RC2

Extension

A zero-length array of bytes

Formatter

The formatter parameter.

HashAlgorithm

HashAlgorithm.MD5

Label

An empty string ("")

Priority

MessagePriority.Normal

Recoverable

false

ResponseQueue

null

SenderCertificate

A zero-length array of bytes

TimeToBeReceived

Message.InfiniteTimeout

TimeToReachQueue

Message.InfiniteTimeout

TransactionStatusQueue

null

UseAuthentication

false

UseDeadLetterQueue

false

UseEncryption

false

UseJournalQueue

false

UseTracing

false

using System;
using System.Messaging;
using System.Drawing;
using System.IO;

namespace MyProject
{
	

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

		//************************************************** 
		// Provides an entry point into the application. 
		//		  
		// This example sends and receives a message from 
		// a queue. 
		//************************************************** 

		public static void Main()
		{
			// Create a new instance of the class.
			MyNewQueue myNewQueue = new MyNewQueue();

			// Create a queue on the local computer.
			CreateQueue(".\\myQueue");
			
			// Send a message to a queue.
			myNewQueue.SendMessage();

			// Receive a message from a queue.
			myNewQueue.ReceiveMessage();

			return;
		}

		//************************************************** 
		// Creates a new queue. 
		//************************************************** 

		public static void CreateQueue(string queuePath)
		{
			try	
				{
				if(!MessageQueue.Exists(queuePath))
				{
					MessageQueue.Create(queuePath);
				}
				else
				{
					Console.WriteLine(queuePath + " already exists.");
				}
			}
			catch (MessageQueueException e)
			{
				Console.WriteLine(e.Message);
			}
			
		}

		//************************************************** 
		// Sends an image to a queue, using the BinaryMessageFormatter. 
		//************************************************** 
		
		public 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 = new MessageQueue(".\\myQueue");
				
				Message myMessage = new Message(myImage, new BinaryMessageFormatter());

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

			return;
		}


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

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

				// Set the formatter to indicate body contains an Order.
				myQueue.Formatter = new BinaryMessageFormatter();

				// Receive and format the message. 
				System.Messaging.Message myMessage = myQueue.Receive(); 
				Bitmap myImage = (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;
		}
	}
}
#using <mscorlib.dll>
#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>
__gc class MyNewQueue 
{
	//*************************************************
	// Creates a new queue.
	//*************************************************
public:
	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.
	//*************************************************
public:
	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(S"SentImage::bmp");

			// Connect to a queue on the local computer.
			MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

			Message* myMessage = new Message(myImage, new BinaryMessageFormatter());

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

		return;
	}

	//*************************************************
	// Receives a message containing an image.
	//*************************************************
public:
	void ReceiveMessage() 
	{
		try 
		{
			// Connect to the a queue on the local computer.
			MessageQueue* myQueue = new MessageQueue(S".\\myQueue");

			// Set the formatter to indicate body contains an Order.
			myQueue->Formatter = new 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(S"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 = new MyNewQueue();

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

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

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

	return 0;
}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC

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

.NET Framework

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

.NET Compact Framework

Supported in: 3.5, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.