Home All Groups Group Topic Archive Search About

About MessageQueue Problem

Author
26 Jun 2005 10:56 AM
Risen
Hi,All,

I read MSDN about MessageQueue,and then I want to write some code to test
MessageQueue in Vb.Net 2003. But there are some errors in code,and I don't
know which code are incorrect. Who can tell me how to correct it. Thanks a
lot.


Risen.

-------------------------------------------------------------------
Pls see my code as below:


Imports System
Imports System.Messaging

Public Class Form1
    Inherits System.Windows.Forms.Form

......
......

    Public Shared Sub EnsureQueueExists(ByVal path As String)
        If Not MessageQueue.Exists(path) Then
            MessageQueue.Create(path)
        End If
    End Sub 'EnsureQueueExists

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim queuePath As String = ".\private$\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)

        Dim orderRequest As New Order
        orderRequest.itemId = 1025
        orderRequest.quantity = 5
        orderRequest.address = "One Microsoft Way"

        queue.Send(orderRequest)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
        Console.WriteLine("Processing Orders")

        Dim queuePath As String = ".\private$\orders"
        EnsureQueueExists(queuePath)
        Dim queue As New MessageQueue(queuePath)
        CType(queue.Formatter, XmlMessageFormatter).TargetTypeNames = New
String() {"Order"}

        While True
            Dim newOrder As Order = CType(queue.Receive().Body, Order)  '<-
Error!! But I don't know the reason that causes it.
            newOrder.ShipItems()
        End While

    End Sub
End Class

Public Class Order

    Public itemId As Integer
    Public quantity As Integer
    Public address As String


    Public Sub ShipItems()

        Console.WriteLine("Order Placed:")
        Console.WriteLine(ControlChars.Tab & "Item ID  : {0}", itemId)
        Console.WriteLine(ControlChars.Tab & "Quantity : {0}", quantity)
        Console.WriteLine(ControlChars.Tab & "Ship To  : {0}", address)

        ' Add order to the database.
        ' Insert code here.

    End Sub 'ShipItems
End Class 'Order

Author
26 Jun 2005 1:16 PM
Peter van der Goes
"Risen" <rise***@21cn.com> wrote in message
news:uw7Y$3jeFHA.640@tk2msftngp13.phx.gbl...
> Hi,All,
>
> I read MSDN about MessageQueue,and then I want to write some code to test
> MessageQueue in Vb.Net 2003. But there are some errors in code,and I don't
> know which code are incorrect. Who can tell me how to correct it. Thanks a
> lot.
>
>
> Risen.
>
<snip>
Can you quote the error messages you are getting and indicate to which lines
in your code they apply?
That would be very helpful to those interested in responding to your
question.

--
Peter [MVP Visual Developer]
Jack of all trades, master of none.
Author
26 Jun 2005 1:50 PM
Risen
Hi,Peter,

When app run at line "Dim newOrder As Order = CType(queue.Receive().Body,
Order)", the app has no response, and it does not show error messages. You
can test it in your own vs.net. I don't know what's wrong with it. Thanks a
lot.


Risen.


Show quoteHide quote
> "Risen" <rise***@21cn.com> wrote in message
> news:uw7Y$3jeFHA.640@tk2msftngp13.phx.gbl...
>> Hi,All,
>>
>> I read MSDN about MessageQueue,and then I want to write some code to test
>> MessageQueue in Vb.Net 2003. But there are some errors in code,and I
>> don't know which code are incorrect. Who can tell me how to correct it.
>> Thanks a lot.
>>
>>
>> Risen.
>>
> <snip>
> Can you quote the error messages you are getting and indicate to which
> lines in your code they apply?
> That would be very helpful to those interested in responding to your
> question.
>
> --
> Peter [MVP Visual Developer]
> Jack of all trades, master of none.
>
Author
26 Jun 2005 3:19 PM
David Browne
"Risen" <rise***@21cn.com> wrote in message
news:%23gZ63XleFHA.1400@TK2MSFTNGP15.phx.gbl...
> Hi,Peter,
>
> When app run at line "Dim newOrder As Order = CType(queue.Receive().Body,
> Order)", the app has no response, and it does not show error messages. You
> can test it in your own vs.net. I don't know what's wrong with it. Thanks
> a lot.
>

The app does throw an error, but your test program is just badly designed.
You should catch and display the exception.  Also it's easier to use a
Console Application as a test harness because it's completely
self-contained.  I could just paste it into a new program and run it to
reproduce your error.  There was nothing serious in your program, and with
better testing and debugging practices you would have gotten it working on
your own.

Anyway here's a proper test program with your problems fixed.

Imports System.Messaging
Public Class Order

    Public itemId As Integer
    Public quantity As Integer
    Public address As String


    Public Sub ShipItems()

        Console.WriteLine("Order Placed:")
        Console.WriteLine(ControlChars.Tab & "Item ID  : {0}", itemId)
        Console.WriteLine(ControlChars.Tab & "Quantity : {0}", quantity)
        Console.WriteLine(ControlChars.Tab & "Ship To  : {0}", address)

        ' Add order to the database.
        ' Insert code here.

    End Sub 'ShipItems
End Class 'Order
Module Module1


    Sub Main()
        Try
            Send()
            Console.WriteLine("Send OK")

            Recieve()
            Console.WriteLine("Recieve OK")
        Catch ex As Exception
            Console.WriteLine(ex.ToString)

        End Try



    End Sub
    Sub Send()

        Dim queuePath As String = ".\private$\orders"
        EnsureQueueExists(queuePath)


        Dim queue As New MessageQueue(queuePath)
        queue.Formatter = New XmlMessageFormatter(New Type()
{GetType(Order)})


        Dim orderRequest As New Order
        orderRequest.itemId = 1025
        orderRequest.quantity = 5
        orderRequest.address = "One Microsoft Way"

        queue.Send(orderRequest)

    End Sub
    Sub Recieve()

        Dim queuePath As String = ".\private$\orders"
        Dim queue As New MessageQueue(queuePath)
        queue.Formatter = New XmlMessageFormatter(New Type()
{GetType(Order)})

        For Each msg As Message In queue
            Dim newOrder As Order = CType(msg.Body, Order)
            newOrder.ShipItems()
        Next

    End Sub

    Sub EnsureQueueExists(ByVal queuePath As String)
        If Not System.Messaging.MessageQueue.Exists(queuePath) Then
            MessageQueue.Create(queuePath)
        End If
    End Sub

End Module
Author
27 Jun 2005 9:04 AM
Risen
Thank you very much for your help. I will try according to what you said.


Show quoteHide quote
"David Browne" <davidbaxterbrowne no potted m***@hotmail.com> дÈëÏûÏ¢ÐÂÎÅ:ebvj0JmeFHA.***@TK2MSFTNGP10.phx.gbl...
>
> "Risen" <rise***@21cn.com> wrote in message
> news:%23gZ63XleFHA.1400@TK2MSFTNGP15.phx.gbl...
>> Hi,Peter,
>>
>> When app run at line "Dim newOrder As Order = CType(queue.Receive().Body,
>> Order)", the app has no response, and it does not show error messages.
>> You can test it in your own vs.net. I don't know what's wrong with it.
>> Thanks a lot.
>>
>
> The app does throw an error, but your test program is just badly designed.
> You should catch and display the exception.  Also it's easier to use a
> Console Application as a test harness because it's completely
> self-contained.  I could just paste it into a new program and run it to
> reproduce your error.  There was nothing serious in your program, and with
> better testing and debugging practices you would have gotten it working on
> your own.
>
> Anyway here's a proper test program with your problems fixed.
>
> Imports System.Messaging
> Public Class Order
>
>    Public itemId As Integer
>    Public quantity As Integer
>    Public address As String
>
>
>    Public Sub ShipItems()
>
>        Console.WriteLine("Order Placed:")
>        Console.WriteLine(ControlChars.Tab & "Item ID  : {0}", itemId)
>        Console.WriteLine(ControlChars.Tab & "Quantity : {0}", quantity)
>        Console.WriteLine(ControlChars.Tab & "Ship To  : {0}", address)
>
>        ' Add order to the database.
>        ' Insert code here.
>
>    End Sub 'ShipItems
> End Class 'Order
> Module Module1
>
>
>    Sub Main()
>        Try
>            Send()
>            Console.WriteLine("Send OK")
>
>            Recieve()
>            Console.WriteLine("Recieve OK")
>        Catch ex As Exception
>            Console.WriteLine(ex.ToString)
>
>        End Try
>
>
>
>    End Sub
>    Sub Send()
>
>        Dim queuePath As String = ".\private$\orders"
>        EnsureQueueExists(queuePath)
>
>
>        Dim queue As New MessageQueue(queuePath)
>        queue.Formatter = New XmlMessageFormatter(New Type()
> {GetType(Order)})
>
>
>        Dim orderRequest As New Order
>        orderRequest.itemId = 1025
>        orderRequest.quantity = 5
>        orderRequest.address = "One Microsoft Way"
>
>        queue.Send(orderRequest)
>
>    End Sub
>    Sub Recieve()
>
>        Dim queuePath As String = ".\private$\orders"
>        Dim queue As New MessageQueue(queuePath)
>        queue.Formatter = New XmlMessageFormatter(New Type()
> {GetType(Order)})
>
>        For Each msg As Message In queue
>            Dim newOrder As Order = CType(msg.Body, Order)
>            newOrder.ShipItems()
>        Next
>
>    End Sub
>
>    Sub EnsureQueueExists(ByVal queuePath As String)
>        If Not System.Messaging.MessageQueue.Exists(queuePath) Then
>            MessageQueue.Create(queuePath)
>        End If
>    End Sub
>
> End Module
>
>