Calling Inline .NET Code From Inline XSLT In BizTalk

A while back I wrote about calling external assemblies from within a BizTalk map. A problem I mentioned was that the member variable in the class that the map was calling seemed to be getting shared amongst execution instances. Each map creates a sequential page number in the XSLT and puts it into the destination XML. However, I’d see output where the first message had pages “1..3..5..7..8” and the second message had pages “2..4..6..9.” Very strange. I thought I fixed the problem, but it surfaced today in our Test environment.

So, I set out to keep everything local to the map and get rid of external assembly calls. After banging my head for a few minutes, I came up the perfect solution. I decided to mix inline script with inline XSLT. “Madness” you say? I built a small test scenario. The map I constructed looks like this:

In the first Scripting functoid, I have “inline C#” selected, and I created a global variable. I then have a function to increment that variable and return the next number in sequence.

Did you know that you could have “global variables” in a map? Neat stuff. If I check out the XSLT that BizTalk generates for my map, I can see my function exposed as such:

Now I know how to call this within my XSLT! The second Scripting functoid’s inline XSLT looks like this:


Notice that I can call the C# method written in the previous functoid with this code:

<xsl:value-of select=”userCSharp:GetPageNumber()”/>

The “prefix” is the auto-generated one from the XSLT. Now, all the calculations are happening locally within the map, and not relying on outside components. The result of this map is a document that looks like this:

There you go. Using global variables within a BizTalk map and calling a C# function from within the XSLT itself.

Technorati Tags:

Author: Richard Seroter

Richard Seroter is Director of Developer Relations and Outbound Product Management at Google Cloud. He’s also an instructor at Pluralsight, a frequent public speaker, the author of multiple books on software design and development, and a former InfoQ.com editor plus former 12-time Microsoft MVP for cloud. As Director of Developer Relations and Outbound Product Management, Richard leads an organization of Google Cloud developer advocates, engineers, platform builders, and outbound product managers that help customers find success in their cloud journey. Richard maintains a regularly updated blog on topics of architecture and solution design and can be found on Twitter as @rseroter.

11 thoughts

  1. This is a wonderful diamond in the rough for BizTalk maps. One thing to mention is that you can call your map global variable from an Inline C# script functoid as well. Using your example above, instead of using an xslt Call Template, use the Inline C# option and enter “return GetPageNumber();” in the inline script buffer section and it will return your page number as well. Either way, thanks for the blog post, it was very helpful.

    – John

  2. hi, your article is very helpful. I would like to know how the call would change if I had to pass a parameter to the GetPageNumber function. In my case I have a XSLTvariable that needs to be passed as a parameter to a C# function. Your help is highly appreciated. Thanks,asrinivasan.

  3. Hi, thanks for your post. Can you attach the code file here?I tried using your sample but getting error in xslt template. Anyway I got the work around using script, iteration and index functoids using global variables.

    Thanks
    Venu

  4. I’m trying to call an inline c# function from the inline xslt and I am able to call one that has no parameters fine but this other function takes two input parameters, any sample on how to build this in xslt I tried a ton of ways and all fail. Regards

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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

This site uses Akismet to reduce spam. Learn how your comment data is processed.