About MessageQueue Problem

R

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
 
P

Peter van der Goes

Risen said:
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.
 
R

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.
 
D

David Browne

Risen said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top