Thursday, April 30, 2009

Debugging BizTalk IL With Reflector

I've been using Reflector quite a bit lately to analyse BizTalk artifacts. Have also been talking to a colleague recently about IL debugging to work out how BizTalk works under the covers.

Recently I found Deblector which allows IL debugging in Reflector. This is a really cool combo. John Flanders wrote about debugging orchestration IL back in '04 but Deblector reveals the IL of the framework and dependencies too without any prep.

BizTalk's Orchestration Debugger is a nice tool and great 95% of the time. If you need something more substantial I suggest you have a look at the Deblector add-in.

Below is some instruction on setting up Deblector and an example of finding some message content between a transform shape and an assign shape (something that cannot be done in the BizTalk Orchestration Debugger).

Setup Deblector

  1. Get the latest version of Reflector

  2. Download and unzip DeblectorAddin-1.01-Alpha

  3. In Reflector go to View->Options->Browser

  4. Select 'Automatically Resolve Assemblies' and Visbility = 'All Items'

  5. Then go to View->Options->Add-Ins

  6. Click Add and choose DeblectorAddIn.dll from the unzipped Deblector folder

  7. Choose Tools->Deblector to start the add-in

Start Debugging

  1. Load the assembly to debug into Reflector

  2. Choose the 'Attach to process' toolbar button and choose the right BTSBTSvc.exe process

  3. Select the orchestration segment (aka scope) to debug and the line of IL to break on. Not all the IL can be traversed using the debugger so you may have to set a breakpoint block until you get used to the UX. I have found that the value set operation (stfld) and target instruction transfers (brfalse, brfalse_s, brtrue, brtrue_s) seem to be good breakpoints. Note that a breakpoint can only be set while the process is paused

  4. Click Run and send your message to start the orchestration

  5. When the breakpoint is hit Reflector will highlight the breakpoint and you can then query the locals, use the Shell commands and step through the code base. You'll also see that the orchestration instance is marked as Active in the BizTalk admin console

  6. To end the debug session click the Deblector Stop button but note that the host instance you attached to will be stopped too

Transformation Debug Example

In this example Message1 is transformed to Message2 and then a distinguished property in Message2 is assigned a value.


When the breakpoint is hit the Auto window in the debugger can be used to view each of the variables in scope.


If the property assignment was failing (for example) then the Message2 read buffer could be queried to determine Message2's content before the assignment. The read buffer is held as a byte array so this needs to be printed through the debugger Shell window and then converted to a string to view the Xml. The print statement in the case above follows:

print local_3.__Message_2._parts._items[0].__valueToken._value._rewriter._state._readCache._source._streamFactory._sourceStream._buffer


The printed text can then be converted using some code similar to that listed here:

List<byte> bytes = new List<byte>();
List<string> strings = new List<string>();
strings.AddRange(textBox1.Text.Split(
new string[] {"\r\n"}, StringSplitOptions.RemoveEmptyEntries));
foreach (string item in strings)
{
    
List<string> debuggerText = new List<string>();
    debuggerText.AddRange(item.Split(
new char[] {' '}, StringSplitOptions.RemoveEmptyEntries));
    debuggerText.Reverse();
    bytes.Add(
byte.Parse(debuggerText[0]));
}
string bytesAsString = Encoding.ASCII.GetString(bytes.ToArray());
textBox2.Text = bytesAsString;

No comments:

Post a Comment