0

I use BizTalk 2016 and have configured default settings in BizTalk MSMQ adapter (body type 8209).

I am trying to receive message from c# code but get the following exception:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Messaging.dll

Additional information: Cannot deserialize the message passed as an argument. Cannot recognize the serialization format.

Code used (slimmed):

message = messageQueue.Receive();
message.Formatter = new ActiveXMessageFormatter();
document.Load(message.Body.ToString());

Exception is thrown when I access the Body property of the message, which triggers the formatter to access the message content.

I have tried to specify a formatter, and tried a few different types, but they are not working. I fear that there are som Byte Order Marks on the data, that needs to be removed manually. Is that really the case?

I would assume this need is quite common, strange to get stuck on this...!? Please put me on track on this!

JERKER
  • 907
  • 8
  • 17

2 Answers2

0

Ended up using XmlDocument, and it took care of BOM and everything:

XmlDocument document = new XmlDocument();
document.Load(message.BodyStream);

This case I did not need a MessageFormatter. :)

JERKER
  • 907
  • 8
  • 17
0

Alternative solution not using XmlDocument. Trick is to use BodyStream to avoid for any formatter to be initialized:

MessageQueue messageQueue = new MessageQueue(@".\private$\test");

System.Messaging.Message message = new System.Messaging.Message();
message.BodyType = 8209;
message = messageQueue.Receive();

using (FileStream fileStream = File.Create(@"C:\TEMP\output.xml"))
{
  message.BodyStream.Seek(0, SeekOrigin.Begin);
  message.BodyStream.CopyTo(fileStream);
}

This code works using UTF-8 with special characters as input/output.

JERKER
  • 907
  • 8
  • 17