Easily retrieving message body AND context from BizTalk tracking database

I've been searching for a while now on a way to retrieve message body and context from BizTalk without having to use WMI,
simply because I didn't want to write my messages to file, read them back in, parse the context and then delete the generated
files again.
Interesting blog post I stumbled upon was this: View post
But this was not sufficient, because no one managed to get the FULL message context from this method. However, there is a very easy way to do this.
First of all, I defined two strings representing a property name, and a property namespace. They will be the out values for our method.
Now the Context object we get from the tracked message out of the BizTalk Operations dll, implements the Microsoft.BizTalk.Message.Interop.IBaseMessageContext interface. When you look closely at it's methods, you will see a "ReadAt" method. That's the one we need to use.
The Context object also contains a CountProperties variable, which doesn't need any further explanation I think.
So the next part is pretty straight-forward right?
Loop through the CountProperties values, use them as indexes, and call the ReadAt(index, name, namespace) method
for every loop passage, and hold the property value in another string variable.
                string sname = "";
                string snamespace = "";
                for (int i = 0; i < imsg.Context.CountProperties; i++)
                {
                    string value = imsg.Context.ReadAt(i, out sname, out snamespace).ToString();
                    Console.WriteLine(sname + " (" + snamespace + ") = " + value);
                }
And there you have it! Full message context!
Enjoy experimenting with this very cool way of getting body and context of a BizTalk message! IMHO, this is a much better
approach than the WMI way.

Currently rated 3.0 by 1 people

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Jan
Posted on: 7/2/2009 at 3:32 AM
Categories: BizTalk
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (21) | Post RSSRSS comment feed

Terminating BizTalk instances: WMI vs Operations

The Administration console that is delivered with the installation of BizTalk allows a user to query suspended service instances and resume or terminate them accordingly. However, when you want to go ?outside the box?, and I often do :) you might want to consider finding out possibilities of doing those actions yourself, for instance from a web based managing platform. (note: the following methods work for both BTS2006 and 2009)

When you install BizTalk, there is a WMI provider installed on your machine as well, located in the root/MicrosoftBizTalkServer namespace. That namespace actually provides just about every functionality you need for administration purposes. But in this case, I typically want to focus on the MSBTS_ServiceInstance class. (this is a snapshot taken from server explorer in VS2008)

image

If you right click the class and generate an instance, your current project will get an extra class called ROOT.MicrosoftBizTalkServer.MSBTS_ServiceInstance.cs. When you take a look at that class, you will see the three methods that I?m interested in at this moment: Suspend, Resume, and Terminate. Let?s focus on the Terminate example for this case.

If you want to terminate a service instance through WMI (and there are examples of that over the entire web, so let?s NOT get into that deeply), you can simply use the generated class and say something like:

image

When you provide the InstanceID of your service instance, together with the name and server of the BizTalk Management Database of the BizTalk environment you are working on, the generated class will construct the WMI query for you and execute behind the scenes. So what you need to keep in mind with this method is that you need to go through the entire WMI architecture before actually taking an action.

To me, it sounds easier if I could do the same action in a more direct approach to BizTalk. And lucky for both me as the people who read this (at least I hope people read this :)) we can do that!

When you go to the root install directory of BizTalk (typically that?s C:\Program Files\Microsoft BizTalk Server) you will notice an assembly called Microsoft.BizTalk.Operations.dll, which in this case you can compare to a holy grail :) Let?s take a quick peek at the assembly with Reflector:

image

Of course now every still-awake reader notices the same methods I previously stated. And using these methods are of the same ease as the WMI methods. It goes as follows:

image 

Parameters stay the same. Create an instance of the BizTalkOperations class using the servername and database name of the BizTalk Management Database, and terminate your instance using the InstanceID of the Service Instance object.

Not much difference at first sight, but the thing here really is that when using the BizTalk.Operations, you have a more direct database approach to your action, instead of going through the entire WMI system before actually doing something. Sounds like that could save some performance when you would terminate instances with 500 at a time.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Jan
Posted on: 7/2/2009 at 3:30 AM
Categories: C# | BizTalk
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (45) | Post RSSRSS comment feed

Persist message context when re-injecting a message in BizTalk

There are many cases in BizTalk environments when one want to re-inject a message into their system. I?m mainly referring to testing environments where it doesn?t really matter if a message is repeated.

However, it can also be preferred to copy the context of the message into the newly injected message, because your system might want to do some processing of that context while re-injecting. When you do this from code, it?s a snap!

First of all, we need to load up our message body and context:

image

Note that you need to specify the sql server name and database name of the BizTalk Tracking Database (commonly named BizTalkDTADb, but you can configure any database name). The System.GUID attribute should be replaced with the message id of the message you want to re-inject.

Now that we have an IBaseMessage object, we can access the context and copy it to the context of a new out IBaseMessage object.

 

image

 

This way we can keep a difference between promoted context properties and standard context properties. And that?s the only thing to it, msg_out is ready to be re-injected
to the BizTalk system

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Jan
Posted on: 7/2/2009 at 3:29 AM
Categories: C# | BizTalk
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (30) | Post RSSRSS comment feed

Incorporate LINQ in your BizTalk projects

LINQ is something that has had my full attention ever since it was released. And I?ve been wanting to do something on this blog with LINQ, but never really found the inspiration to do something ?special? with it. Up until now.

So it occurred to me that there is not (to my knowledge) anything available that allows you to incorporate your LINQ way-of-work with BizTalk. And why not? There are purposes enough to use LINQ in a BizTalk environment, and it doesn?t even have to be rocket science to get there. On the contrary, using LINQ for BizTalk is about as easy as it can get really. And the means of use for this are limitless. Let?s go through some of the possibilities.

NOTE: Throughout this article, you will see a few different ways of how we could incorporate LINQ in our day-to-day BizTalk development. Iin good practice, we will NOT try to re-invent hot water. Instead, we will gratefully use existing functionality through LINQ to objects, and LINQ to XML.

A first example of LINQ usage in BizTalk is re-using BizTalk objects to provide a central BizTalk Administration Console. The advantages of this are obvious: we don?t have a need for a local Windows application to view our ports, locations, orchestrations and schemas. Instead, we can incorporate all BizTalk hubs from an infrastructure into one administration site where we can query administration information, start and stop our ports, and so on. One way we can do this, is through LINQ. The following will show you how.

When you install BizTalk Server, a number of assemblies are installed to the install location. By default, this is C:\Program Files\Microsoft BizTalk Server 2006. In this directory, there is one particular assembly that draws attention, namely Microsoft.BizTalk.ExplorerOM.dll. When we would take a look at this assembly through the Reflector tool (http://www.red-gate.com/products/reflector/), we would typically see a definition like this:

clip_image002

The BtsCatalogExplorer object is the central part of this project, that is because of the following properties it holds:

clip_image004

And these are just a glimpse of the properties. In general, you can say that the BtsCatalogExplorer object ?represents? one Administration Console, or one BizTalk Management instance.

So what is the problem? We could just take those collections and fire up some LINQ queries on that, right?

Well, unfortunately no. The collection objects in the BizTalk assembly are all implementing the IEnumerable and ICollection interface. No surprises, but unfortunately, also no LINQ available with extension methods. Yet this really is the key to where we want to go to, and the solution is as easy as a HelloWorld solution. We simply have to create our own object that contains a BtsCx atalogExplorer object, and cast the collection properties to generic list objects that implement IEnumerable<T>, which we need when we want to use LINQ methods in LINQ to object. When we take the SendPort collection as an example, basically what we need to do is this:

clip_image006

What we do here, is take the BtsCatalogExplorer object, and instantiate it through btsex. In order, we then cast the SendPortCollection to an IEnumerable<SendPort> through the OfType<T> method, which we then convert to a List<SendPort> through the ToList<T> method. The result is a generic List object of SendPort object, that implements IEnumerable<T>. Now that we have this LINQable object, we can do something like the following in any client environment:

clip_image008

It is a very basic, very simple example, but it shows how easy it is to introduce LINQ into the administration side of BizTalk.

Now there also is a processing side to BizTalk. Wouldn?t it be great to incorporate LINQ to that side as well? Now you can. The following example will demonstrate a simple tool to promote properties through LINQ, and I call it the LINQPromotor Pipeline Component. For this example, I will show the results first, and then the method used.

So let?s start off by summarizing the requirements. For this demo it is sufficient that we use one component per property we want to use. So the component needs to take in a property name to promote, a namespace, and a LINQ query to retrieve the value to promote. So typically, we want a configuration like this:

clip_image010
clip_image012
When we use this pipeline on an incoming message, we see the following when we query the message context:

clip_image014

Note that there are some things marked with red to draw attention. First of all, in the pipeline configuration properties, you see the PropertyName and PropertyNamespace values configurable. These are values are shown in the context where you see Name = PromotedByLinq and Namespace = http://schemas.linq.com/promotions/.

But the key here is the LINQ statement property. Instead of an XPath query that we would fill in to retrieve the value to promote, we actually write a LINQ statement to do that query. This makes the query easier to read and to configure.

One way to do this is through runtime compilation. Take the linq query in as a string, add it to a method that takes in an XElement object, compile it at runtime, execute and promote the returned string. That summarizes the way of work in this example. Now we will take a closer look.

First of all, the linq statement gets inserted in a stringbuilder that builds up a piece of code that contains one method with one parameter of type XElement, and that returns a string as the value:

public string GetVal(XElement element){}.

We then compile that piece of code using the CSharpCodeProvice object, that is located in the Microsoft.CSharp namespace.

clip_image016

Note that in order to use LINQ, you need to specify the CompilerVersion to v3.5. MSDN documentation will specify a version ?3.5?, but ?v3.5? is what you want to use.

The code is compiled in memory, so we can just return an assembly ob ject, create an instance of the class we generated, and invoke the method:

clip_image018

The string that is returned contains the value that we can simply promote in our IBaseMessage object through outMessage.Context.Promote(PropertyName, PropertyNamespace, value);

And the results speak for themselves.

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Posted by: Jan
Posted on: 7/2/2009 at 3:28 AM
Categories: C# | BizTalk
Actions: E-mail | Kick it! | DZone it! | del.icio.us
Post Information: Permalink | Comments (19) | Post RSSRSS comment feed