MessageQueue.Send 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将对象发送到队列。
重载
| 名称 | 说明 |
|---|---|
| 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引用的非事务性队列。
public:
void Send(System::Object ^ obj);
public void Send(object obj);
member this.Send : obj -> unit
Public Sub Send (obj As Object)
参数
- obj
- Object
要发送到队列的对象。
例外
示例
下面的代码示例连接到消息队列并将消息发送到队列。
#using <system.dll>
#using <system.messaging.dll.>
using namespace System;
using namespace System::Messaging;
ref class MyNewQueue
{
public:
void SendMessage()
{
// Connect to a queue on the local computer.
MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
// Send a message to the queue.
if ( myQueue->Transactional )
{
// Create a transaction.
MessageQueueTransaction^ myTransaction = gcnew 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;
}
};
int main()
{
// Create a new instance of the class.
MyNewQueue^ myNewQueue = gcnew MyNewQueue;
// Send a message to a queue.
myNewQueue->SendMessage();
return 0;
}
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)
{
// 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;
}
}
}
Imports System.Messaging
Public Class MyNewQueue
'
' Provides an entry point into the application.
'
' This example sends a message to a queue.
'
Public Shared Sub Main()
' Create a new instance of the class.
Dim myNewQueue As New MyNewQueue
' Send a message to a queue.
myNewQueue.SendMessage()
Return
End Sub
'
' Sends a message to a queue.
'
Public Sub SendMessage()
' Connect to a queue on the local computer.
Dim myQueue As New MessageQueue(".\myQueue")
' Send a message to the queue.
If myQueue.Transactional = True Then
' Create a transaction.
Dim myTransaction As 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.")
End If
Return
End Sub
End Class
下面的代码示例将应用程序定义的 Order 类发送到队列,然后从该队列接收消息。
注解
使用此重载将包含
如果使用此重载将消息发送到事务队列,该消息将发送到死信队列。 如果希望消息是包含其他消息的事务的一部分,请使用采用 MessageQueueTransaction 或 MessageQueueTransactionType 作为参数的重载。
如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
适用于
Send(Object, MessageQueueTransaction)
将对象发送到由此 MessageQueue引用的事务队列。
public:
void Send(System::Object ^ obj, System::Messaging::MessageQueueTransaction ^ transaction);
public void Send(object obj, System.Messaging.MessageQueueTransaction transaction);
member this.Send : obj * System.Messaging.MessageQueueTransaction -> unit
Public Sub Send (obj As Object, transaction As MessageQueueTransaction)
参数
- obj
- Object
要发送到队列的对象。
- transaction
- MessageQueueTransaction
例外
参数 transaction 为 null.
示例
下面的代码示例将字符串发送到事务队列,然后从该队列接收消息。
#using <system.dll>
#using <system.messaging.dll>
using namespace System;
using namespace System::Messaging;
/// <summary>
/// Provides a container class for the example.
/// </summary>
ref class MyNewQueue
{
public:
//*************************************************
// Sends a message to a queue.
//*************************************************
void SendMessageTransactional()
{
// Connect to a queue on the local computer.
MessageQueue^ myQueue = gcnew MessageQueue( ".\\myTransactionalQueue" );
// Send a message to the queue.
if ( myQueue->Transactional )
{
// Create a transaction.
MessageQueueTransaction^ myTransaction = gcnew 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.
//*************************************************
void ReceiveMessageTransactional()
{
// Connect to a transactional queue on the local computer.
MessageQueue^ myQueue = gcnew MessageQueue( ".\\myTransactionalQueue" );
// Set the formatter.
array<Type^>^p = gcnew array<Type^>(1);
p[ 0 ] = String::typeid;
myQueue->Formatter = gcnew XmlMessageFormatter( p );
// Create a transaction.
MessageQueueTransaction^ myTransaction = gcnew MessageQueueTransaction;
try
{
// Begin the transaction.
myTransaction->Begin();
// Receive the message.
Message^ myMessage = myQueue->Receive( myTransaction );
String^ myOrder = static_cast<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;
}
};
//*************************************************
// Provides an entry point into the application.
//
// This example sends and receives a message from
// a transactional queue.
//*************************************************
int main()
{
// Create a new instance of the class.
MyNewQueue^ myNewQueue = gcnew MyNewQueue;
// Send a message to a queue.
myNewQueue->SendMessageTransactional();
// Receive a message from a queue.
myNewQueue->ReceiveMessageTransactional();
return 0;
}
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)
{
// 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;
}
}
}
Imports System.Messaging
Public Class MyNewQueue
'
' Provides an entry point into the application.
'
' This example sends and receives a message from
' a transactional queue.
'
Public Shared Sub Main()
' Create a new instance of the class.
Dim myNewQueue As New MyNewQueue
' Send a message to a queue.
myNewQueue.SendMessageTransactional()
' Receive a message from a queue.
myNewQueue.ReceiveMessageTransactional()
Return
End Sub
'
' Sends a message to a queue.
'
Public Sub SendMessageTransactional()
' Connect to a queue on the local computer.
Dim myQueue As New MessageQueue(".\myTransactionalQueue")
' Send a message to the queue.
If myQueue.Transactional = True Then
' Create a transaction.
Dim myTransaction As New MessageQueueTransaction
' Begin the transaction.
myTransaction.Begin()
' Send the message.
myQueue.Send("My Message Data.", myTransaction)
' Commit the transaction.
myTransaction.Commit()
End If
Return
End Sub
'
' Receives a message containing an Order.
'
Public Sub ReceiveMessageTransactional()
' Connect to a transactional queue on the local computer.
Dim myQueue As New MessageQueue(".\myTransactionalQueue")
' Set the formatter.
myQueue.Formatter = New XmlMessageFormatter(New Type() _
{GetType([String])})
' Create a transaction.
Dim myTransaction As New MessageQueueTransaction
Try
' Begin the transaction.
myTransaction.Begin()
' Receive the message.
Dim myMessage As Message = _
myQueue.Receive(myTransaction)
Dim myOrder As [String] = CType(myMessage.Body, _
[String])
' Display message information.
Console.WriteLine(myOrder)
' Commit the transaction.
myTransaction.Commit()
Catch e As MessageQueueException
' Handle nontransactional queues.
If e.MessageQueueErrorCode = _
MessageQueueErrorCode.TransactionUsage Then
Console.WriteLine("Queue is not transactional.")
End If
' Else catch other sources of a MessageQueueException.
' Roll back the transaction.
myTransaction.Abort()
' Catch other exceptions as necessary, such as
' InvalidOperationException, thrown when the formatter
' cannot deserialize the message.
End Try
Return
End Sub
End Class
注解
使用此重载,使用参数定义的内部事务上下文将包含 obj 参数的消息发送到由 MessageQueue该参数引用的 transaction 事务队列。 发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送任何其他对象, Message则对象将被序列化并插入到消息正文中。
如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。
如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。
MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic 将主线程的状态设置为STA,因此必须在子例程中Main应用MTAThreadAttribute该状态。 否则,使用另一个线程发送事务性消息将 MessageQueueException 引发异常。 使用以下片段应用 MTAThreadAttribute 该代码片段。
<System.MTAThreadAttribute>
public sub Main()
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
- DefaultPropertiesToSend
- Message
- MessageQueueTransaction
- Transactional
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
适用于
Send(Object, MessageQueueTransactionType)
将对象发送到由此 MessageQueue引用的队列。
public:
void Send(System::Object ^ obj, System::Messaging::MessageQueueTransactionType transactionType);
public void Send(object obj, System.Messaging.MessageQueueTransactionType transactionType);
member this.Send : obj * System.Messaging.MessageQueueTransactionType -> unit
Public Sub Send (obj As Object, transactionType As MessageQueueTransactionType)
参数
- obj
- Object
要发送到队列的对象。
- transactionType
- MessageQueueTransactionType
其中 MessageQueueTransactionType 一个值,描述要与消息关联的事务上下文的类型。
例外
该 transactionType 参数不是其中一个 MessageQueueTransactionType 成员。
示例
下面的代码示例演示了如何使用 Send(Object, MessageQueueTransactionType)。
// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message^ msg = gcnew Message("Example Message Body");
// Send the message.
queue->Send(msg, MessageQueueTransactionType::Single);
queue->Close();
// 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 参数的消息发送到由该参数引用 MessageQueue的 transactionType 队列。
Automatic如果已将外部事务上下文附加到要用于发送消息的线程,请指定transactionType该参数。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务消息发送到非事务线程。
发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送任何其他对象, Message则对象将被序列化并插入到消息正文中。
如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
- MessageQueueTransactionType
- DefaultPropertiesToSend
- Message
- Transactional
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
适用于
Send(Object, String)
将对象发送到由此 MessageQueue 引用的非事务性队列,并指定消息的标签。
public:
void Send(System::Object ^ obj, System::String ^ label);
public void Send(object obj, string label);
member this.Send : obj * string -> unit
Public Sub Send (obj As Object, label As String)
参数
- obj
- Object
要发送到队列的对象。
- label
- String
邮件的标签。
例外
参数 label 为 null.
示例
下面的代码示例演示了如何使用 Send(Object, String)。
// Connect to a queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleQueue");
// Create a new message.
Message^ msg = gcnew Message("Example Message Body");
// Send the message.
queue->Send(msg, "Example Message Label");
queue->Close();
// 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");
注解
使用此重载将包含
消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。
如果使用此重载将消息发送到事务队列,该消息将发送到死信队列。 如果希望消息是包含其他消息的事务的一部分,请使用采用 MessageQueueTransaction 或 MessageQueueTransactionType 作为参数的重载。
Path在发送消息之前,必须指定此MessageQueue实例的属性。 如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性。
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
适用于
Send(Object, String, MessageQueueTransaction)
将对象发送到由此 MessageQueue 引用的事务队列,并指定消息的标签。
public:
void Send(System::Object ^ obj, System::String ^ label, System::Messaging::MessageQueueTransaction ^ transaction);
public void Send(object obj, string label, System.Messaging.MessageQueueTransaction transaction);
member this.Send : obj * string * System.Messaging.MessageQueueTransaction -> unit
Public Sub Send (obj As Object, label As String, transaction As MessageQueueTransaction)
参数
- obj
- Object
要发送到队列的对象。
- label
- String
邮件的标签。
- transaction
- MessageQueueTransaction
例外
示例
下面的代码示例演示了如何使用 Send(Object, String, MessageQueueTransaction)。
// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message^ msg = gcnew Message("Example Message Body");
// Create a message queuing transaction.
MessageQueueTransaction^ transaction = gcnew 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 (Exception^ ex)
{
// Cancel the transaction.
transaction->Abort();
// Propagate the exception.
throw ex;
}
finally
{
// Dispose of the transaction object.
delete transaction;
queue->Close();
}
// 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则对象将被序列化并插入到消息正文中。
消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。
如果使用此重载将消息发送到非事务性队列,则消息可能会发送到死信队列,而不会引发异常。
如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend 设置的 Message.Formatter 属性值,并且消息的属性优先于队列 MessageQueue.Formatter 的属性
MessageQueueTransaction 线程单元感知,因此,如果单元状态为 STA,则不能在多个线程中使用事务。 Visual Basic 将主线程的状态设置为STA,因此必须在子例程中Main应用MTAThreadAttribute该状态。 否则,使用另一个线程发送事务性消息将 MessageQueueException 引发异常。 使用以下片段应用 MTAThreadAttribute 该代码片段。
<System.MTAThreadAttribute>
public sub Main()
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
- DefaultPropertiesToSend
- Message
- MessageQueueTransaction
- Transactional
- Label
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()
适用于
Send(Object, String, MessageQueueTransactionType)
将对象发送到由此 MessageQueue 引用的队列,并指定消息的标签。
public:
void Send(System::Object ^ obj, System::String ^ label, System::Messaging::MessageQueueTransactionType transactionType);
public void Send(object obj, string label, System.Messaging.MessageQueueTransactionType transactionType);
member this.Send : obj * string * System.Messaging.MessageQueueTransactionType -> unit
Public Sub Send (obj As Object, label As String, transactionType As MessageQueueTransactionType)
参数
- obj
- Object
要发送到队列的对象。
- label
- String
邮件的标签。
- transactionType
- MessageQueueTransactionType
其中 MessageQueueTransactionType 一个值,描述要与消息关联的事务上下文的类型。
例外
参数 label 为 null.
消息队列应用程序指示事务使用不正确。
该 transactionType 参数不是其中一个 MessageQueueTransactionType 成员。
示例
下面的代码示例演示了如何使用 Send(Object, String, MessageQueueTransactionType)。
// Connect to a transactional queue on the local computer.
MessageQueue^ queue = gcnew MessageQueue(".\\exampleTransQueue");
// Create a new message.
Message^ msg = gcnew Message("Example Message Body");
// Send the message.
queue->Send(msg, "Example Message Label",
MessageQueueTransactionType::Single);
queue->Close();
// 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 参数的消息发送到由该参数引用 MessageQueue的 transactionType 队列。
Automatic如果已将外部事务上下文附加到要用于发送消息的线程,请指定transactionType该参数。 指定 Single 是否要将消息作为单个内部事务发送。 可以指定 None 是否要将事务消息发送到非事务线程。
发送到队列的对象可以是一个或多个 Message 托管对象。 如果发送任何其他对象, Message则对象将被序列化并插入到消息正文中。 使用此重载,可以指定标识消息的字符串标签。
消息标签不同于消息队列标签,但两者都依赖于应用程序,并且对消息队列没有继承意义。
如果在调用
该 DefaultPropertiesToSend 属性适用于除 .之外 Message的任何对象。 例如,如果使用成员指定标签或优先级 DefaultPropertiesToSend ,则这些值将应用于包含对象的任何消息,当应用程序将对象发送到队列时,该消息不属于类型 Message 。 发送时 Message,为 Message 优先 DefaultPropertiesToSend顺序设置的 Message.Formatter 属性值,消息的属性优先于队列 MessageQueue.Formatter 的属性。
下表显示了此方法在各种工作组模式下是否可用。
| 工作组模式 | 可用 |
|---|---|
| 本地计算机 | 是的 |
| 本地计算机和直接格式名称 | 是的 |
| 远程计算机 | 否 |
| 远程计算机和直接格式名称 | 是的 |
另请参阅
- MessageQueueTransactionType
- DefaultPropertiesToSend
- Message
- Transactional
- Label
- Peek()
- Receive()
- BeginPeek()
- BeginReceive()