How to: Use MSMQ in the .NET Compact Framework

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Creating .NET Compact Framework applications that use Message Queuing (also known as MSMQ) is similar to the process that is used in the .NET Framework. However, Windows CE does not support all the features described in MSMQ in the .NET Compact Framework.

The following code examples show how to create a message queue, send a message to the queue, and receive a message from the queue, all on the same device. No network connectivity is required. A simple class, Order, is used to create objects for Message Queuing.

These examples assume that Message Queuing has been installed on the device. For more information about obtaining the Message Queuing component, see MSMQ in the .NET Compact Framework.

To define the Order class

  • Add the following class to your project.

    ' 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
    

To create the message queue

  • Add the following method to your form.

    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
    

To send a message to the queue

  • Add the following method to your form.

    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
    

To receive the message from the queue

  • Add the following method to your form.

    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
    

To test message queuing

  1. Add a button to the form, labeled Send, that calls the CreateQueue and SendMessageToQueue methods.

  2. Add a button to the form, labeled Receive, that calls the ReceiveMessageFromQueue method.

Compiling the Code

This example requires references to the following namespaces:

See Also

Concepts

MSMQ in the .NET Compact Framework

.NET Compact Framework How-to Topics

Message Queues and Messaging Technology Backgrounder