Message 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
提供对定义消息队列消息所需的属性的访问。
public ref class Message : System::ComponentModel::Componentpublic class Message : System.ComponentModel.Componenttype Message = class
    inherit ComponentPublic Class Message
Inherits Component- 继承
示例
下面的代码示例演示如何使用 设置消息正文的格式 BinaryMessageFormatter。
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;
        }
    }
}
Imports System.Messaging
Imports System.Drawing
Imports System.IO
Namespace MyProj
    _
   
   
   Public Class MyNewQueue
      
      
      '**************************************************
      ' Provides an entry point into the application.
      '		 
      ' This example sends and receives a message from
      ' a queue.
      '**************************************************
      Public Shared Sub Main()
         ' Create a new instance of the class.
         Dim myNewQueue As 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
      End Sub
      
      
      '**************************************************
      ' Creates a new queue.
      '**************************************************
      Public Shared Sub CreateQueue(queuePath As String)
         Try
            If Not MessageQueue.Exists(queuePath) Then
               MessageQueue.Create(queuePath)
            Else
               Console.WriteLine((queuePath + " already exists."))
            End If
         Catch e As MessageQueueException
            Console.WriteLine(e.Message)
         End Try
      End Sub
       
      
      '**************************************************
      ' Sends an image to a queue, using the BinaryMessageFormatter.
      '**************************************************
      Public Sub 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.
            Dim myImage As Image = Bitmap.FromFile("SentImage.bmp")
            
            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")
            
            Dim myMessage As New Message(myImage, New BinaryMessageFormatter())
            
            ' Send the image to the queue.
            myQueue.Send(myMessage)
         Catch e As ArgumentException
            Console.WriteLine(e.Message)
         End Try 
         
         Return
      End Sub
      
      
      
      '**************************************************
      ' Receives a message containing an image.
      '**************************************************
      Public Sub ReceiveMessage()
         
         Try
            
            ' Connect to the a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")
            
            ' Set the formatter to indicate body contains an Order.
            myQueue.Formatter = New BinaryMessageFormatter()
            
            ' Receive and format the message. 
            Dim myMessage As System.Messaging.Message = myQueue.Receive()
            Dim myImage As Bitmap = CType(myMessage.Body, Bitmap)
            
            ' This will be saved in the \bin\debug or \bin\retail folder.
            myImage.Save("ReceivedImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp)
         
         
         
         'Catch
         ' Handle Message Queuing exceptions.
         
         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)
         
         Catch e As IOException
         End Try
         ' Handle file access exceptions.
         
         ' Catch other exceptions as necessary.
         Return
      End Sub
   End Class
End Namespace 'MyProj
下面的代码示例演示如何使用 设置消息正文的格式 XmlMessageFormatter。
#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;
ref class Order
{
public:
   int orderId;
   DateTime orderTime;
};
ref class MyNewQueue
{
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 );
      }
   }
   void SendMessage()
   {
      try
      {
         // Create a new order and set values.
         Order^ sentOrder = gcnew Order;
         sentOrder->orderId = 3;
         sentOrder->orderTime = DateTime::Now;
         // Connect to a queue on the local computer.
         MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
         // Create the new order.
         Message^ myMessage = gcnew Message( sentOrder );
         // Send the order to the queue.
         myQueue->Send( myMessage );
      }
      catch ( ArgumentException^ e ) 
      {
         Console::WriteLine( e->Message );
      }
      return;
   }
   void ReceiveMessage()
   {
      // Connect to the a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );
      // Set the formatter to indicate body contains an Order.
      array<Type^>^p = gcnew array<Type^>(1);
      p[ 0 ] = Order::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Receive and format the message. 
         Message^ myMessage = myQueue->Receive();
         Order^ myOrder = dynamic_cast<Order^>(myMessage->Body);
         // Display message information.
         Console::WriteLine( "Order ID: {0}", myOrder->orderId );
         Console::WriteLine( "Sent: {0}", myOrder->orderTime );
      }
      catch ( MessageQueueException^ ) 
      {
         // Handle Message Queuing exceptions.
      }
      // Handle invalid serialization format.
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }
      // Catch other exceptions as necessary.
      return;
   }
};
int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;
   // Create a queue on the local computer.
   MyNewQueue::CreateQueue( ".\\myQueue" );
   // Send a message to a queue.
   myNewQueue->SendMessage();
   // Receive a message from a queue.
   myNewQueue->ReceiveMessage();
   return 0;
}
using System;
using System.Messaging;
using System.Drawing;
using System.IO;
namespace MyProject
{
    // 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();
            // 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 Order to a queue.
        //**************************************************
        
        public void SendMessage()
        {
            try
            {
                // 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");
                // Create the new order.
                Message myMessage = new Message(sentOrder);
                // Send the order to the queue.
                myQueue.Send(myMessage);
            }
            catch(ArgumentException e)
            {
                Console.WriteLine(e.Message);
            }
            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;
        }
    }
}
Imports System.Messaging
Imports System.Drawing
Imports System.IO
   
' The following example 
' sends to a queue and receives from a queue.
Public Class Order
      Public orderId As Integer
      Public orderTime As DateTime
End Class
   
  
' Provides a container class for the example.
Public Class MyNewQueue
      
      
   ' Provides an entry point into the application.
   '		 
   ' This example sends and receives a message from
   ' a queue.
   Public Shared Sub Main()
      ' Create a new instance of the class.
      Dim myNewQueue As 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
   End Sub
      
      
      ' Creates a new queue.
   Public Shared Sub CreateQueue(queuePath As String)
      Try
         If Not MessageQueue.Exists(queuePath) Then
            MessageQueue.Create(queuePath)
         Else
            Console.WriteLine((queuePath + " already exists."))
         End If
      Catch e As MessageQueueException
         Console.WriteLine(e.Message)
      End Try
   End Sub
       
      
      ' Sends an Order to a queue.
   Public Sub SendMessage()
      Try
            
            ' Create a new order and set values.
            Dim sentOrder As New Order()
            sentOrder.orderId = 3
            sentOrder.orderTime = DateTime.Now
            
            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")
            
            
            
            ' Create the new order.
            Dim myMessage As New Message(sentOrder)
            
            ' Send the order to the queue.
            myQueue.Send(myMessage)
      Catch e As ArgumentException
            Console.WriteLine(e.Message)
      End Try 
         
      Return
   End Sub
      
      
      
 
      ' Receives a message containing an order.
 
   Public Sub ReceiveMessage()
         ' Connect to the a queue on the local computer.
         Dim myQueue As New MessageQueue(".\myQueue")
         
         ' Set the formatter to indicate body contains an Order.
         myQueue.Formatter = New XmlMessageFormatter(New Type() {GetType(Order)})
         
         Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQueue.Receive()
            Dim myOrder As Order = CType(myMessage.Body, Order)
            
            ' Display message information.
            Console.WriteLine(("Order ID: " + myOrder.orderId.ToString()))
            Console.WriteLine(("Sent: " + myOrder.orderTime.ToString()))
         
  
         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)
         End Try
         
         ' Catch other exceptions as necessary.
         Return
   End Sub
End Class
注解
Message使用 类从队列中查看或接收消息,或者在向队列发送消息时对消息属性进行精细控制。
MessageQueue Message在从队列中速览或接收消息时使用 类,因为 和 MessageQueue.Receive 方法都MessageQueue.Peek创建 类的新实例Message并设置实例的属性。 类 Message 的只读属性适用于从队列中检索消息,而读/写属性适用于发送和检索消息。 从队列中速览或接收消息时 MessageQueue ,其 MessageReadPropertyFilter 属性将确定检索消息的属性。
类 MessageQueue 的 Send 方法允许您为要发送到该队列的消息指定任何对象类型。 可以使用 MessageQueue 实例的 属性为发送到队列的 DefaultPropertiesToSend 一般消息指定设置。 设置类型包括格式化程序、标签、加密和身份验证。 在协调消息应用程序以响应确认和报告消息时,还可以为相应 DefaultPropertiesToSend 成员指定值。 Message使用实例将消息发送到队列可以灵活地访问和修改其中的许多属性 - 无论是针对单个消息还是逐条消息。 Message 属性优先于 DefaultPropertiesToSend。
消息数据存储在 属性中, Body 在较小程度上存储在 AppSpecific 和 Extension 属性中。 加密、序列化或反序列化消息数据时,仅影响 属性的内容 Body 。
发送消息时, Body 将使用 Formatter 指定的 属性对 属性的内容进行序列化。 序列化的内容在 属性中找到 BodyStream 。 还可以直接设置 BodyStream 属性,例如,将文件作为消息的数据内容发送。 可以在发送消息之前随时更改 Body 或 Formatter 属性,并且调用 时 Send,数据将相应地序列化。
由 MessageQueue.DefaultPropertiesToSend 属性定义的属性仅适用于不是 类型的 Message消息。 如果为 MessageQueue指定 DefaultPropertiesToSend 属性,则发送到该队列的Message实例中具有相同名称的属性会导致忽略这些默认属性。
有关 实例 Message的初始属性值列表, Message 请参阅 构造函数。
构造函数
| Message() | 初始化 Message 类的新实例(主体为空)。 | 
| Message(Object) | 通过用 Message 将指定的对象序列化到消息体中,初始化 XmlMessageFormatter 类的新实例。 | 
| Message(Object, IMessageFormatter) | 初始化 Message 类的新实例,使用指定的格式化程序将指定的对象序列化到消息体中。 | 
字段
| InfiniteTimeout | 指定不存在超时。 | 
属性
| AcknowledgeType | 获取或设置返回给发送应用程序的确认消息的类型。 | 
| Acknowledgment | 获取该消息表示的确认分类。 | 
| AdministrationQueue | 获取或设置接收由消息队列生成的确认消息的队列。 | 
| AppSpecific | 获取或设置应用程序特定的附加信息。 | 
| ArrivedTime | 获取消息到达目标队列的时间。 | 
| AttachSenderId | 获取或设置一个值,该值指示发送方 ID 是否应附在消息中。 | 
| Authenticated | 获取一个值,该值指示消息是否已验证。 | 
| AuthenticationProviderName | 获取或设置用于生成消息数字签名的加密提供程序的名称。 | 
| AuthenticationProviderType | 获取或设置用于生成消息数字签名的加密提供程序类型。 | 
| Body | 获取或设置消息的内容。 | 
| BodyStream | 获取或设置消息正文中的信息。 | 
| BodyType | 获取或设置消息正文所包含数据的类型。 | 
| CanRaiseEvents | 获取一个指示组件是否可以引发事件的值。(继承自 Component) | 
| ConnectorType | 获取或设置一个值,该值指示由发送应用程序设置通常由消息队列设置的某些消息属性。 | 
| Container | 获取包含 IContainer 的 Component。(继承自 Component) | 
| CorrelationId | 获取或设置确认、报告和响应消息用以引用原始消息的消息标识符。 | 
| DesignMode | 获取一个值,用以指示 Component 当前是否处于设计模式。(继承自 Component) | 
| DestinationQueue | 获取消息的预期目标队列。 | 
| DestinationSymmetricKey | 获取或设置用于对应用程序加密的消息或发送到外部队列的消息进行加密的对称密钥。 | 
| DigitalSignature | 获取或设置消息队列用于验证消息的数字签名。 | 
| EncryptionAlgorithm | 获取或设置用于加密私有消息体的加密算法。 | 
| Events | 获取附加到此 Component 的事件处理程序的列表。(继承自 Component) | 
| Extension | 获取或设置与消息关联的、应用程序定义的附加信息。 | 
| Formatter | 获取或设置用于将对象序列化到消息体中,或从消息体中反序列化对象的格式化程序。 | 
| HashAlgorithm | 获取或设置消息队列在验证消息或为消息创建数字签名时使用的哈希算法。 | 
| Id | 获取消息的标识符。 | 
| IsFirstInTransaction | 获取一个值,该值指示消息是否是事务中发送的第一条消息。 | 
| IsLastInTransaction | 获取一个值,该值指示消息是否是事务中发送的最后一条消息。 | 
| Label | 获取或设置描述消息的应用程序定义的 Unicode 字符串。 | 
| LookupId | 在 MSMQ 3.0 中引入。 获取消息的查找标识符。 | 
| MessageType | 获取消息类型: | 
| Priority | 获取或设置消息优先级,该优先级用于确定消息在队列中的位置。 | 
| Recoverable | 获取或设置一个值,该值指示在出现计算机故障或网络问题时是否保证传递消息。 | 
| ResponseQueue | 获取或设置接收应用程序生成的响应消息的队列。 | 
| SecurityContext | 获取或设置消息的安全上下文。 | 
| SenderCertificate | 获取或设置用于验证消息的安全证书。 | 
| SenderId | 获取发送用户的标识符。 | 
| SenderVersion | 获取用于发送消息的消息队列版本。 | 
| SentTime | 获取发送计算机上的源队列管理器发送消息的日期和时间。 | 
| Site | (继承自 Component) | 
| SourceMachine | 获取发出消息的计算机。 | 
| TimeToBeReceived | 获取或设置要从目标队列接收消息所需的最长时间。 | 
| TimeToReachQueue | 获取或设置消息到达队列所用的最长时间。 | 
| TransactionId | 获取曾包含消息的事务的标识符。 | 
| TransactionStatusQueue | 获取源计算机中的事务状态队列。 | 
| UseAuthentication | 获取或设置一个值,该值指示消息发送前是否验证了(或是否必须验证)。 | 
| UseDeadLetterQueue | 获取或设置一个值,该值指示是否将未能传递的消息的副本发送到死信队列。 | 
| UseEncryption | 获取或设置一个值,该值指示是否使消息成为私有的。 | 
| UseJournalQueue | 获取或设置一个值,该值指示是否在始发计算机的计算机日记中保留消息的副本。 | 
| UseTracing | 获取或设置一个值,该值指示是否在消息向目标队列移动的过程中跟踪消息。 | 
方法
| CreateObjRef(Type) | 创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。(继承自 MarshalByRefObject) | 
| Dispose() | 释放由 Component 使用的所有资源。(继承自 Component) | 
| Dispose(Boolean) | 释放由 Component 占用的非托管资源,还可以另外再释放托管资源。(继承自 Component) | 
| Equals(Object) | 确定指定对象是否等于当前对象。(继承自 Object) | 
| GetHashCode() | 作为默认哈希函数。(继承自 Object) | 
| GetLifetimeService() | 
		已过时.
	 检索控制此实例的生存期策略的当前生存期服务对象。(继承自 MarshalByRefObject) | 
| GetService(Type) | 返回一个对象,该对象表示由 Component 或它的 Container 提供的服务。(继承自 Component) | 
| GetType() | 获取当前实例的 Type。(继承自 Object) | 
| InitializeLifetimeService() | 
		已过时.
	 获取生存期服务对象来控制此实例的生存期策略。(继承自 MarshalByRefObject) | 
| MemberwiseClone() | 创建当前 Object 的浅表副本。(继承自 Object) | 
| MemberwiseClone(Boolean) | 创建当前 MarshalByRefObject 对象的浅表副本。(继承自 MarshalByRefObject) | 
| ToString() | 返回包含 Component 的名称的 String(如果有)。 不应重写此方法。(继承自 Component) | 
事件
| Disposed | 在通过调用 Dispose() 方法释放组件时发生。(继承自 Component) |