This is just a quick post about accessing context properties within a Pipeline Component.
The below picture displays 3 things:
1. The most common technique people use to access context properties within a Pipeline Component
2. The way I prefer to access the common context properties
3. The way I prefer to access custom context properties
What don’t I like about the common approach?
As mentioned the most common approach is the first two lines in the example method. While it does the job I often find people may misspell the name or namespace of the property. This just means it takes longer to get it right.
Why do I prefer the other approach?
My preferred approach takes advantage of the fact that the context properties derive from the MessageContextPropertyBase class. Based on this I have a helper class which is available in the sample which lets you pass in an instance of the context property class. This will then use the properties of this class to read/write or promote the property. This works because the MessageContextPropertyBase class contains properties which give you access to the Name and Namespace of the context property.
In this approach if you want to access the common context properties such as the message type then you can add a reference to the Microsoft.BizTalk.GlobalPropertySchemas.dll which contains most of these properties.
If you want to access a custom context property then add a reference to your assembly containing your property schema and then you can access it that way. This is demonstrated as the bottom few lines of the code snippet.
As result I find this approach just means you don’t get any errors by getting a hard coded string wrong.
As I said just a quick one but hopefully this may save a few people a little bit of time.
The code sample is available below: