Reading the message body and context from the BizTalkDTADb using operations library in BizTalk 2006
June 4th, 2007Update 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.
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.
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!
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!
June 5th, 2007 at 12:51 am
Really useful. Can you please send me you sample application with source code?
thanks
June 5th, 2007 at 7:14 am
Sujesh,
I’ve sent it to you!
Richard
June 6th, 2007 at 2:51 am
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
June 7th, 2007 at 2:31 pm
ElreyRonald,
It should be in your inbox!
June 7th, 2007 at 4:01 pm
Test
June 12th, 2007 at 2:02 am
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!
June 13th, 2007 at 11:42 pm
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
June 15th, 2007 at 11:07 am
Richard, exactly what I’m looking for. Could you please send me the source code.
Thanks in advance.
June 16th, 2007 at 11:33 pm
can you send it to me to. need to create a app. that queries out messages based on props. and then get the body
June 26th, 2007 at 6:15 am
Hi
Please send me your sample application with source code?
Thanks in advance.
Srini
June 29th, 2007 at 9:42 pm
Hi Richard,
This post looks very helpful. Could you kindly mail me the utility.
thanks,
Saravana Kumar
July 5th, 2007 at 2:38 pm
Nice Tool. Could you also please send me the sample and code as well? Thanks!
July 5th, 2007 at 2:49 pm
I’ve now done what I should have done in the first place – uploaded the project to the server. Download it here
July 23rd, 2007 at 2:10 pm
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
July 23rd, 2007 at 3:19 pm
@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?
July 23rd, 2007 at 6:02 pm
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
July 24th, 2007 at 2:54 am
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
July 24th, 2007 at 7:19 am
@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.
July 24th, 2007 at 7:20 am
@Abe Powell: You find it here.
July 24th, 2007 at 2:53 pm
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
August 4th, 2007 at 6:47 pm
It’s really cool application. Let me get your sample application with source code.
Please!
Thanks,
Raja Kumaravel
November 22nd, 2007 at 10:52 pm
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
November 28th, 2007 at 9:48 pm
@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.
December 26th, 2007 at 8:19 pm
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.
January 7th, 2008 at 4:34 pm
Richard, could you please send me your sample application with source code? The link is dead.
Thanks in advance.
January 8th, 2008 at 1:54 pm
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.
January 15th, 2008 at 4:18 am
[...] new savior is…Richard Hallgren; great first name by the [...]
February 13th, 2008 at 12:16 pm
Hi richard,
This is very usefull indeed, please send me the sample application and source code.
Thanks in Advance,
Kavitha
March 28th, 2008 at 7:14 pm
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…
September 5th, 2008 at 8:19 am
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 .
September 9th, 2008 at 2:50 pm
@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.
October 1st, 2008 at 3:56 pm
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?
October 2nd, 2008 at 10:46 am
The problem is solved. I had Biztalk Server 2006, not 2006 R2. With R2 works fine. Thank you!
October 2nd, 2008 at 10:59 am
[...] 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 [...]
October 14th, 2008 at 11:36 am
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 ?
December 2nd, 2009 at 7:37 pm
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
December 3rd, 2009 at 7:23 am
@Dan: Sounds you’re using the right property. Are you sure you have activated DTA tracking in the ports?