locked
SQL Service Broker message validation not working (well_formed_xml) RRS feed

  • Question

  • Hi, I can´t seem to get the message validation working in my service broker queue.

    I am using SQL Server 2008 with Service Broker enabled in my database.

    When I run the following script I don´t get any error messages and the queue transports the message successfully although the message sent is not a well formed xml.

    My script to set up the queue:

    CREATE MESSAGE TYPE [//TestMessage] AUTHORIZATION [dbo] VALIDATION = WELL_FORMED_XML
    CREATE CONTRACT [//TestContract] AUTHORIZATION [dbo] ([//TestMessage] SENT BY INITIATOR)
    CREATE QUEUE [TestSendQueue] WITH STATUS = ON , RETENTION = OFF  ON [PRIMARY]
    CREATE QUEUE [TestReceiveQueue] WITH STATUS = ON , RETENTION = OFF  ON [PRIMARY]
    CREATE SERVICE [//TestSendService]  AUTHORIZATION [dbo]  ON QUEUE [TestSendQueue] ([//TestContract])
    CREATE SERVICE [//TestReceiveService]  AUTHORIZATION [dbo]  ON QUEUE [TestReceiveQueue] ([//TestContract])


    --this sends a ill-formed xml and the engine returns: "Command(s) completed successfully." as message when I execute

    declare @InitDlgHandle uniqueidentifier
    BEGIN DIALOG @InitDlgHandle
             FROM SERVICE [//TestSendService]
             TO SERVICE N'//TestReceiveService'
             ON CONTRACT [//TestContract]
             WITH
                 ENCRYPTION = OFF;

        SEND ON CONVERSATION @InitDlgHandle
             MESSAGE TYPE [//TestMessage]
              ('This is not a valid xml!');

    -- this query successfully receives the ill-formed xml message
      receive cast(message_body as xml) as message  from TestReceiveQueue

    Please help. Am i missing something in my scripts? I think my begin dialog should report an error when I try to send my message, did I not understand the expected functionality?

    Regards,
    Per Edgren

     


    /nugget
    Wednesday, May 12, 2010 11:17 AM

Answers

  • The problem here is that the term "well_formed_xml" as defined by the XML standards is actually much more permissive than most people realize, because it allows not only XML Documents, but also XML Fragments.  

    That doesn't sound like a big deal at first, after all it just means that it does not require a single root element.  However, the result of that is that this allows ANY other type of XML tag at the top-level, including, (wait for it ... ) raw text.  Yep, because

      "< root >Any random entitized raw text here< /root >"

    is a valid XML document, that also means that:

     

      "Any random entitized raw text here"

    is a valid XML fragment.

     

    ---------

    So, to conclude, the reason you are not getting any errors from your invalid XML message is because it is not in fact invalid, rather it is a perfectly valid XML fragment.  Try the following SEND commands with your example to see this demonstrated:

     

     SEND ON CONVERSATION @InitDlgHandle
     MESSAGE TYPE [//TestMessage]
     ('<tag>This is not a valid xml! (because it has an unterminated tag)');
    
     SEND ON CONVERSATION @InitDlgHandle
     MESSAGE TYPE [//TestMessage]
     ('This actually IS a valid xml! (because it is a text fragment)');
    

     


    -- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
    Proactive Performance Solutions, Inc.
    "Performance is our middle name."

    Please! Remember to Vote all helpful replies as Helpful
    • Proposed as answer by Pawel Marciniak Monday, May 17, 2010 5:15 PM
    • Marked as answer by nugget2 Tuesday, May 18, 2010 6:53 AM
    Monday, May 17, 2010 2:57 PM

All replies

  • Service Broker only validates incoming messages, not outgoing (see Remarks in http://msdn.microsoft.com/en-us/library/ms187744.aspx ). Therefore what should happen in your case is that SEND should succeed, but when the message reaches the target service, it should automatically send an error message to the initiator service. Therefore you shouldn't be able to receive the message. Please double check that your receive statement actually receives the malformed message (the fact that it returns without errors doesn't prove anything).
    Wednesday, May 12, 2010 4:03 PM
  • Hi Pawel!

    Thank you for your response. I have been monitoring the the initiator service and there are no error messages sent back. Also if you try my supplied script you will be able to both successfully send a ill-formed xml to the iniator and retrieve the same message on the recieving end without any error messages.

    If this is the intended behaviour I don´t see the point with message validation. (since it doesn´t validate or react at all)

    Regards,
    Per


    /nugget
    Friday, May 14, 2010 6:23 AM
  • The problem here is that the term "well_formed_xml" as defined by the XML standards is actually much more permissive than most people realize, because it allows not only XML Documents, but also XML Fragments.  

    That doesn't sound like a big deal at first, after all it just means that it does not require a single root element.  However, the result of that is that this allows ANY other type of XML tag at the top-level, including, (wait for it ... ) raw text.  Yep, because

      "< root >Any random entitized raw text here< /root >"

    is a valid XML document, that also means that:

     

      "Any random entitized raw text here"

    is a valid XML fragment.

     

    ---------

    So, to conclude, the reason you are not getting any errors from your invalid XML message is because it is not in fact invalid, rather it is a perfectly valid XML fragment.  Try the following SEND commands with your example to see this demonstrated:

     

     SEND ON CONVERSATION @InitDlgHandle
     MESSAGE TYPE [//TestMessage]
     ('<tag>This is not a valid xml! (because it has an unterminated tag)');
    
     SEND ON CONVERSATION @InitDlgHandle
     MESSAGE TYPE [//TestMessage]
     ('This actually IS a valid xml! (because it is a text fragment)');
    

     


    -- RBarryYoung, (302)375-0451 blog: MovingSQL.com, Twitter: @RBarryYoung
    Proactive Performance Solutions, Inc.
    "Performance is our middle name."

    Please! Remember to Vote all helpful replies as Helpful
    • Proposed as answer by Pawel Marciniak Monday, May 17, 2010 5:15 PM
    • Marked as answer by nugget2 Tuesday, May 18, 2010 6:53 AM
    Monday, May 17, 2010 2:57 PM
  • Thank you very much for your detailed explaination.

    Regards,
    Per


    /nugget
    Tuesday, May 18, 2010 6:54 AM