a trace of thought on...BizTalk Server, Team Foundation Server, AppFabric, etc. RSS 2.0
 Wednesday, May 11, 2005

There have been several folks who have discussed how to create messages "from scratch" within an Orchestration context - you can read Matt's thoughts and check out a BizTalk documentation excerpt. This is a common question, and I had an additional technique I thought I would share... 

Background: When you have a schema that has many promoted properties (or distinguished fields), or many elements that can be set via xpath expressions easily, it can be useful to simply start with a "template" document instance and populate the element content that you are interested in. 

In this situation, you will often have a "Message Assignment" shape that looks something like this:

xmlDoc.LoadXml("<ns0:BizTalkSampleS3 
      xmlns:ns0="http://BizTalkSample.Schemas.BizTalkSampleS3">
      <SomeElement></SomeElement></ns0:BizTalkSampleS3>");
someMsg = xmlDoc;
someMsg.SomeElement = "some content";
// (or xpath(someMsg,someElementXPath) = "some content" if we don't have a 
// distinguished field.)
   

One disadvantage of loading up "template" xml documents from either expression shapes or code (via XmlDocument.LoadXml) is that those xml fragments can get easily "lost", and are hard to update early in the development cycle when schemas may still be in flux.  Loading the template files from the file system is problematic because the question arises "where should I store these files, so that I can find them in any environment I deploy to?" (Solvable, but a hassle.)

Instead, why not embed the template xml documents as assembly resources?  For those unfamiliar with that process, I have a short tutorial here (& a helper class.) 

  1. You will need a C# project as part of your overall BizTalk solution.  Place your template xml file(s) in the directory corresponding to this project, and add them as an "existing item" to the project. 
  2. Select this file within the Solution Explorer, and within the Properties window, select "Embedded Resource" as the "Build Action" as shown here:

     

  3. Place this class (text here) within the same C# project that houses the resources you have added.
  4. To construct a message, drag out a "Message Assignment" shape, and within the associated expression write some code like the following.  Simply pass the file name of the template document as an argument to GetXmlDocResource (or GetStringResource.)
    sampleUsingTemplate = 
       BizTalkSample.Components.EmbeddedResourceCache.
          GetXmlDocResource("BizTalkSampleS3_output.xml");
       // Populate the "rest" of the message with distinguished fields, promoted 
       // properties, xpath expressions, etc.
       sampleUsingTemplate.SomeElement = "foo";
                

The class I have supplied will cache the loaded resources in a hashtable for performance sake, and allow you to load resources as both strings and XmlDocuments.

A last thought: Many people ask, "Why can't I just create a message using a new operator or a default constructor of some sort?" Well, because few XSD schemas sufficiently constrain the set of valid instance documents enough for that to be useful - what form would a "default message" take? (Would it have the optional elements you need? Some elements that you don't want?)

Enjoy - feedback appreciated!

Wednesday, May 11, 2005 3:29:14 PM (Central Standard Time, UTC-06:00)  #    Comments [6] -
BizTalk Tools | BizTalk Insights
Tracked by:
"BizTalk -" (Matt Meleski's .Net Blog - The ABC's of .NET) [Trackback]
"BizTalk -" (Matt Meleski's .Net Blog - The ABC's of .NET) [Trackback]
Thursday, August 4, 2005 10:42:28 AM (Central Standard Time, UTC-06:00)
Is there a particular reason the C# project must be included within the Biztalk solution? Could you build the EmbeddedResourceCache assembly separately and use it from multiple orchestrations?
Jeremy
Thursday, August 4, 2005 2:33:24 PM (Central Standard Time, UTC-06:00)
Jeremy - you can certainly uses the C# project from multiple orchestrations in multiple solutions. I have it bundled into the same solution just for convenience.
Monday, November 28, 2005 6:47:24 AM (Central Standard Time, UTC-06:00)
Hi Scott,

I've implemented this very common routine in another manner, allowing for better maintainable code in my opinion. Please see http://martijnh.blogspot.com/2005/10/schema-instance-generator-for-use.html. Let me know what you think, my e-mail address can be found here: http://martijnh.blogspot.com/2004/07/e-mail-address.html
Tuesday, December 20, 2005 8:32:08 AM (Central Standard Time, UTC-06:00)
I like the schema instance generator concept quite a bit - very nice. My only comment would be that for many schemas, you will have much of the structure optional (or minOccurs=0), and yet the instance document you need for a particular case might very well require that bit to be "filled in". The "generate instance" concept breaks down a bit there.
Thursday, August 24, 2006 3:50:50 AM (Central Standard Time, UTC-06:00)
Nice tip, Scott. Wish I had read this post sooner.
willem
Wednesday, May 2, 2007 3:31:31 PM (Central Standard Time, UTC-06:00)
Scott,
It seems too big elefant for such small mouse. :)
Comments are closed.
Archive
<December 2014>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
About the author:

Scott Colestock lives, writes, and works as an independent consultant in the Twin Cities (Minneapolis, Minnesota) area.

© Copyright 2014
Scott Colestock
Sign In
All Content © 2014, Scott Colestock
DasBlog theme 'Business' created by Christoph De Baene (delarou)