MessageQueue.Send 方法

定义

向队列发送对象。

重载

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。

Send(Object)

将对象发送到此 MessageQueue 引用的非事务性队列。

C#
public void Send (object obj);

参数

obj
Object

要发送到队列的对象。

例外

尚未设置 Path 属性。

- 或 -

访问“消息队列”方法时出错。

示例

下面的代码示例连接到消息队列并将消息发送到队列。

C#
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 sends a message to 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();

            return;
        }

        //**************************************************
        // Sends a message to a queue.
        //**************************************************
        
        public void SendMessage()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                // Create a transaction.
                MessageQueueTransaction myTransaction = new
                    MessageQueueTransaction();

                // Begin the transaction.
                myTransaction.Begin();

                // Send the message.
                myQueue.Send("My Message Data.", myTransaction);

                // Commit the transaction.
                myTransaction.Commit();
            }
            else
            {
                myQueue.Send("My Message Data.");
            }

            return;
        }
    }
}

下面的代码示例将应用程序定义的 Order 类发送到队列,然后从该队列接收消息。

注解

使用此重载将包含 obj 参数的消息发送到 引用的 MessageQueue队列。 发送到队列的对象可以是 Message 或任何托管对象。 如果发送除 以外的 Message任何对象,则对象将被序列化并插入到消息正文中。

如果使用此重载将消息发送到事务队列,则消息将发送到死信队列。 如果希望消息成为包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。

如果在调用 Send(Object)之前未设置 Formatter 属性,格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型的 Message 对象的任何消息。 发送 Message时,为 Message 设置的属性值优先于 DefaultPropertiesToSend ,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Send(Object, MessageQueueTransaction)

将对象发送到此 MessageQueue 所引用的事务性队列。

C#
public void Send (object obj, System.Messaging.MessageQueueTransaction transaction);

参数

obj
Object

要发送到队列的对象。

例外

transaction 参数为 null

尚未设置 Path 属性。

- 或 -

“消息队列”应用程序指示事务用法不正确。

- 或 -

访问“消息队列”方法时出错。

示例

下面的代码示例将字符串发送到事务性队列,然后从该队列接收消息。

C#
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 sends and receives a message from
        // a transactional queue.
        //**************************************************

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

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

            // Receive a message from a queue.
            myNewQueue.ReceiveMessageTransactional();
        
            return;
        }

        //**************************************************
        // Sends a message to a queue.
        //**************************************************
        
        public void SendMessageTransactional()
        {
                        
            // Connect to a queue on the local computer.
            MessageQueue myQueue = new
                MessageQueue(".\\myTransactionalQueue");

            // Send a message to the queue.
            if (myQueue.Transactional == true)
            {
                // Create a transaction.
                MessageQueueTransaction myTransaction = new
                    MessageQueueTransaction();

                // Begin the transaction.
                myTransaction.Begin();

                // Send the message.
                myQueue.Send("My Message Data.", myTransaction);

                // Commit the transaction.
                myTransaction.Commit();
            }

            return;
        }

        //**************************************************
        // Receives a message containing an Order.
        //**************************************************
        
        public  void ReceiveMessageTransactional()
        {
            // Connect to a transactional queue on the local computer.
            MessageQueue myQueue = new
                MessageQueue(".\\myTransactionalQueue");

            // Set the formatter.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(String)});
            
            // Create a transaction.
            MessageQueueTransaction myTransaction = new
                MessageQueueTransaction();

            try
            {
                // Begin the transaction.
                myTransaction.Begin();
                
                // Receive the message.
                Message myMessage =	myQueue.Receive(myTransaction);
                String myOrder = (String)myMessage.Body;

                // Display message information.
                Console.WriteLine(myOrder);

                // Commit the transaction.
                myTransaction.Commit();
            }
            
            catch (MessageQueueException e)
            {
                // Handle nontransactional queues.
                if (e.MessageQueueErrorCode ==
                    MessageQueueErrorCode.TransactionUsage)
                {
                    Console.WriteLine("Queue is not transactional.");
                }
                
                // Else catch other sources of MessageQueueException.

                // Roll back the transaction.
                myTransaction.Abort();
            }

            // Catch other exceptions as necessary, such as
            // InvalidOperationException, thrown when the formatter
            // cannot deserialize the message.

            return;
        }
    }
}

注解

使用此重载,使用 由 参数定义的内部事务上下文,将包含 obj 参数的消息发送到 引用的transaction事务队列MessageQueue。 发送到队列的对象可以是 Message 或任何托管对象。 如果发送除 以外的 Message任何对象,则对象将被序列化并插入到消息正文中。

如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。

如果在调用 Send(Object)之前未设置 Formatter 属性,格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型的 Message 对象的任何消息。 发送 Message时,为 Message 设置的属性值优先于 DefaultPropertiesToSend ,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。

MessageQueueTransaction 是线程单元感知,因此如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic 将main线程的状态设置为 STA,因此必须在子例程中Main应用 MTAThreadAttribute 。 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。 使用以下片段应用 MTAThreadAttribute

VB
<System.MTAThreadAttribute>
 public sub Main()

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Send(Object, MessageQueueTransactionType)

将对象发送到此 MessageQueue 所引用的队列。

C#
public void Send (object obj, System.Messaging.MessageQueueTransactionType transactionType);

参数

obj
Object

要发送到队列的对象。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

例外

transactionType 参数不是 MessageQueueTransactionType 成员之一。

尚未设置 Path 属性。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, MessageQueueTransactionType) 的用法。

C#

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

// Send the message.
queue.Send(msg, MessageQueueTransactionType.Single);

注解

使用此重载使用 由 参数定义的事务上下文,将包含 obj 参数的消息发送到 引用的transactionType队列MessageQueue。 如果已将外部事务上下文附加到要用于发送消息的线程,则为 transactionType 参数指定 Automatic 。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务性消息发送到非事务线程。

发送到队列的对象可以是 Message 或任何托管对象。 如果发送除 以外的 Message任何对象,则对象将被序列化并插入到消息正文中。

如果在调用 Send(Object)之前未设置 Formatter 属性,格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型的 Message 对象的任何消息。 发送 Message时,为 Message 设置的属性值优先于 DefaultPropertiesToSend ,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。

下表显示了此方法在各种工作组模式下是否可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Send(Object, String)

将对象发送到此 MessageQueue 引用的非事务性队列,并指定消息的标签。

C#
public void Send (object obj, string label);

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

例外

label 参数为 null

尚未设置 Path 属性。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String) 的用法。

C#

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

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

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

注解

使用此重载将包含 obj 参数的消息发送到 引用 MessageQueue的队列。 使用此重载,可以指定标识消息的字符串标签。 发送到队列的对象可以是 Message、结构、数据对象或任何托管对象。 如果发送除 以外的 Message任何对象,则会序列化对象并将其插入到消息正文中。

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。

如果使用此重载将消息发送到事务队列,则会将消息发送到死信队列。 如果希望消息成为包含其他消息的事务的一部分,请使用采用 MessageQueueTransactionMessageQueueTransactionType 作为参数的重载。

Path 发送消息之前,必须指定此 MessageQueue 实例的 属性。 如果在调用 Send(Object)之前未设置 Formatter 属性,则格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型 Message 对象的任何消息。 发送 时, MessageMessage 设置的属性值优先于 DefaultPropertiesToSend ,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Send(Object, String, MessageQueueTransaction)

将对象发送到此 MessageQueue 引用的事务性队列中,并指定该消息的标签。

C#
public void Send (object obj, string label, System.Messaging.MessageQueueTransaction transaction);

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

例外

label 参数为 null

- 或 -

transaction 参数为 null

尚未设置 Path 属性。

- 或 -

“消息队列”应用程序指示了不正确的事务用法。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransaction) 的用法。

C#

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

注解

使用此重载,可以使用 参数定义的内部事务上下文,将包含 obj 参数的消息发送到 由 MessageQueue引用的 transaction 事务队列。 使用此重载,可以指定标识消息的字符串标签。 发送到队列的对象可以是 Message、结构、数据对象或任何托管对象。 如果发送除 以外的 Message任何对象,则会序列化对象并将其插入到消息正文中。

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。

如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。

如果在调用 Send(Object)之前未设置 Formatter 属性,则格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型 Message 对象的任何消息。 发送 时, MessageMessage 优先设置的属性值优先于 DefaultPropertiesToSend ,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性

MessageQueueTransaction 是线程单元感知,因此,如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic 将main线程的状态设置为 STA,因此必须在子例程中Main应用 MTAThreadAttribute 。 否则,利用另一个线程发送事务性消息将引发 MessageQueueException 异常。 使用以下片段应用 MTAThreadAttribute

VB
<System.MTAThreadAttribute>
 public sub Main()

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Send(Object, String, MessageQueueTransactionType)

将对象发送到此 MessageQueue 引用的队列中,并指定该消息的标签。

C#
public void Send (object obj, string label, System.Messaging.MessageQueueTransactionType transactionType);

参数

obj
Object

要发送到队列的对象。

label
String

消息的标签。

transactionType
MessageQueueTransactionType

MessageQueueTransactionType 值之一,它描述与消息关联的事务上下文的类型。

例外

label 参数为 null

“消息队列”应用程序指示了不正确的事务用法。

transactionType 参数不是 MessageQueueTransactionType 成员之一。

尚未设置 Path 属性。

- 或 -

访问“消息队列”方法时出错。

示例

以下代码示例演示了 Send(Object, String, MessageQueueTransactionType) 的用法。

C#

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

// Send the message.
queue.Send(msg, "Example Message Label",
    MessageQueueTransactionType.Single);

注解

使用此重载,可以使用 参数定义的事务上下文,将包含 obj 参数的消息发送到 引用的transactionType队列MessageQueue。 如果已将外部事务上下文附加到要用于发送消息的线程,请为 transactionType 参数指定 Automatic 。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务性消息发送到非事务线程。

发送到队列的对象可以是 Message 或任何托管对象。 如果发送除 以外的 Message任何对象,则会序列化对象并将其插入到消息正文中。 使用此重载,可以指定标识消息的字符串标签。

消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。

如果在调用 Send(Object)之前未设置 Formatter 属性,则格式化程序默认为 XmlMessageFormatter

属性 DefaultPropertiesToSend 适用于 除 以外的 Message任何对象。 例如,如果使用 成员指定标签或优先级 DefaultPropertiesToSend ,则当应用程序将对象发送到队列时,这些值将应用于包含非类型 Message 对象的任何消息。 发送 时, MessageMessage 设置的属性值优先于 DefaultPropertiesToSend,消息的 Message.Formatter 属性优先于队列的 MessageQueue.Formatter 属性。

下表显示了此方法是否在各种工作组模式下可用。

工作组模式 可用
本地计算机
本地计算机和直接格式名称
远程计算机
远程计算机和直接格式名称

另请参阅

适用于

.NET Framework 4.8.1 和其他版本
产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1