MessageQueue Class

 

Provides access to a queue on a Message Queuing server.

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

System.Object
  System.MarshalByRefObject
    System.ComponentModel.Component
      System.Messaging.MessageQueue

[MessagingDescriptionAttribute("MessageQueueDesc")]
public class MessageQueue : Component, IEnumerable

NameDescription
System_CAPS_pubmethodMessageQueue()

Initializes a new instance of the MessageQueue class. After the default constructor initializes the new instance, you must set the instance's Path property before you can use the instance.

System_CAPS_pubmethodMessageQueue(String)

Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path.

System_CAPS_pubmethodMessageQueue(String, Boolean)

Initializes a new instance of the MessageQueue class that references the Message Queuing queue at the specified path and with the specified read-access restriction.

System_CAPS_pubmethodMessageQueue(String, Boolean, Boolean)

Initializes a new instance of the MessageQueue class.

System_CAPS_pubmethodMessageQueue(String, Boolean, Boolean, QueueAccessMode)

Initializes a new instance of the MessageQueue class.

System_CAPS_pubmethodMessageQueue(String, QueueAccessMode)

Initializes a new instance of the MessageQueue class.

NameDescription
System_CAPS_pubpropertyAccessMode

Gets a value that indicates the access mode for the queue.

System_CAPS_pubpropertyAuthenticate

Gets or sets a value that indicates whether the queue accepts only authenticated messages.

System_CAPS_pubpropertyBasePriority

Gets or sets the base priority Message Queuing uses to route a public queue's messages over the network.

System_CAPS_protpropertyCanRaiseEvents

Gets a value indicating whether the component can raise an event.(Inherited from Component.)

System_CAPS_pubpropertyCanRead

Gets a value that indicates whether the MessageQueue can be read.

System_CAPS_pubpropertyCanWrite

Gets a value that indicates whether the MessageQueue can be written to.

System_CAPS_pubpropertyCategory

Gets or sets the queue category.

System_CAPS_pubpropertyContainer

Gets the IContainer that contains the Component.(Inherited from Component.)

System_CAPS_pubpropertyCreateTime

Gets the time and date that the queue was created in Message Queuing.

System_CAPS_pubpropertyDefaultPropertiesToSend

Gets or sets the message property values to be used by default when the application sends messages to the queue.

System_CAPS_pubpropertyDenySharedReceive

Gets or sets a value that indicates whether this MessageQueue has exclusive access to receive messages from the Message Queuing queue.

System_CAPS_protpropertyDesignMode

Gets a value that indicates whether the Component is currently in design mode.(Inherited from Component.)

System_CAPS_pubpropertySystem_CAPS_staticEnableConnectionCache

Gets or sets a value that indicates whether a cache of connections will be maintained by the application.

System_CAPS_pubpropertyEncryptionRequired

Gets or sets a value that indicates whether the queue accepts only non-private (non-encrypted) messages.

System_CAPS_protpropertyEvents

Gets the list of event handlers that are attached to this Component.(Inherited from Component.)

System_CAPS_pubpropertyFormatName

Gets the unique queue name that Message Queuing generated at the time of the queue's creation.

System_CAPS_pubpropertyFormatter

Gets or sets the formatter used to serialize an object into or deserialize an object from the body of a message read from or written to the queue.

System_CAPS_pubpropertyId

Gets the unique Message Queuing identifier of the queue.

System_CAPS_pubpropertyLabel

Gets or sets the queue description.

System_CAPS_pubpropertyLastModifyTime

Gets the last time the properties of a queue were modified.

System_CAPS_pubpropertyMachineName

Gets or sets the name of the computer where the Message Queuing queue is located.

System_CAPS_pubpropertyMaximumJournalSize

Gets or sets the maximum size of the journal queue.

System_CAPS_pubpropertyMaximumQueueSize

Gets or sets the maximum size of the queue.

System_CAPS_pubpropertyMessageReadPropertyFilter

Gets or sets the property filter for receiving or peeking messages.

System_CAPS_pubpropertyMulticastAddress

Introduced in MSMQ 3.0. Gets or sets the multicast address associated with the queue.

System_CAPS_pubpropertyPath

Gets or sets the queue's path. Setting the Path causes the MessageQueue to point to a new queue.

System_CAPS_pubpropertyQueueName

Gets or sets the friendly name that identifies the queue.

System_CAPS_pubpropertyReadHandle

Gets the native handle used to read messages from the message queue.

System_CAPS_pubpropertySite

Gets or sets the ISite of the Component.(Inherited from Component.)

System_CAPS_pubpropertySynchronizingObject

Gets or sets the object that marshals the event-handler call resulting from a ReceiveCompleted or PeekCompleted event.

System_CAPS_pubpropertyTransactional

Gets a value that indicates whether the queue accepts only transactions.

System_CAPS_pubpropertyUseJournalQueue

Gets or sets a value that indicates whether received messages are copied to the journal queue.

System_CAPS_pubpropertyWriteHandle

Gets the native handle used to send messages to the message queue.

NameDescription
System_CAPS_pubmethodBeginPeek()

Initiates an asynchronous peek operation that has no time-out. The operation is not complete until a message becomes available in the queue.

System_CAPS_pubmethodBeginPeek(TimeSpan)

Initiates an asynchronous peek operation that has a specified time-out. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginPeek(TimeSpan, Cursor, PeekAction, Object, AsyncCallback)

Initiates an asynchronous peek operation that has a specified time-out and that uses a specified cursor, a specified peek action, and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginPeek(TimeSpan, Object)

Initiates an asynchronous peek operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginPeek(TimeSpan, Object, AsyncCallback)

Initiates an asynchronous peek operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginReceive()

Initiates an asynchronous receive operation that has no time-out. The operation is not complete until a message becomes available in the queue.

System_CAPS_pubmethodBeginReceive(TimeSpan)

Initiates an asynchronous receive operation that has a specified time-out. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginReceive(TimeSpan, Cursor, Object, AsyncCallback)

Initiates an asynchronous receive operation that has a specified time-out and uses a specified cursor and a specified state object. The state object provides associated information throughout the lifetime of the operation. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginReceive(TimeSpan, Object)

Initiates an asynchronous receive operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodBeginReceive(TimeSpan, Object, AsyncCallback)

Initiates an asynchronous receive operation that has a specified time-out and a specified state object, which provides associated information throughout the operation's lifetime. This overload receives notification, through a callback, of the identity of the event handler for the operation. The operation is not complete until either a message becomes available in the queue or the time-out occurs.

System_CAPS_pubmethodSystem_CAPS_staticClearConnectionCache()

Clears the connection cache.

System_CAPS_pubmethodClose()

Frees all resources allocated by the MessageQueue.

System_CAPS_pubmethodSystem_CAPS_staticCreate(String)

Creates a non-transactional Message Queuing queue at the specified path.

System_CAPS_pubmethodSystem_CAPS_staticCreate(String, Boolean)

Creates a transactional or non-transactional Message Queuing queue at the specified path.

System_CAPS_pubmethodCreateCursor()

Creates a new Cursor for the current message queue.

System_CAPS_pubmethodCreateObjRef(Type)

Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodSystem_CAPS_staticDelete(String)

Deletes a queue on a Message Queuing server.

System_CAPS_pubmethodDispose()

Releases all resources used by the Component.(Inherited from Component.)

System_CAPS_protmethodDispose(Boolean)

Disposes of the resources (other than memory) used by the MessageQueue.(Overrides Component.Dispose(Boolean).)

System_CAPS_pubmethodEndPeek(IAsyncResult)

Completes the specified asynchronous peek operation.

System_CAPS_pubmethodEndReceive(IAsyncResult)

Completes the specified asynchronous receive operation.

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_pubmethodSystem_CAPS_staticExists(String)

Determines whether a Message Queuing queue exists at the specified path.

System_CAPS_protmethodFinalize()

Releases unmanaged resources and performs other cleanup operations before the Component is reclaimed by garbage collection.(Inherited from Component.)

System_CAPS_pubmethodGetAllMessages()

Returns all the messages that are in the queue.

System_CAPS_pubmethodGetEnumerator()

Obsolete.Enumerates the messages in a queue. GetEnumerator is deprecated. GetMessageEnumerator2 should be used instead.

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodSystem_CAPS_staticGetMachineId(String)

Gets the identifier of the computer on which the queue referenced by this MessageQueue is located.

System_CAPS_pubmethodGetMessageEnumerator()

Obsolete.Creates an enumerator object for all the messages in the queue. GetMessageEnumerator is deprecated. GetMessageEnumerator2 should be used instead.

System_CAPS_pubmethodGetMessageEnumerator2()

Creates an enumerator object for all the messages in the queue.

System_CAPS_pubmethodSystem_CAPS_staticGetMessageQueueEnumerator()

Provides forward-only cursor semantics to enumerate through all public queues on the network.

System_CAPS_pubmethodSystem_CAPS_staticGetMessageQueueEnumerator(MessageQueueCriteria)

Provides forward-only cursor semantics to enumerate through all public queues on the network that meet the specified criteria.

System_CAPS_pubmethodSystem_CAPS_staticGetPrivateQueuesByMachine(String)

Retrieves all the private queues on the specified computer.

System_CAPS_pubmethodSystem_CAPS_staticGetPublicQueues()

Retrieves all the public queues on the network.

System_CAPS_pubmethodSystem_CAPS_staticGetPublicQueues(MessageQueueCriteria)

Retrieves all the public queues on the network that meet the specified criteria.

System_CAPS_pubmethodSystem_CAPS_staticGetPublicQueuesByCategory(Guid)

Retrieves all the public queues on the network that belong to the specified category.

System_CAPS_pubmethodSystem_CAPS_staticGetPublicQueuesByLabel(String)

Retrieves all the public queues on the network that carry the specified label.

System_CAPS_pubmethodSystem_CAPS_staticGetPublicQueuesByMachine(String)

Retrieves all the public queues that reside on the specified computer.

System_CAPS_pubmethodSystem_CAPS_staticGetSecurityContext()

Retrieves the security context that MSMQ associates with the current user (thread identity) at the time of this call.

System_CAPS_protmethodGetService(Type)

Returns an object that represents a service provided by the Component or by its Container.(Inherited from Component.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodPeek()

Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available.

System_CAPS_pubmethodPeek(TimeSpan)

Returns without removing (peeks) the first message in the queue referenced by this MessageQueue. The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.

System_CAPS_pubmethodPeek(TimeSpan, Cursor, PeekAction)

Returns without removing (peeks) the current or next message in the queue, using the specified cursor. The Peek method is synchronous, so it blocks the current thread until a message becomes available or the specified time-out occurs.

System_CAPS_pubmethodPeekByCorrelationId(String)

Peeks the message that matches the given correlation identifier and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.

System_CAPS_pubmethodPeekByCorrelationId(String, TimeSpan)

Peeks the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

System_CAPS_pubmethodPeekById(String)

Peeks the message whose message identifier matches the id parameter.

System_CAPS_pubmethodPeekById(String, TimeSpan)

Peeks the message whose message identifier matches the id parameter. Waits until the message appears in the queue or a time-out occurs.

System_CAPS_pubmethodPeekByLookupId(Int64)

Introduced in MSMQ 3.0. Peeks at the message that matches the given lookup identifier from a non-transactional queue.

System_CAPS_pubmethodPeekByLookupId(MessageLookupAction, Int64)

Introduced in MSMQ 3.0. Peeks at a specific message from the queue. The message can be specified by a lookup identifier or by its position at the front or end of the queue.

System_CAPS_pubmethodPurge()

Deletes all the messages contained in the queue.

System_CAPS_pubmethodReceive()

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

System_CAPS_pubmethodReceive(MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

System_CAPS_pubmethodReceive(MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and blocks the current thread of execution until a message is available.

System_CAPS_pubmethodReceive(TimeSpan)

Receives the first message available in the queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceive(TimeSpan, Cursor)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

System_CAPS_pubmethodReceive(TimeSpan, Cursor, MessageQueueTransaction)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

System_CAPS_pubmethodReceive(TimeSpan, Cursor, MessageQueueTransactionType)

Receives the current message in the queue, using a specified cursor. If no message is available, this method waits until either a message is available, or the time-out expires.

System_CAPS_pubmethodReceive(TimeSpan, MessageQueueTransaction)

Receives the first message available in the transactional queue referenced by the MessageQueue and waits until either a message is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceive(TimeSpan, MessageQueueTransactionType)

Receives the first message available in the queue referenced by the MessageQueue. This call is synchronous, and waits until either a message is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceiveByCorrelationId(String)

Receives the message that matches the given correlation identifier (from a non-transactional queue) and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.

System_CAPS_pubmethodReceiveByCorrelationId(String, MessageQueueTransaction)

Receives the message that matches the given correlation identifier (from a transactional queue) and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.

System_CAPS_pubmethodReceiveByCorrelationId(String, MessageQueueTransactionType)

Receives the message that matches the given correlation identifier and immediately raises an exception if no message with the specified correlation identifier currently exists in the queue.

System_CAPS_pubmethodReceiveByCorrelationId(String, TimeSpan)

Receives the message that matches the given correlation identifier (from a non-transactional queue) and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceiveByCorrelationId(String, TimeSpan, MessageQueueTransaction)

Receives the message that matches the given correlation identifier (from a transactional queue) and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceiveByCorrelationId(String, TimeSpan, MessageQueueTransactionType)

Receives the message that matches the given correlation identifier and waits until either a message with the specified correlation identifier is available in the queue, or the time-out expires.

System_CAPS_pubmethodReceiveById(String)

Receives the message that matches the given identifier from a non-transactional queue and immediately raises an exception if no message with the specified identifier currently exists in the queue.

System_CAPS_pubmethodReceiveById(String, MessageQueueTransaction)

Receives the message that matches the given identifier (from a transactional queue) and immediately raises an exception if no message with the specified identifier currently exists in the queue.

System_CAPS_pubmethodReceiveById(String, MessageQueueTransactionType)

Receives the message that matches the given identifier and immediately raises an exception if no message with the specified identifier currently exists in the queue.

System_CAPS_pubmethodReceiveById(String, TimeSpan)

Receives the message that matches the given identifier (from a non-transactional queue) and waits until either a message with the specified identifier is available in the queue or the time-out expires.

System_CAPS_pubmethodReceiveById(String, TimeSpan, MessageQueueTransaction)

Receives the message that matches the given identifier (from a transactional queue) and waits until either a message with the specified identifier is available in the queue or the time-out expires.

System_CAPS_pubmethodReceiveById(String, TimeSpan, MessageQueueTransactionType)

Receives the message that matches the given identifier and waits until either a message with the specified identifier is available in the queue or the time-out expires.

System_CAPS_pubmethodReceiveByLookupId(Int64)

Introduced in MSMQ 3.0. Receives the message that matches the given lookup identifier from a non-transactional queue.

System_CAPS_pubmethodReceiveByLookupId(MessageLookupAction, Int64, MessageQueueTransaction)

Introduced in MSMQ 3.0. Receives a specific message from a transactional queue. The message can be specified by a lookup identifier or by its position at the front or end of the queue.

System_CAPS_pubmethodReceiveByLookupId(MessageLookupAction, Int64, MessageQueueTransactionType)

Introduced in MSMQ 3.0. Receives a specific message from the queue, using the specified transaction context. The message can be specified by a lookup identifier or by its position at the front or end of the queue.

System_CAPS_pubmethodRefresh()

Refreshes the properties presented by the MessageQueue to reflect the current state of the resource.

System_CAPS_pubmethodResetPermissions()

Resets the permission list to the operating system's default values. Removes any queue permissions you have appended to the default list.

System_CAPS_pubmethodSend(Object)

Sends an object to non-transactional queue referenced by this MessageQueue.

System_CAPS_pubmethodSend(Object, MessageQueueTransaction)

Sends an object to the transactional queue referenced by this MessageQueue.

System_CAPS_pubmethodSend(Object, MessageQueueTransactionType)

Sends an object to the queue referenced by this MessageQueue.

System_CAPS_pubmethodSend(Object, String)

Sends an object to the non-transactional queue referenced by this MessageQueue and specifies a label for the message.

System_CAPS_pubmethodSend(Object, String, MessageQueueTransaction)

Sends an object to the transactional queue referenced by this MessageQueue and specifies a label for the message.

System_CAPS_pubmethodSend(Object, String, MessageQueueTransactionType)

Sends an object to the queue referenced by this MessageQueue and specifies a label for the message.

System_CAPS_pubmethodSetPermissions(AccessControlList)

Assigns access rights to the queue based on the contents of an access control list.

System_CAPS_pubmethodSetPermissions(MessageQueueAccessControlEntry)

Assigns access rights to the queue based on the contents of an access control entry.

System_CAPS_pubmethodSetPermissions(String, MessageQueueAccessRights)

Gives a computer, group, or user the specified access rights.

System_CAPS_pubmethodSetPermissions(String, MessageQueueAccessRights, AccessControlEntryType)

Gives a computer, group, or user the specified access rights, with the specified access control type (allow, deny, revoke, or set).

System_CAPS_pubmethodToString()

Returns a String containing the name of the Component, if any. This method should not be overridden.(Inherited from Component.)

NameDescription
System_CAPS_pubfieldSystem_CAPS_staticInfiniteQueueSize

Specifies that no size restriction exists for a queue.

System_CAPS_pubfieldSystem_CAPS_staticInfiniteTimeout

Specifies that no time-out exists for methods that peek or receive messages.

NameDescription
System_CAPS_pubeventDisposed

Occurs when the component is disposed by a call to the Dispose method. (Inherited from Component.)

System_CAPS_pubeventPeekCompleted

Occurs when a message is read without being removed from the queue. This is a result of the asynchronous operation, BeginPeek.

System_CAPS_pubeventReceiveCompleted

Occurs when a message has been removed from the queue. This event is raised by the asynchronous operation, BeginReceive.

NameDescription
System_CAPS_pubmethodAsParallel()

Overloaded. Enables parallelization of a query.(Defined by ParallelEnumerable.)

System_CAPS_pubmethodAsQueryable()

Overloaded. Converts an IEnumerable to an IQueryable.(Defined by Queryable.)

System_CAPS_pubmethodCast<TResult>()

Casts the elements of an IEnumerable to the specified type.(Defined by Enumerable.)

System_CAPS_pubmethodOfType<TResult>()

Filters the elements of an IEnumerable based on a specified type.(Defined by Enumerable.)

The Message Queuing technology allows applications running at different times to communicate across heterogeneous networks and systems that might be temporarily offline. Applications send, receive, or peek (read without removing) messages from queues. Message Queuing is an optional component of Windows 2000 and Windows NT, and must be installed separately.

The MessageQueue class is a wrapper around Message Queuing. There are multiple versions of Message Queuing, and using the MessageQueue class can result in slightly different behavior, depending on the operating system you are using. For information about specific features of each version of Message Queuing, see the topic "What's New in Message Queuing" in the Platform SDK in MSDN.

The MessageQueue class provides a reference to a Message Queuing queue. You can specify a path in the MessageQueue constructor to connect to an existing resource, or you can create a new queue on the server. Before you can call Send(Object), Peek, or Receive, you must associate the new instance of the MessageQueue class with an existing queue. At that point, you can manipulate the queue properties such as Category and Label.

MessageQueue supports two types of message retrieval: synchronous and asynchronous. The synchronous methods, Peek and Receive, cause the process thread to wait a specified time interval for a new message to arrive in the queue. The asynchronous methods, BeginPeek and BeginReceive, allow the main application tasks to continue in a separate thread until a message arrives in the queue. These methods work by using callback objects and state objects to communicate information between threads.

When you create a new instance of the MessageQueue class, you are not creating a new Message Queuing queue. Instead, you can use the Create(String), Delete(String), and Purge methods to manage queues on the server.

Unlike Purge, Create(String) and Delete(String) are static members, so you can call them without creating a new instance of the MessageQueue class.

You can set the MessageQueue object's Path property with one of three names: the friendly name, the FormatName, or the Label. The friendly name, which is defined by the queue's MachineName and QueueName properties, is MachineName\QueueName for a public queue, and MachineName\Private$\QueueName for a private queue. The FormatName property allows offline access to message queues. Lastly, you can use the queue's Label property to set the queue's Path.

For a list of initial property values for an instance of MessageQueue, see the MessageQueue constructor.

The following code example creates new MessageQueue objects using various path name syntax types. In each case, it sends a message to the queue whose path is defined in the constructor.

using System;
using System.Messaging;

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

		//**************************************************
		// Provides an entry point into the application.
		//		 
		// This example demonstrates several ways to set
		// a queue's path.
		//**************************************************

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

			myNewQueue.SendPublic();
			myNewQueue.SendPrivate();
			myNewQueue.SendByLabel();
			myNewQueue.SendByFormatName();
			myNewQueue.MonitorComputerJournal();
			myNewQueue.MonitorQueueJournal();
			myNewQueue.MonitorDeadLetter();
			myNewQueue.MonitorTransactionalDeadLetter();

			return;
		}

		// References public queues.
		public void SendPublic()
		{
			MessageQueue myQueue = new MessageQueue(".\\myQueue");
			myQueue.Send("Public queue by path name.");

			return;
		}

		// References private queues.
		public void SendPrivate()
		{
			MessageQueue myQueue = new 
				MessageQueue(".\\Private$\\myQueue");
			myQueue.Send("Private queue by path name.");

			return;
		}

		// References queues by label.
		public void SendByLabel()
		{
			MessageQueue myQueue = new MessageQueue("Label:TheLabel");
			myQueue.Send("Queue by label.");

			return;
		}

		// References queues by format name.
		public void SendByFormatName()
		{
			MessageQueue myQueue = new 
				MessageQueue("FormatName:Public=5A5F7535-AE9A-41d4" + 
				"-935C-845C2AFF7112");
			myQueue.Send("Queue by format name.");

			return;
		}

		// References computer journal queues.
		public void MonitorComputerJournal()
		{
			MessageQueue computerJournal = new 
				MessageQueue(".\\Journal$");
			while(true)
			{
				Message journalMessage = computerJournal.Receive();
				// Process the journal message.
			}
		}

		// References queue journal queues.
		public void MonitorQueueJournal()
		{
			MessageQueue queueJournal = new 
				MessageQueue(".\\myQueue\\Journal$");
			while(true)
			{
				Message journalMessage = queueJournal.Receive();
				// Process the journal message.
			}
		}

		// References dead-letter queues.
		public void MonitorDeadLetter()
		{
			MessageQueue deadLetter = new 
				MessageQueue(".\\DeadLetter$");
			while(true)
			{
				Message deadMessage = deadLetter.Receive();
				// Process the dead-letter message.
			}
		}

		// References transactional dead-letter queues.
		public void MonitorTransactionalDeadLetter()
		{
			MessageQueue TxDeadLetter = new 
				MessageQueue(".\\XactDeadLetter$");
			while(true)
			{
				Message txDeadLetter = TxDeadLetter.Receive();
				// Process the transactional dead-letter message.
			}
		}

	}
}

The following code example sends a message to a queue, and receives a message from a queue, using an application-specific class called Order.

using System;
using System.Messaging;

namespace MyProject
{

	// This class represents an object the following example 
	// sends to a queue and receives from a queue.
	public class Order
	{
		public int orderId;
		public DateTime orderTime;
	};	

	/// <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();

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

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

			return;
		}


		//**************************************************
		// Sends an Order to a queue.
		//**************************************************

		public void SendMessage()
		{

			// Create a new order and set values.
			Order sentOrder = new Order();
			sentOrder.orderId = 3;
			sentOrder.orderTime = DateTime.Now;

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

			// Send the Order to the queue.
			myQueue.Send(sentOrder);

			return;
		}


		//**************************************************
		// Receives a message containing an Order.
		//**************************************************

		public  void ReceiveMessage()
		{
			// 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 XmlMessageFormatter(new Type[]
				{typeof(MyProject.Order)});

			try
			{
				// Receive and format the message. 
				Message myMessage =	myQueue.Receive(); 
				Order myOrder = (Order)myMessage.Body;

				// Display message information.
				Console.WriteLine("Order ID: " + 
					myOrder.orderId.ToString());
				Console.WriteLine("Sent: " + 
					myOrder.orderTime.ToString());
			}

			catch (MessageQueueException)
			{
				// Handle Message Queuing exceptions.
			}

			// Handle invalid serialization format.
			catch (InvalidOperationException e)
			{
				Console.WriteLine(e.Message);
			}

			// Catch other exceptions as necessary.

			return;
		}
	}
}

.NET Framework
Available since 1.1

Only the GetAllMessagesmethodis thread safe.

Return to top
Show: