Service Broker message_body error when casting binary data to xml in C#

Posted by TimBuckTwo on Stack Overflow See other posts from Stack Overflow or by TimBuckTwo
Published on 2010-05-07T23:55:02Z Indexed on 2010/05/07 23:58 UTC
Read the original article Hit count: 370

Filed under:
|
|
|
|

I am using Message Broker with Sql server 2008, and designing an External Activator service to consume messages from my target queue.

My Problem: Cant cast the returned message body from the SqlDataReader object:

"WAITFOR (RECEIVE TOP(1) conversation_handle, message_type_name, message_body FROM [{1}]), TIMEOUT {2}" operation, I cant cast the binary data to XML in C#

SqlBinary MessageBody = reader.GetSqlBinary(2);                                            
MemoryStream memstream = new MemoryStream();
XmlDocument xmlDoc = new XmlDocument();
memstream.Write(MessageBody.Value, 0, MessageBody.Length);
memstream.Position= 0;
//below line Fails With Error:{"Data at the root level is invalid. Line 1, position 1."}
xmlDoc.LoadXml(Encoding.ASCII.GetString(memstream.ToArray()));
    memstream.Close();

To prevent poison message I do not use CAST(message_body as XML), Any suggestions would be greatly appreciated.

© Stack Overflow or respective owner

Related posts about service

Related posts about broker