Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

MQ msg appears as XML

Status
Not open for further replies.

chiph

Programmer
Jun 9, 1999
9,878
US
Hey y'all, anybody done anything with MessageQueuing yet under .NET? Below is some code below that writes a simple "Hello World" message to a queue. When I inspect the message body (on the server), it shows up as an XML string.

Any idea how I can stop this? I've been looking at the String class to see if it has any properties to stop this, but haven't found anything yet.


[tt]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim QueueName As String
Dim q As System.Messaging.MessageQueue
Dim sTemp As String = "Hello World"

GetMQName(QueueName)

q = New System.Messaging.MessageQueue()
q.Path = QueueName
q.Send(sTemp, "Hello Label")
q.Close()

End Sub

Private Function GetMQName(ByRef MQName As String) As Boolean
Dim rk As RegistryKey = Registry.LocalMachine

GetMQName = True
Try
rk = rk.OpenSubKey("SOFTWARE\MyCompany\EvtSvc")
MQName = rk.GetValue("MQName")
Catch
GetMQName = False
End Try

End Function
[/tt]
 
OK, I solved what was happening.

It turns out that both the Message and MessageQueue have a "Formatter" property, which describes how the object should be serialized. The default is XmlMessageFormatter, but you also have a choice of ActiveXMessageFormatter and BinaryMessageFormatter.

XmlMessageFormatter does as what the name implies -- it formats the message as XML, and in my case surrounded my string's contents with <string> elements.

ActiveXMessageFormatter produces message contents compatible with previous users of MSMQ.

BinaryMessageFormatter I would assume does a direct binary copy of an object before placing it on the queue. I think this might be least useful in a .NET world, but would be handy if you have to interoperate with IBMs MQ Series.

You can also produce your own formatter by implementing the IMessageFormatter interface. Haven't investigated this (no need)

So I found that using the ActiveXMessageFormatter produced plain-jane strings in my Queue, which is what I wanted. Source code changes are below:
[tt]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim QueueName As String
Dim q As System.Messaging.MessageQueue
Dim m As System.Messaging.Message
Dim sTemp As String = &quot;Hello World&quot;

GetMQName(QueueName)

q = New System.Messaging.MessageQueue()
m = New System.Messaging.Message()

q.Path = QueueName
m.Formatter = New System.Messaging.ActiveXMessageFormatter()
m.Body = sTemp
q.Send(m, &quot;Hello Label&quot;)
q.Close()

End Sub
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top