Reading the message body and context from the BizTalkDTADb using operations library in BizTalk 2006

June 4th, 2007

Update 2007-07-05: The example project used in the post can be downloaded from here.

The operations dll (Micrsoft.BizTalk.Operations) is one of the new bits in BizTalk 2006 that at least I haven’t heard much about (most of you probably find in the C:\Program Files\Microsoft BizTalk Server 2006 folder). However it’s a library with a lot of useful functionality.

In this post I’ll focus on how it’s possible to use the library to get hold of message parts and message context from the BizTalk tracking database (usually called the BizTalkDTADb). If you’re unfamiliar with the architecture of the tracking in BizTalk this article is a good place to start.

What used to be the problem?

In BizTalk 2004 one soon got into problems when trying to read the message parts and the message context from the tracking database. The problem is that the the Xml is compressed (something that makes totally sence – Xml is a perfect candidate for compression). To my knowledge no one has found a good way to decompress the message context. There’s however a way to decompress the message parts (and only the parts) that Rob posted on in the BizTalk newsgroup. But this method doesn’t work on the compressed context of the same message that the parts belong to! One could assume that the same method could be used for both compression and decompression but I haven’t got it to work for the context of the message (read about my frustration here)

What’s wrong with WMI and MSBTS_TrackedMessageInstance?

So the option that was left to us before BizTalk 2006 for reading all the message parts as well as the message context was to use the MSBTS_TrackedMessageInstance WMI scrtipt and save everything down to file. The problem with this approach is that it’s slow and ugly! Say that we like to save a couple of thousand messages from the tracking database. When forced to save the messages to file we’ll end up with a slow and ugly solution where writing to file takes ages, then we have to read the content of the different files before cleaning up and deleting the files.

Micrsoft.BizTalk.Operations to the rescue

A quick view in Lutz Roeder’s Reflector shows us a couple of interesting methods in the operations dll. However I’ll only use the GetTrackedMessage method in this post.

operations  

I’ve written a tiny test application that uses the library to read the body part of the message (the meat of the message) and a couple of properties I’m interested in from the context of the message. It looks something like this. If your interested drop me an email and I’ll send it. The first screenshot below shows an example of reading the InterchangeID from the context of a tracked message.

OperationsTest2  

This screenshot shows an example of reading the full body of a tracked message from the tracking database. Try doing this with less then 10 lines of code using the MSBTS_TrackedMessageInstance script!

OperationsTest1  

 There is really nothing to the application, reading a tracked message using the MessageID will return an object implementing IBaseMessage and we basically read the BodyPart property of - it really couldn’t be any easier.

public static string GetMessageBodyByMessageID(string dbServer, string dbName, Guid messageID) { TrackingDatabase dta = new TrackingDatabase(dbServer, dbName); BizTalkOperations operations = new BizTalkOperations(); IBaseMessage message = operations.GetTrackedMessage(messageID, dta); string body = string.Empty; using(StreamReader streamReader = new StreamReader(message.BodyPart.Data)) { body = streamReader.ReadToEnd(); } return body; }

I’d be very interested in how people use the operations library both when it comes to read and use tracked messages, but also in other ways! Use the comments to tell me and other readers how you use it your solutions!

37 Responses to “Reading the message body and context from the BizTalkDTADb using operations library in BizTalk 2006”

  1. Sujesh Kodoth Says:

    Really useful. Can you please send me you sample application with source code?

    thanks

  2. Richard Says:

    Sujesh,

    I’ve sent it to you!

    Richard

  3. ElreyRonald Says:

    Hi Sujesh, You blog has caught my interest since I am trying to wrap my head around BizTalk. Can you also send me the application. Thanks -ElreyRonald

  4. Richard Says:

    ElreyRonald,

    It should be in your inbox!

  5. Richard Says:

    Test

  6. Aaron Kim Says:

    Very useful indeed! I’d have to save body messages to DB for monitoring. Would you please send me the sample and code as well? Greatly appreciated! Thanks!

  7. Richard Says:

    Aaron,

    Sent it to you. If you find time I’d love to hear more about your monitoring! I’d love a short technical description.

    Richard

  8. Robert F Says:

    Richard, exactly what I’m looking for. Could you please send me the source code.

    Thanks in advance.

  9. Sune Nielsen Says:

    can you send it to me to. need to create a app. that queries out messages based on props. and then get the body

  10. Srinivas K Says:

    Hi
    Please send me your sample application with source code?
    Thanks in advance.
    Srini

  11. Saravana Kumar Says:

    Hi Richard,

    This post looks very helpful. Could you kindly mail me the utility.

    thanks,
    Saravana Kumar

  12. Jack Hutchinson Says:

    Nice Tool. Could you also please send me the sample and code as well? Thanks!

  13. Richard Says:

    I’ve now done what I should have done in the first place – uploaded the project to the server. Download it here

  14. RyanElee Says:

    hi Richard,

    your post is very helpful for me,but when i use the GetTrackedMessage method, the system throwed out a exception as below:

    The Operations artifact corresponding to the input parameters was not found.

    I directly run your test program on my computer and another one, it is same issue too. I use biztalk 2006 & sql server 2005.

    Hope get your help.

    THanks in advance

  15. Richard Says:

    @RyanElee: Sound to me like the item your trying to bring up tracking info about from the DTADb actually isn’t tracked. This could be due the fact that you haven’t enabled tracking of the message body?

  16. Abe Powell Says:

    This Looks Interesting,

    I would love a copy of the tool you have created. I have typically developed solutions to use self created DB’s for tracking/message retrieval purposes. I would love to be able to use the built in TRacking DB for some of this.

    Thanks

  17. RyanElee Says:

    Richard,

    Do you mean I must track a message, thus I can get the tracked message body by GetTrackedMessage method?

    But how wondering I encounterred this issue that the input parameters was not found.

    Regard

  18. Richard Says:

    @RyanElee: The “The Operations artifact corresponding to the input parameters was not found.” message indicates that the artifact in the DTADb that corresponds to the id you hand the GetTrackedMessage method (as a parameter) doesn’t exists.

    This probably means that you haven’t enabled tracking of the message body on the port that the message uses. You do this by right click on the port in the BizTalk administration console and choose “Tracking …”. You then get prompted by a message box where it’s possible to enable tracking of message bodies at different stages of the port process.

  19. Richard Says:

    @Abe Powell: You find it here.

  20. RyanElee Says:

    Richard,

    Thanks very much.

    Before you answer me , I found I can get the suspended instance’s message body from BizTalkMsgBoxDb by the GetMessage method in BizTalkOperations class. The complete instance’s message won’t to be preserved into the BizTalkMsgBoxDb, right? I mean if i want to get the historic instances’ message body, i have to set the tracking property in the biztalk administrator.

    Thanks

  21. Raja Kumaravel Says:

    It’s really cool application. Let me get your sample application with source code.
    Please!

    Thanks,
    Raja Kumaravel

  22. Charles Says:

    David:

    The HAT interface has the option to track all messages. I believe its referring to the muliple parts of the message.

    But yet it save the many parts of the same message. How do I select multiple messages and save a bunch of messages to the disk?

    Thanks in anticipation

    Charlesd

  23. Richard Says:

    @Charles: Send me an email and explain a but further what it is you wanna do and I’m sure we’ll find a solution. I don’t really understand what it is you asking about.

  24. Marcio Ribas Says:

    Hi Richard.

    Man the download link is broke, can you, please, send me a copy of your program? I have try but I’m receiving this error: “Failed to connect to the BizTalkDTADb database on the local server” (I’m using Biztalk 2006 and SQL Server 2005).

    Thanks for any help.

  25. Eugene Says:

    Richard, could you please send me your sample application with source code? The link is dead.
    Thanks in advance.

  26. Marcus Says:

    Hi Richard!

    Great article, but the sample code is unavailable unfortunately. I’m stuck with what I believe is an error due to missing a dll or so, so looking at the sample would help me out a lot.

  27. My Message Tracking Management Prayers have been Answered! - Rich Wallace Says:

    [...] new savior is…Richard Hallgren; great first name by the [...]

  28. Kavitha Says:

    Hi richard,

    This is very usefull indeed, please send me the sample application and source code.

    Thanks in Advance,
    Kavitha

  29. Cristina Says:

    Almost an year later, this post is still a sanity saver. Thank you very much for the sample code! We needed the EDI message in its original form inside an orchestration, and looking it up in the DTA database the old-fashioned way proved a lot more difficult than anticipated…

  30. Mehdi Etehadi Says:

    My problem is:
    I have a installed BTS and it is working for a while. Now I need all information about messages which are passed through BTS, but unfortunately I didn’t create neither BAM activity nor message tracking at ports.
    Now I think the best place to find tracking information is Log files.
    But I don’t know where they are .
    I hope that I could explain my idea.
    Thank you for attention .

  31. Richard Says:

    @Mehdi

    BizTalk is really “slimmed down” when it comes to logging content of messages by default. All to make it perform better when moving messages around. You’ll have to either track it using BAM, your own logging or by using the built in message logging. Otherwise no content of the message will be logged or stored. To my knowledge you can’t get your past traffic if you haven’t configured to use any of the above tracking options.

  32. Fat64 Says:

    Hallo Richard!
    I tried to use your code from this page to get messge body and I had an error in

    BizTalkOperations operations = new BizTalkOperations();

    The error is:
    System.ArgumentNullException was unhandled by user code
    Message=”Value cannot be null.\r\nParameter name: dbName”
    Source=”Microsoft.BizTalk.Operations”
    ParamName=”dbName”
    StackTrace:
    at Microsoft.BizTalk.Operations.BizTalkDatabase.CheckInputParams(String dbServer, String dbName)

    What do I wrong?

  33. Fat64 Says:

    The problem is solved. I had Biztalk Server 2006, not 2006 R2. With R2 works fine. Thank you!

  34. 3 ways of programmatically extracting a message body from the BizTalk tracking database « Connected Thoughts - Thiago Almeida Says:

    [...] the great post mentioning this method by Richard Hallgren: http://www.richardhallgren.com/reading-the-message-body-and-context-from-the-biztalkdtadb-using-oper... 2. Use the WMI MSBTS_TrackedMessageInstance.SaveToFile method to save the instance to disk. This [...]

  35. Umesh Says:

    Hi,
    I want to read body and attachment(all attachment) from my mail box.
    Currently i am reading my body as an xml file but i am not able to read body and attachment at a time .

    I taken Body part content type- Text/Xml and Body part index=1

    Please help me in this issue.

    Also i want to know can i read only body as a text ,i dont want it in xml file.

    Can any one help me ?

  36. Dan Says:

    ey Man, very useful..
    I just want to know what is de messageID… I using the function inside a component and this component is called from an Orchestration… but still I dont know what message property I need to send to the function.. I tried with BTS.MessageId but doesnt work…. do you know what message property is the one?

    thanks in advanced

  37. Richard Says:

    @Dan: Sounds you’re using the right property. Are you sure you have activated DTA tracking in the ports?

Leave a Reply