Retrieving the Context of a BizTalk Message from an Orchestration

Messages in BizTalk are implemented in two different ways.

First, messages that flow through Receive and Send pipelines are represented by the IBaseMessage interface. This interface makes it easy to access the contents of BizTalk messages in a streaming fashion as well as work with its context properties.

Second, messages used in the BizTalk orchestration engine, known as XLANG/s messages are represented by instances of the XLANGMessage class. This class is very different and much more constrained than the preceding interface. In particular, it does not seem to be possible to iterate over the set of context properties associated with a BizTalk message.

The reason why there are two different implementations of the same concept is made obvious if we think about how BizTalk processes messages.

When messages are first coming through a Receive pipeline, all we have is a stream of bytes. It is the jop of the BizTalk Messaging Agent to process those incoming bytes – with the help of custom or builtin pipeline components – and serialize them to the MessageBox. At the same time, the Messaging Agent maintains an in-memory representation of the message context.

On the contrary, it is important to understand that messages used inside an orchestration do not live in memory. Instead, you can think of an XLANG/s message as a handle to a record in the MessageBox. This is why we need explicit Construct shapes that wrap a transactional database scope when creating Bizalk messages and, incidentally, this is why messages inside an orchestration are immutable.

Why do we Need to Access Context Properties?

Although mainly used for routing purposes, there are several occasions where it might make sense to be able to iterate over the set of properties in the context of a BizTalk message from inside an orchestration.

One such scenario, that’ll describe in a next post, involves copying the entire context from one BizTalk message to another. This is not as easy as it seems…

Credit: I have been wanting for a solution to this
problem for a long time, until I eventually stumbled upon a working implementation made by Carlos Medina. The code for this article is a slight adaptation of the code Carlos released as part of his ContextAccessor Functoid CodePlex project.

Retrieving the Context of an XLANG/s Message

The following code will iterate over all segments in the current orchestration
in search of the specified message object and return a collection of context properties.

using Microsoft.XLANGs.BaseTypes;
using Microsoft.XLANGs.Core;

public static Hashtable GetContext(XLANGMessage message)
    foreach (Segment segment in Service.RootService._segments)
      IDictionary fields =
          , segment.ExceptionContext);

      foreach (DictionaryEntry field in fields)
        XMessage msg = (field.Value as XMessage);
        if (msg == null)

        if (String.Compare(msg.Name, message.Name) != 0)

        return msg.GetContextProperties();
  catch (Exception /* e */)
    // do not provoke failure
    // probably best to add some logging here

  return new Hashtable();

The resulting Hashtable collection holds values for the context properties, keyed by instances of XmlQName class. This makes it quite straightforward to retrieve the fully qualified name of each context property.

This class, however, is not intended to be used directly from user code so I will only treat the resulting collection as opaque. Nevertheless, this tip will prove useful later on when I’ll present my solution to dynamic updates to the context of BizTalk messages inside an orchestration.

This entry was posted in BizTalk, Tips. Bookmark the permalink.

2 Responses to Retrieving the Context of a BizTalk Message from an Orchestration

  1. Pingback: How to use context data with Business Rules | bLogical

  2. Pingback: How to use context data with Business Rules |

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s