如何:在 .NET Compact Framework 中使用 MSMQ

更新:2007 年 11 月

创建使用消息队列(也称为 MSMQ)的 .NET Compact Framework 应用程序与 .NET Framework 中使用的过程相似。不过,对于 .NET Compact Framework 中的 MSMQ 中描述的功能,Windows CE 并非全都支持。

下面的代码示例演示如何在同一设备上创建消息队列、将消息发送到队列以及从队列接收消息。不需要任何网络连接。示例中使用 Order 这样一个简单类来创建消息队列的对象。

这些示例假定消息队列已安装在设备上。有关获取消息队列组件的更多信息,请参见 .NET Compact Framework 中的 MSMQ

定义 Order 类

  • 将以下类添加到项目中。

    ' This class represents an object that
    ' is sent to and received from the queue.
    Public Class Order
        Dim ID As Integer
        Dim DTime As DateTime
        Public Property orderID() As Integer
            Get
                Return Me.ID
            End Get
            Set(ByVal value As Integer)
                Me.ID = value
            End Set
        End Property
        Public Property orderTime() As DateTime
            Get
                Return Me.DTime
            End Get
            Set(ByVal value As DateTime)
                Me.DTime = value
            End Set
        End Property
    End Class
    

创建消息队列

  • 向窗体中添加以下方法。

    Private Sub CreateQueue()
        ' Determine whether the queue exists.
        If Not MessageQueue.Exists(QPath) Then
            Try
                ' Create the queue if it does not exist.
                myQ = MessageQueue.Create(QPath)
                MessageBox.Show("Queue Created")
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Else
            MessageBox.Show("Queue Exists")
        End If
    End Sub
    

向队列发送消息

  • 向窗体中添加以下方法。

    Private Sub SendMessageToQueue()
        ' Create a new order and set values.
        Dim sendOrder As New Order()
        sendOrder.orderID = 23123
        sendOrder.orderTime = DateTime.Now
        Try
            myQ.Send(sendOrder)
            MessageBox.Show("Message Sent")
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
    

从队列接收消息

  • 向窗体中添加以下方法。

    Private Sub ReceiveMessageFromQueue()
        ' Connect to the a queue on the device.
        myQ = New MessageQueue(QPath)
    
        ' Set the formatter to indicate the body contains an Order.
        Dim targetTypes() As Type
        targetTypes = New Type() {GetType(Order)}
        myQ.Formatter = New XmlMessageFormatter(targetTypes)
        Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQ.Receive()
            Dim myOrder As Order = CType(myMessage.Body, Order)
    
            ' Display message information.
            MessageBox.Show("Order ID: " & _
                myOrder.orderID.ToString() & _
                    Chr(10) & "Sent: " & myOrder.orderTime.ToString())
    
        Catch m As MessageQueueException
            ' Handle Message Queuing exceptions.
            MessageBox.Show(m.Message)
        Catch e As InvalidOperationException
            ' Handle invalid serialization format.
            MessageBox.Show(e.Message)
        End Try
    End Sub
    

测试消息队列

  1. 向窗体中添加标签为“发送”的按钮,用于调用 CreateQueue 和 SendMessageToQueue 方法。

  2. 向窗体中添加标签为“接收”的按钮,用于调用 ReceiveMessageFromQueue 方法。

编译代码

此示例需要引用下面的命名空间:

请参见

任务

MSMQ 丛书订购应用程序示例

概念

.NET Compact Framework 中的 MSMQ

.NET Compact Framework 帮助主题

消息队列和消息处理技术背景知识