Export (0) Print
Expand All

MessageQueue.Send Method (Object, String, MessageQueueTransaction)

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

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

public void Send (
	Object obj,
	string label,
	MessageQueueTransaction transaction
)
public void Send (
	Object obj, 
	String label, 
	MessageQueueTransaction transaction
)
public function Send (
	obj : Object, 
	label : String, 
	transaction : MessageQueueTransaction
)
Not applicable.

Parameters

obj

The object to send to the queue.

label

The label of the message.

transaction

The MessageQueueTransaction object.

Exception typeCondition

ArgumentNullException

The label parameter is a null reference (Nothing in Visual Basic).

-or-

The transaction parameter is a null reference (Nothing in Visual Basic).

MessageQueueException

The Path property has not been set.

-or-

The Message Queuing application indicated an incorrect transaction usage.

-or-

An error occurred when accessing a Message Queuing method.

Use this overload to send a message that contains the obj parameter to the transactional queue referenced by the MessageQueue, using an internal transaction context defined by the transaction parameter. With this overload, you can specify the string label that identifies the message. The object you send to the queue can be a Message, a structure, a data object, or any managed object. If you send any object other than a Message, the object is serialized and inserted into the body of the message.

The message label is distinct from the message queue label, but both are application-dependent and have no inherit meaning to Message Queuing.

If you use this overload to send a message to a non-transactional queue, the message might be sent to the dead-letter queue without throwing an exception.

If you do not set the Formatter property before calling Send(Object), the formatter defaults to the XmlMessageFormatter.

The DefaultPropertiesToSend property applies to any object other than a Message. If you specify, for example, a label or a priority using the DefaultPropertiesToSend member, these values apply to any message that contains an object that is not of type Message when your application sends it to the queue. When sending a Message, the property values set for the Message take precedence over DefaultPropertiesToSend and the message's Message.Formatter property takes precedence over the queue's MessageQueue.Formatter property

MessageQueueTransaction is threading apartment aware, so if your apartment state is STA, you cannot use the transaction in multiple threads. Visual Basic sets the state of the main thread to STA, so you must apply the MTAThreadAttribute in the Main subroutine. Otherwise, sending a transactional message using another thread throws a MessageQueueException exception. You apply the MTAThreadAttribute by using the following fragment.

<System.MTAThreadAttribute>
 public sub Main()

The following table shows whether this method is available in various Workgroup modes.

Workgroup mode

Available

Local computer

Yes

Local computer and direct format name

Yes

Remote computer

No

Remote computer and direct format name

Yes

NoteNote:

This method is thread-safe when its object argument is a Message instance. The method is not thread-safe when its object argument is a non-Message instance.

The following code example demonstrates the use of Send(Object,String,MessageQueueTransaction).


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

// Create a new message.
Message msg = new Message("Example Message Body");

// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();

try
{
    // Begin a transaction.
    transaction.Begin();

    // Send the message to the queue.
    queue.Send(msg, "Example Message Label", transaction);

    // Commit the transaction.
    transaction.Commit();
}
catch(System.Exception e)
{
    // Cancel the transaction.
    transaction.Abort();

    // Propagate the exception.
    throw e;
}
finally
{
    // Dispose of the transaction object.
    transaction.Dispose();
}


// Connect to a transactional queue on the local computer.
MessageQueue queue = new MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message msg = new Message("Example Message Body");
// Create a message queuing transaction.
MessageQueueTransaction transaction = new MessageQueueTransaction();

try {
    // Begin a transaction.
    transaction.Begin();
    // Send the message to the queue.
    queue.Send(msg, "Example Message Label", transaction);
    // Commit the transaction.
    transaction.Commit();
}
catch (System.Exception e) {
    // Cancel the transaction.
    transaction.Abort();
    // Propagate the exception.
    throw e;
}
finally {
    // Dispose of the transaction object.
    transaction.Dispose();
}

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft