Tuesday, May 27, 2008

Retrieving an eConnect XML Message from MSMQ

A common issue with eConnect's development is the ability to retrieve messages extracted with the eConnect Requester service from an MSMQ queue. While most developers will be able to setup the Requester Service to extract data from Microsoft Dynamics GP, most will find it hard to retrieve those messages from their integrating solutions. The following VB.NET code shows a quick and efficient way of doing just that.


Private Sub GetMessage()
'Create queue object to retrieve messages from the default outgoing queue
Dim MyQueue As New MessageQueue(".\private$\econnect_outgoing9")

'Create an MSMQ formatter and transaction objects
MyQueue.Formatter = New ActiveXMessageFormatter

Dim MyTransaction As New MessageQueueTransaction

'Create a message object
Dim MyMessage As Message

Try
'Retrieve a message from the queue
'This example assumes there is always a message waiting in the queue

MyTransaction.Begin()
MyMessage = MyQueue.Receive(MyTransaction)

MyTransaction.Commit()

'Retrieve the string from the message

Dim MyDocument As [String] = CType(MyMessage.Body, [String])
'Load the string into an XML document object

Dim MyXml As New XmlDocument
MyXml.LoadXml(MyDocument)

'Display the XML from the queue message
MessageText.Text = MyXml.InnerXml
Catch err As SystemException

ErrorText.Text = err.InnerException.ToString()
End Try
End Sub


To summarize, it will be necessary to perform the following steps to extract a message from an MSMQ queue with your .NET application:

1) Create the queue object pointing to the eConnect queue
2) Create an MSMQ formatter and the correspoding transaction objects
3) Create a Message object
4) Retrieve the message from the queue
5) Retrieve the particular strings from within the message

From there on you can do anything with the information retrieved. In my example, I just chose to display the XML content of the message, but you can chose to pass this information to your application to perform any actions or store into some database table along with other info.

Until next post!

MG.-
Mariano Gomez, MIS, MCP, PMP
Maximum Global Business, LLC
http://www.maximumglobalbusiness.com/

3 comments:

Carlos said...

This example works fine, but how about retrieving multiple messages or looping through the message queue. I have the exact same code, but the receive keeps waiting for a message when the Q is empty.

Carlos

Scott said...

I have no problems getting the xml document from the Outgoing Service's MSMQ, but I can't seem to get it deserialized back into an econnect object. How does one accomplish this?

And for the person who was asking about getting several items you would want to use the MessageEnumerator like so:

Dim myQueue As New MessageQueue("FormatName:Direct=TCP:192.168.210.138\private$\econnect_outgoing")
myQueue.Formatter = New ActiveXMessageFormatter
Dim myMessage As New Message
Dim eConnect As New eConnectType
Dim serializer As New XmlSerializer(GetType(eConnectType))
Dim invmast As New IVItemMasterType
Try
Dim menum As MessageEnumerator = myQueue.GetMessageEnumerator2
While menum.MoveNext(New TimeSpan(0, 0, 1))
myMessage = menum.Current

Mariano Gomez said...

Scott,

Thanks for your question. Please refer to the following Incoming Service example at:

http://msdn.microsoft.com/en-us/library/aa973880(v=MSDN.10).aspx

Please let me know if I can be of further assistance.