<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Gobbledygooks by Richard Hallgren &#187; Tutorial</title>
	<atom:link href="http://www.richardhallgren.com/category/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.richardhallgren.com</link>
	<description>.NET, BizTalk and integration focused scribbles</description>
	<lastBuildDate>Fri, 23 Apr 2010 07:13:45 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Efficient grouping and debatching of big files using BizTalk 2006</title>
		<link>http://www.richardhallgren.com/efficient-grouping-and-debatching-of-big-files-using-biztalk-2006/</link>
		<comments>http://www.richardhallgren.com/efficient-grouping-and-debatching-of-big-files-using-biztalk-2006/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 10:06:06 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[BizTalk 2006]]></category>
		<category><![CDATA[Sample code]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/efficient-grouping-and-debatching-of-big-files-using-biztalk-2006/</guid>
		<description><![CDATA[I&#8217;ve seen people struggle both on the forums and while doing consulting when in it comes to finding an good way of grouping and transforming content in file before debatching it. Say for example we have a text file like the example below.
0001;Test row, id 0001, category 10;10
0002;Test row, id 0002, category 10;10
0003;Test row, id [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve seen people struggle both on the <a href="http://forums.microsoft.com/msdn/ShowPost.aspx?PostID=3564558&amp;SiteID=1" target="_blank">forums</a> and while doing consulting when in it comes to finding an good way of grouping and transforming content in file before debatching it. Say for example we have a text file like the example below.</p>
<pre>0001;Test row, id 0001, category 10;10
0002;Test row, id 0002, category 10;10
0003;Test row, id 0003, category 10;10
0004;Test row, id 0004, category 20;20
0005;Test row, id 0005, category 20;20
0006;Test row, id 0006, category 20;20
0007;Test row, id 0007, category 20;20
0008;Test row, id 0008, category 10;10
0009;Test row, id 0009, category 10;10
0010;Test row, id 0010, category 30;30</pre>
<p>Notice how the the ten rows belong to three different categories (10,20 and 30). These kind of export are in my experience quite common batch export from legacy systems and they usually aren&#8217;t ten rows (in my last project the sizes ranged from 5 MB to 25 MB) &#8230;</p>
<h3>The problem</h3>
<p>The problem is that the receiving system expects the data to be in <em>separate groups,</em> <em>grouped by the categories the rows belong to</em>. The expected message might look something like the below for category 10 (notice how all rows within the group are from category 10)</p>
<pre class="xml:nogutter:nocontrols:collapse:showcolumns" name="code">&lt;ns1:Group numberOfRows=&quot;5&quot; xmlns:ns1=&quot;http://Blah/Group&quot;&gt;
  &lt;Row&gt;
    &lt;Id&gt;0001&lt;/Id&gt;
    &lt;Text&gt;Test row, id 0001, category 10&lt;/Text&gt;
    &lt;Category&gt;10&lt;/Category&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
    &lt;Id&gt;0002&lt;/Id&gt;
    &lt;Text&gt;Test row, id 0002, category 10&lt;/Text&gt;
    &lt;Category&gt;10&lt;/Category&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
    &lt;Id&gt;0003&lt;/Id&gt;
    &lt;Text&gt;Test row, id 0003, category 10&lt;/Text&gt;
    &lt;Category&gt;10&lt;/Category&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
    &lt;Id&gt;0008&lt;/Id&gt;
    &lt;Text&gt;Test row, id 0008, category 10&lt;/Text&gt;
    &lt;Category&gt;10&lt;/Category&gt;
  &lt;/Row&gt;
  &lt;Row&gt;
    &lt;Id&gt;0009&lt;/Id&gt;
    &lt;Text&gt;Test row, id 0009, category 10&lt;/Text&gt;
    &lt;Category&gt;10&lt;/Category&gt;
  &lt;/Row&gt;
&lt;/ns1:Group&gt;</pre>
<p>The problem is now that we need to find a efficient way of first <em>grouping the incoming flat file based message</em> and then to <em>debatch it using those groups. <strong>Our ultimate goal is to have separate messages that groups all rows that belongs to the same category and then send these messages to the receiving system</strong>. </em>How would you solve this?</p>
<p>I&#8217;ve seen loads of different solution involving orchestrations, databases etc, but the main problem they all had in common is that they&#8217;ve loaded up to much of the message in memory and finally hit an <em>OutOfMemoryException</em>.</p>
<h3>The solution</h3>
<p>The way to solve this is to use pure messaging as one of the new features in BizTalk 2006 is the new <a href="http://www.microsoft.com/technet/prodtechnol/biztalk/2006/evaluate/overview/default.mspx" target="_blank">large messages transformation engine</a>.</p>
<blockquote>
<p><b>Large message transformation.</b> In previous versions of BizTalk Server, mapping of documents always occurred in-memory. While in-memory mapping provides the best performance, it can quickly consume resources when large documents are mapped. In BizTalk Server 2006, large messages will be mapped by the new large message transformation engine, which buffers message data to the file system, keeping the memory consumption flat.</p>
</blockquote>
<p>So the idea is the to read the incoming flat file, use the <a href="FlatFileDisassembler " target="_blank">Flat File Disassembler</a> to transform the message to it&#8217;s XML representation (step 1,2 and in the figure below) and the to use XSLT to transform in to groups (step 4 and 5). We will then use the <a href="http://msdn.microsoft.com/en-us/library/aa561814.aspx" target="_blank">XML Disassembler</a> to split those groups into separate messages containing all the rows within a category (step 6 and 7).</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295groupingflow2-2.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="GroupingFlow2" src="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295groupingflow2-thumb.png" width="500" height="98" /></a></p>
<p>Step 1, 2 and 3 are <a href="http://msdn.microsoft.com/en-us/library/aa577898.aspx" target="_blank">straight forward</a> and pure configuration. Step 4 and 5 will require some custom XSLT and I&#8217;ll describe that in more detail in the section below.&#160; Step 6 and 7 will be discussed in the last section of the post.</p>
<h4>Grouping</h4>
<p>Let&#8217;s start by looking at a way to group the message. I will use some custom XSLT and a technique called the <a href="http://www.jenitennison.com/xslt/grouping/muenchian.html" target="_blank">Muenchian method</a>. A segment from the XML representation of the flat file message could look something like this.</p>
<pre class="xml:nogutter:nocontrols:collapse:showcolumns" name="code">&lt;Rows xmlns=&quot;http://Blah/Incoming_FF&quot;&gt;
    &lt;Row xmlns=&quot;&quot;&gt;
        &lt;ID&gt;0001&lt;/ID&gt;
        &lt;Text&gt;Test row, id 0001, category 10&lt;/Text&gt;
        &lt;Category&gt;10&lt;/Category&gt;
    &lt;/Row&gt;
    &lt;Row xmlns=&quot;&quot;&gt;
        &lt;ID&gt;0002&lt;/ID&gt;
        &lt;Text&gt;Test row, id 0002, category 10&lt;/Text&gt;
        &lt;Category&gt;10&lt;/Category&gt;
    &lt;/Row&gt;
...
[message cut for readability]</pre>
<p>The XSLT will use could look something like the below. It&#8217;s kind of straight forward and I&#8217;ve tried commenting the important parts of in the actual script. Basically it will use keys to fins the unique categories and then (again using keys) selecting those rows within the category to loop and write to a group.</p>
<pre class="xml:nogutter:nocontrols:collapse:showcolumns" name="code">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;xsl:stylesheet version=&quot;1.0&quot;
                xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;
                xmlns:ns1=&quot;http://GroupAndDebatch.Schemas.Incoming_FF&quot;
                xmlns:ns2=&quot;http://GroupAndDebatch.Schemas.Grouped&quot;
                &gt;
    &lt;!--Defining the key we're gonna use--&gt;
    &lt;xsl:key name=&quot;rows-by-category&quot; match=&quot;Row&quot; use=&quot;Category&quot; /&gt;

    &lt;xsl:template match=&quot;/ns1:Rows&quot;&gt;
        &lt;ns2:Groups&gt;

        &lt;!--Looping the unique categories to get a group for--&gt;
        &lt;xsl:for-each select=&quot;Row[count(. | key('rows-by-category', Category)[1]) = 1]&quot;&gt;

            &lt;!--Creating a new group and set the numberOfRows--&gt;
            &lt;Group numberOfRows=&quot;{count(key('rows-by-category', Category))}&quot;&gt;

            &lt;!--Loop all the rows within the specific category we're on--&gt;
            &lt;xsl:for-each select=&quot;key('rows-by-category', Category)&quot;&gt;
                &lt;Row&gt;
                    &lt;ID&gt;
                        &lt;xsl:value-of select=&quot;ID&quot;/&gt;
                    &lt;/ID&gt;
                    &lt;Text&gt;
                        &lt;xsl:value-of select=&quot;Text&quot;/&gt;
                    &lt;/Text&gt;
                    &lt;Category&gt;
                        &lt;xsl:value-of select=&quot;Category&quot;/&gt;
                    &lt;/Category&gt;
                &lt;/Row&gt;
            &lt;/xsl:for-each&gt;
            &lt;/Group&gt;
        &lt;/xsl:for-each&gt;
        &lt;/ns2:Groups&gt;
    &lt;/xsl:template&gt;

&lt;/xsl:stylesheet&gt;</pre>
<blockquote>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295note-2.gif"><img style="border-right-width: 0px; margin: 0px 10px 0px 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="note" align="left" src="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295note-thumb.gif" width="18" height="18" /></a>You have found all the <a href="http://msdn.microsoft.com/en-us/library/aa302298.aspx" target="_blank">XSLT and XML related features</a> in Visual Studio &#8211; right?</p>
</blockquote>
<p>Ok, so the above XSLT will give us a XML structure that looks some like this.</p>
<pre class="xml:nogutter:nocontrols:collapse:showcolumns" name="code">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;ns2:Groups xmlns:ns2=&quot;http://Blah/Groups&quot; xmlns:ns1=&quot;http://Blah/Group&quot;&gt;
    &lt;ns1:Group numberOfRows=&quot;5&quot;&gt;
        &lt;Row&gt;
            &lt;ID&gt;0001&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0001, category 10&lt;/Text&gt;
            &lt;Category&gt;10&lt;/Category&gt;
        &lt;/Row&gt;
        &lt;Row&gt;
            &lt;ID&gt;0002&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0002, category 10&lt;/Text&gt;
            &lt;Category&gt;10&lt;/Category&gt;
        &lt;/Row&gt;
        &lt;Row&gt;
            &lt;ID&gt;0003&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0003, category 10&lt;/Text&gt;
            &lt;Category&gt;10&lt;/Category&gt;
        &lt;/Row&gt;
        &lt;Row&gt;
            &lt;ID&gt;0008&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0008, category 10&lt;/Text&gt;
            &lt;Category&gt;10&lt;/Category&gt;
        &lt;/Row&gt;
        &lt;Row&gt;
            &lt;ID&gt;0009&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0009, category 10&lt;/Text&gt;
            &lt;Category&gt;10&lt;/Category&gt;
        &lt;/Row&gt;
    &lt;/ns1:Group&gt;
    &lt;ns1:Group numberOfRows=&quot;4&quot;&gt;
        &lt;Row&gt;
            &lt;ID&gt;0004&lt;/ID&gt;
            &lt;Text&gt;Test row, id 0004, category 20&lt;/Text&gt;
            &lt;Category&gt;20&lt;/Category&gt;
        &lt;/Row&gt;
...
[message cut for readability]</pre>
<p><strong>Finally! This we can debatch!</strong></p>
<h4>Debatching</h4>
<p>Debatch the <em>Groups</em> message above is also rather straight forward and I won&#8217;t spend much time on in this post. The best way to learn more about it is to have a look ate the <a href="http://msdn.microsoft.com/en-us/library/ms966332.aspx" target="_blank">EnvelopeProcessing sample</a> in the BizTalk SDK.</p>
<p><strong>And the end result of the debatching are single messages within a unique category, just as the receiving system expects! Problem solved.</strong></p>
<h3>Issue #1 &#8211; slow transformations</h3>
<p>The first time I&#8217;ve put a solution like this in test and started testing with some real sized messages (&gt; 1 MB) I really panicked, the mapping took forever. And I really mean forever, I sat there waiting for 2-3 hours (!) for a single file getting transformed. When I had tested the same XML based file in Visual Studio the transformation took about 10 seconds so I knew that wasn&#8217;t it. With some digging <a href="http://technet.microsoft.com/en-us/library/aa560481.aspx" target="_blank">here</a> I found the <em>TransformThreshold</em> parameter.</p>
<p><em>TransformThreshold</em> decides how big a message can be in memory before BizTalk start buffering it to disk. The default value is 1 MB and one really has to be careful when changing this. Make sure you thought hard about your solution and situation before changing the value &#8211; how much traffic do you receive and how much of that can you afford reading in to memory?</p>
<p>In my case I received a couple of big files spread out over a night so setting parameter with a large amount wasn&#8217;t really a problem and that really solved the problem. The mapping finished in below 10 minutes as I now allow a much bigger message to be read into memory and executed in memory before switching over to the large message transformation engine and start buffering to disk (which is always much slower).</p>
<h3>Problem #2 &#8211; forced to temp storage</h3>
<p>Looking at the model of the data flow again you probably see that I&#8217;m using the XML Disassembler to split the grouped files (step 5 to step 6).</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295groupingflow3-2.png"><img style="border-right-width: 0px; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" border="0" alt="GroupingFlow3" src="http://www.richardhallgren.com/wp-content/uploads/2008/07/windowslivewriterefficientgroupingandsplittingofbigfiles-8295groupingflow3-thumb.png" width="500" height="146" /></a></p>
<p>The only way I&#8217;ve found this to work is actually to write the Grouped XML message to file and the to read that file in to BizTalk again and in that receive pipeline debatch the message. Not the most elegant solution, but there really isn&#8217;t a another out-of-the-box way of debatching messages (the XML Assembler can&#8217;t do it) and I don&#8217;t want to use an orchestration to execute the a pipeline as I want to keep the solution pure messaging for simplicity and performance reasons.</p>
<h3>Finishing up</h3>
<p>Have you solved similar cases differently? I&#8217;d be very interested in your experience! I also have a sample solution of this &#8211; just send me an <a href="email:richard.hallgren@gmail.com" target="_blank">email</a> and make sure you&#8217;ll get it.</p>
<h3>Update</h3>
<p>Also don’t miss <a href="http://biztalkhotrod.com/Documents/BizTalk_HotRod_Issue6_Q2_2009.pdf" target="_blank">this issue</a> (pdf) of <a href="http://biztalkhotrod.com/" target="_blank">BizTalk Hotrod magazine</a>. There is an article on “Muenchian Grouping and Sorting using Xslt” describing exactly the problem discussed above.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/efficient-grouping-and-debatching-of-big-files-using-biztalk-2006/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Using BAM for latency tracking in a BizTalk request response scenario</title>
		<link>http://www.richardhallgren.com/using-bam-for-latency-tracking-in-a-biztalk-request-response-scenario/</link>
		<comments>http://www.richardhallgren.com/using-bam-for-latency-tracking-in-a-biztalk-request-response-scenario/#comments</comments>
		<pubDate>Mon, 28 Apr 2008 13:35:15 +0000</pubDate>
		<dc:creator>Richard</dc:creator>
				<category><![CDATA[BAM]]></category>
		<category><![CDATA[BizTalk 2006]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/using-bam-for-latency-tracking-in-a-biztalk-request-response-scenario/</guid>
		<description><![CDATA[This post will try and explain how BAM tracking can be used in SOAP based request response scenario in BizTalk 2006. It important to notice that some of the issues discussed in the post are specific to the SOAP adapter and are non-issues if the scenario would for example use the the WCF adapter or [...]]]></description>
			<content:encoded><![CDATA[<p>This post will try and explain how BAM tracking can be used in SOAP based request response scenario in BizTalk 2006. It important to notice that some of the issues discussed in the post are specific to the SOAP adapter and are non-issues if the scenario would for example use the the WCF adapter or similar.</p>
<h4>Describing the scenario</h4>
<p>In this case we have a SOAP receive port that receives a request and returns a response. The request is routed to a orchestration that calls three different send ports. These ports then sends new requests to back-end systems and returns responses (communication with back-ends systems are also SOAP based). The three responses are used to build up the final response that is then returned to original receive port as a final response.</p>
<p>Our goal is to track the duration between the request and response on each of the ports. The idea is also to find a solution and tracking model that doesn&#8217;t have to change if we add or remove ports or add similar processes to track.</p>
<h4><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bscenario-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="429" alt="scenario" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bscenario-thumb.jpg" width="520" border="0" /></a> </h4>
<h4>Defining and deploying the tracking model</h4>
<p>We&#8217;ll start by defining our tracking model in Excel. Our activity contains of the following items: </p>
<ul>
<li><b>InterchangeId</b> (data item)       <br />As we won&#8217;t correlate all the tracking point into one single row (that would break the goal of having one model for all processes, the model would then have to be specific for one process and it&#8217;s specific ports) the interchange id will then tell us what different rows belong together and that describes one process. </li>
<li><b>ReceivePortName</b> (data item)       <br />The name of the receive port. </li>
<li><b>Request</b> (milestone item)       <br />The time the request was either sent or received (depending on if we track a port that received/sent the request using a receive port or send port). </li>
<li><b>Response</b> (milestone item)       <br />The time the response was either sent or received (depending on if we track a port that received/sent it&#8217;s response on a receive port or send port). </li>
<li><b>SendPortName</b> (data item)       <br />The name of the send port. </li>
</ul>
<p>After we described the model it&#8217;s time to export it to an XML representation and then to use the <a href="http://technet.microsoft.com/en-us/library/aa547898.aspx" target="_blank">BM tool</a> to deploy it and generate the BAM database infrastructure. You&#8217;ll find some nice info on this <a href="http://msdn2.microsoft.com/en-us/library/aa558802.aspx" target="_blank">here</a>.</p>
<h4>Using the Tracking Profile Editor to bind the model</h4>
<p>Next step is to bind the model to the implementation using the <a href="http://msdn2.microsoft.com/en-us/library/ms946851.aspx" target="_blank">Tracking Profile Editor</a>. The figure below shows the different properties that were used. Notice that none of the items was bound to the actual orchestration context. All properties are general properties that we track <b>on the ports</b>.<b> </b>This is important as that gives us the possibility to just add and remove ports to change the tracking. </p>
<p><b><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-using-continuation-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="391" alt="tracking profile using continuation" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-using-continuation-thumb.jpg" width="500" border="0" /></a> </b></p>
<p><b></b></p>
<p>The next figure shows how the tracking of the request milestone event actually happens on either the RP1 port or on any of the three different send ports! If we developed a new process using other ports we could just add it here, no new model required. </p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-configure-ports-2-4.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="423" alt="tracking profile configure ports 2" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-configure-ports-2-thumb-1.jpg" width="500" border="0" /></a> </p>
<h4>What about the continuation then?</h4>
<p>Our final problem is that unless we somehow correlate our request tracking point with our receive tracking point the receive we&#8217;ll end up with each tracking point spread over several different rows. In the example below I&#8217;ve marked the request for the RP01 port and the response event on the same port.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bbam-portal-split-results-4.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="158" alt="bam portal split results" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bbam-portal-split-results-thumb-1.jpg" width="520" border="0" /></a> </p>
<p>The reason for this is of course that BAM doesn&#8217;t have a context for the two tracking points and doesn&#8217;t know that actually belongs together. This differs from tracking in a orchestration were we always are in a context (the context of the orchestration), it&#8217;s then easy for BAM to understand that we like to view all the tracking point as one row &#8211; when tracking on ports it&#8217;s different. Continuation helps us tell BAM that we like have a context and correlate these two points. </p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-using-continuation-2-6.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="381" alt="tracking profile using continuation 2" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0btracking-profile-using-continuation-2-thumb-2.jpg" width="500" border="0" /></a> </p>
<p>In our case ServiceID is the prefect candidate for correlating the two points. A request and a response will have the same service id. In an other situation we could just as well have used a value from inside the message (say for example an invoice id).</p>
<p><strong>The result is one single row for the request response for each port</strong>. So in our case a complete process (a complete interchange) is shown on four rows (one row for each of the ports). In the example below the first rows shows us the complete duration (and the other tracking data) between the request response to the client. The other rows show the duration for the send ports communication with the back-ends systems.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bbam-portal-complete-results-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="118" alt="bam portal complete results" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bbam-portal-complete-results-thumb.jpg" width="520" border="0" /></a></p>
<p>This model might not be optimal in an other scenario where your process are more fixed and you can then create a tracking model that is more specific to you actual process. But this solution meets our design goal as we&#8217;re now able to just add and remove port using the tracking profiler to track new port in completely new processes without having to go back and change the tracking model.</p>
<blockquote><p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bnote-2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 0px 0px 5px; border-right-width: 0px" height="14" alt="note" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterusingbamforlatencytrackinginabiztalkrequ-9c0bnote-thumb.gif" width="14" border="0" /></a>&#160; NOTE: When configuring BAM to track a port the <em><a href="http://msdn2.microsoft.com/en-us/library/aa561650.aspx" target="_blank">MessageType</a></em> is actually promoted. This causes some problems in combination with the SOAP based ports that have been published using the Web Services Publishing Wizard. Saravana writes about this <a href="http://www.digitaldeposit.net/blog/2007/08/soap-adapter-and-biztalk-web-publishing.html">here</a> and all his articles on this subject is a must read when working with SOAP ports. The problem however comes down to that the Web Services Publishing Wizard generates code that puts the wrong <em>DocumentSpecName </em>in the message context and that causes the <em>XmlDisassembler</em> to fail (it tricks the <em>XmlDisassembler</em> to look for a <em>MessageType</em> that doesn&#8217;t exists). </p>
<p>This usually isn&#8217;t a problem (unless you like to use a map on a port) but as BAM will force the port to promote the <em>MessageType</em> based on the <em>DocumentSpecName </em>we&#8217;ll have to fix this. Saravana has two solutions to the problem and I find the one that replaces the <em>DocumentSpecName</em> with a null value and lets the <em>XmlDisassembler</em> find the <em>MessageType</em> to work well.</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/using-bam-for-latency-tracking-in-a-biztalk-request-response-scenario/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Writing BizTalk context properties to a message from a WCF service using behaviors</title>
		<link>http://www.richardhallgren.com/writing-biztalk-context-properties-to-a-message-from-a-wcf-service-using-behaviors/</link>
		<comments>http://www.richardhallgren.com/writing-biztalk-context-properties-to-a-message-from-a-wcf-service-using-behaviors/#comments</comments>
		<pubDate>Thu, 24 Apr 2008 17:43:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[BizTalk 2006]]></category>
		<category><![CDATA[R2]]></category>
		<category><![CDATA[Sample code]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.richardhallgren.com/writing-biztalk-context-properties-to-a-message-from-a-wcf-service-using-behaviors/</guid>
		<description><![CDATA[The new WCF adapter in BizTalk 2006 R2 offers a lot of new possibilities. One of those is to write data to the BizTalk Message context properties directly from an exposed WCF Service. A practical use of this technic could be to write the username from the Windows credentials of the calling client into the [...]]]></description>
			<content:encoded><![CDATA[<p>The new WCF adapter in BizTalk 2006 R2 offers a lot of new possibilities. One of those is to write data to the BizTalk Message context properties directly from an exposed WCF Service. A practical use of this technic could be to write the username from the Windows credentials of the calling client into the context of the BizTalk message. This could be useful as this information is encrypted in messages that are received via the WCF adapter and isn&#8217;t possible to read when inside BizTalk. I&#8217;ll try and demonstrate the technique in this post.</p>
<p>If you have used the SOAP adapter before you might know that all you had to do was to turn on Windows based security for the exposed SOAP service and the username was automatically promoted to the context of the incoming BizTalk message. That username could then be used for routing, tracking which user called the service or using the value in plain text when communicating further to other connected systems. However using the WCF adapter this is not true anymore &#8211; when using the new <a href="http://msdn2.microsoft.com/en-us/library/ms789036.aspx" target="_blank">WCF Message Security model</a> the username and password is encrypted in the message and once the message is received by BizTalk it&#8217;s to late to read it. Basically we have to read the username in the actual service and write it into our own context property (that doesn&#8217;t get encrypted).</p>
<p>One way of achieving this is to read the username in the service and then to add it to the WCF Message Headers. All WCF message headers will by default be written to a the BizTalk Message context property called <a href="http://msdn2.microsoft.com/en-us/library/bb259987.aspx" target="_blank">InboundHeaders</a> (in the <em>http://schemas.microsoft.com/BizTalk/2006/1/Adapters/WCF-properties</em> namespace). First we&#8217;ll create an EndpointBehavior that will use a MessageInspector to add the username to the message header.&#160; Finally we create BehaviorExtensionElement so we can use a WCF Custom Binding in BizTalk and configure it to add our new behavior.</p>
<h1>Creating the new EndpointBehavior</h1>
<p>To create the configurable behavior we&#8217;ll need the three classes we mentioned above.</p>
<ol>
<li>A class that implements the <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.dispatcher.idispatchmessageinspector.aspx" target="_blank">IDispatchMessageInspector</a> interface to handle to reading and writing to the actual message. </li>
<li>A class that implements the <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.description.iendpointbehavior.aspx" target="_blank">IEndpointBehavior</a> interface to define what kind of endpoint we&#8217;re creating and what it should do. </li>
<li>A class that implements the <a href="http://msdn2.microsoft.com/en-us/library/system.servicemodel.configuration.behaviorextensionelement.aspx" target="_blank">BehaviorExtensionElement</a> abstract class to&#160; create the behavior and make it configurable. </li>
</ol>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:30a38abf-8115-47a6-9437-40a4eed2f89b" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.Collections.Generic;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.Text;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.ServiceModel;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.ServiceModel.Channels;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.ServiceModel.Dispatcher;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.ServiceModel.Description;
</span><span style="color: #0000FF; ">using</span><span style="color: #000000; "> System.ServiceModel.Configuration;

</span><span style="color: #0000FF; ">namespace</span><span style="color: #000000; "> CustomWCFProperties.Behavior
{
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;summary&gt;</span><span style="color: #008000; ">
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> PromoteUserNameMessageInspector implements IDispatchMessageInspector and adds the name from the WindowsIdentity to a WCF header called WindowsUserName in the </span><span style="color: #008000; text-decoration: underline; ">http://CustomWCFProperties.Schema</span><span style="color: #008000; "> namespace. BeforeSendReply only returns as we're not interested in handling the response.
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;/summary&gt;</span><span style="color: #808080; ">
</span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> PromoteUserNameMessageInspector : IDispatchMessageInspector
    {
        </span><span style="color: #0000FF; ">#region</span><span style="color: #000000; "> IDispatchMessageInspector Members</span><span style="color: #000000; ">

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">object</span><span style="color: #000000; "> AfterReceiveRequest(</span><span style="color: #0000FF; ">ref</span><span style="color: #000000; "> System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel, System.ServiceModel.InstanceContext instanceContext)
        {
            </span><span style="color: #0000FF; ">string</span><span style="color: #000000; "> windowsUserName </span><span style="color: #000000; ">=</span><span style="color: #000000; "> ServiceSecurityContext.Current.WindowsIdentity.Name;
            request.Headers.Add(MessageHeader.CreateHeader(</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">WindowsUserName</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">http://CustomWCFProperties.Schema</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">, windowsUserName));
            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">;
        }

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> BeforeSendReply(</span><span style="color: #0000FF; ">ref</span><span style="color: #000000; "> Message reply, </span><span style="color: #0000FF; ">object</span><span style="color: #000000; "> correlationState)
        {
            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;
        }

        </span><span style="color: #0000FF; ">#endregion</span><span style="color: #000000; ">
    }

    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;summary&gt;</span><span style="color: #008000; ">
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> PromoteUserNameBehavior implements IEndpointBehavior and adds a message inspector to the dispatch behavior. Doesn't use any binding parameters, doesn't validate any configuration etc and can't be used in a client (only in a service).
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;/summary&gt;</span><span style="color: #808080; ">
</span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> PromoteUserNameBehavior : IEndpointBehavior
    {
        </span><span style="color: #0000FF; ">#region</span><span style="color: #000000; "> IEndpointBehavior Members</span><span style="color: #000000; ">

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
        {
            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;
        }

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
        {
            </span><span style="color: #0000FF; ">throw</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> Exception(</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">The method or operation is not implemented.</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">);
        }

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
        {
            endpointDispatcher.DispatchRuntime.MessageInspectors.Add(</span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> PromoteUserNameMessageInspector());
        }

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">void</span><span style="color: #000000; "> Validate(ServiceEndpoint endpoint)
        {
            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; ">;
        }

        </span><span style="color: #0000FF; ">#endregion</span><span style="color: #000000; ">
    }

    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;summary&gt;</span><span style="color: #008000; ">
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> Defines the behavior.
    </span><span style="color: #808080; ">///</span><span style="color: #008000; "> </span><span style="color: #808080; ">&lt;/summary&gt;</span><span style="color: #808080; ">
</span><span style="color: #000000; ">    </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">class</span><span style="color: #000000; "> PromoteUserNameBehaviorElement : BehaviorExtensionElement
    {
        </span><span style="color: #0000FF; ">protected</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">override</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">object</span><span style="color: #000000; "> CreateBehavior()
        {
            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> PromoteUserNameBehavior();
        }

        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">override</span><span style="color: #000000; "> Type BehaviorType
        {
            </span><span style="color: #0000FF; ">get</span><span style="color: #000000; "> { </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">typeof</span><span style="color: #000000; ">(PromoteUserNameBehavior); }
        }
    }
}</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>Finally we have to sign the assembly using a strong key and add it to the GAC.</p>
<h1>Configure the machine.config</h1>
<p>As we need BizTalk and the WCF adapter to pick up the need behavior and make it possible to configure our receive port we need to to add the behavior element to the <a href="http://msdn2.microsoft.com/en-us/library/ms229697(VS.71).aspx" target="_blank">machine.config</a>. The easiest way of doing this is to use the new <a href="http://msdn2.microsoft.com/en-us/library/ms732009.aspx" target="_blank">WCF Service Configuration Editor tool</a> and point to the machine.config file.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4promoteusernamebehavior-gac-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="343" alt="PromoteUserNameBehavior GAC" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4promoteusernamebehavior-gac-thumb.jpg" width="500" border="0" /></a></p>
<p>After the dll been added and the machine.config file has been saved the the line below should have been added to the <a href="http://msdn2.microsoft.com/en-us/library/aa734726.aspx" target="_blank">&lt;behaviorExtensions&gt; element</a> (that is if you use the same strong name key as in the sample project I&#8217;ve linked here).</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:6bc92cdd-9ed1-4dfc-954f-4e60721cfaae" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #0000FF; ">&lt;</span><span style="color: #800000; ">add </span><span style="color: #FF0000; ">name</span><span style="color: #0000FF; ">=&quot;addCustomWCFProperties&quot;</span><span style="color: #FF0000; "> type</span><span style="color: #0000FF; ">=&quot;CustomWCFProperties.Behavior.PromoteUserNameBehaviorElement, AddCustomWCFPropertiesBehavior, Version=1.0.0.0, Culture=neutral, PublicKeyToken=705e34637fdffc54&quot;</span><span style="color: #FF0000; "> </span><span style="color: #0000FF; ">/&gt;</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
<h1>Create the BizTalk Receive Port and Receive Location</h1>
<p>Next thing to do is to start the BizTalk WCF Service Publishing Wizard. Choose to publish a service endpoint and make sure you enable metadata and create a receive location. In this example we&#8217;ll next choose to &quot;Publish schemas as WCF service&quot; and then define our service by naming service operations and so on.</p>
<p>When you then browse to the URL you choose to publish your service to you&#8217;ll see the nice example of how to instance the service you just defined. </p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4wsdl-code-example-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="210" alt="WSDL code example" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4wsdl-code-example-thumb.jpg" width="500" border="0" /></a> </p>
<p>If we then send a request message to service (you&#8217;ll find a client as part of the <a href="http://www.richardhallgren.com/blogfiles/CustomWCFProperties.zip" target="_blank">attached solution here</a>) and inspect the message and its context properties in BizTalk we&#8217;ll see that the username of the calling client is nowhere to be found.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-no-username-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="480" alt="Message No Username" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-no-username-thumb.jpg" width="499" border="0" /></a> </p>
<h1>Configure a WCF-Custom binding and adding a Endpoint Behavior</h1>
<p>To add the username to the message context we&#8217;ll need to add our newly created behavior to our service. We&#8217;ll do this by switch the service over to use a WCF-Custom binding to enable configuration. We then need to add the URL in the address field, define the binding type to a wsHttpBinding and to add our addCustomeWCFProperties behavior to the list of endpoint behaviors.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4add-endpoint-behavior-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="480" alt="Add Endpoint behavior" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4add-endpoint-behavior-thumb.jpg" width="483" border="0" /></a></p>
<blockquote>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4note-2.gif"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; margin: 0px 10px 10px 0px; border-right-width: 0px" height="14" alt="note" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4note-thumb.gif" width="14" align="left" border="0" /></a>&#160; NOTE: there is a limitation in the BizTalk WCF implementation in that you can&#8217;t create the WCF-Custom receive location that uses a HTTP in-process based binding (like the wsHttpBinding used in a WCF-Custom endpoint is) first and then use the WCF Publishing Wizard to only publish a metadata endpoint.</p>
<p><a href="http://www.topxml.com/code/cod-72_10213_biztalk-and-wcf-part-v-publishing-operations-patterns.aspx" target="_blank">Richard Seroter writes about it here</a> and I found the same thing to be true. </p>
<p><em>&quot;This error doesn&#8217;t have to do with mixing MEX endpoints and &#8220;regular&#8221; endpoints in the same IIS web site, but rather, creating MEX endpoints for in-process HTTP bindings seems to trigger this. <b>Note that an IIS-hosted MEX endpoint CAN be created for IIS-hosted HTTP endpoints, but not for in-process hosted HTTP endpoints.&quot;</b></em></p>
<p>If you however choose a different binding that Http or (as in this case) publishes the metadata first and then switches over to a custom binding you&#8217;re ok. </p>
</blockquote>
<p>If we then post another message to the service and inspect the message we&#8217;ll see that the behavior actually added a header and that it&#8217;s part of our BizTalk context properties. The adapter is also smart enough to know that this header isn&#8217;t part of the original headers and therefore stores in it&#8217;s own field within the context properties (you&#8217;ll find as part of the InboundHeaders block as well).</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-username-4.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="221" alt="Message Username" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-username-thumb-1.jpg" width="500" border="0" /></a></p>
<p>One problem remains &#8211; the actual value of the user is nested inside a XML node and the property isn&#8217;t promoted.&#160; </p>
<h1>Extract and promote the value</h1>
<p>To extract and promote the value we use an old fashion pipeline component using the following code in the execute method (the complete project is part of the <a href="http://www.richardhallgren.com/blogfiles/CustomWCFProperties.zip" target="_blank">downloadable sample project</a>).</p>
</p>
<div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5ad57ba1-51bc-4d4d-a29e-20f827ede0e3" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px">
<pre style="background-color:White;white-space:-moz-pre-wrap; white-space: -pre-wrap; white-space: -o-pre-wrap; white-space: pre-wrap; word-wrap: break-word;;overflow: auto;">
<div><!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--><span style="color: #000000; ">        </span><span style="color: #0000FF; ">public</span><span style="color: #000000; "> IBaseMessage Execute(IPipelineContext pc, IBaseMessage inmsg)
        {
            StringReader reader </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> StringReader(inmsg.Context.Read(</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">WindowsUserName</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">http://CustomWCFProperties.Schema</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">).ToString());

            </span><span style="color: #0000FF; ">if</span><span style="color: #000000; "> (reader </span><span style="color: #000000; ">!=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">null</span><span style="color: #000000; ">)
            {
                XPathDocument document </span><span style="color: #000000; ">=</span><span style="color: #000000; "> </span><span style="color: #0000FF; ">new</span><span style="color: #000000; "> XPathDocument(reader);
                XPathNavigator navigator </span><span style="color: #000000; ">=</span><span style="color: #000000; "> document.CreateNavigator();
                </span><span style="color: #0000FF; ">string</span><span style="color: #000000; "> value </span><span style="color: #000000; ">=</span><span style="color: #000000; "> navigator.SelectSingleNode(</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">/</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">).Value;
                inmsg.Context.Promote(</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">WindowsUserName</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">, </span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">http://CustomWCFProperties.Schema</span><span style="color: #000000; ">&quot;</span><span style="color: #000000; ">, value);
            }

            </span><span style="color: #0000FF; ">return</span><span style="color: #000000; "> inmsg;
        }</span></div>
</pre>
<p><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin.  http://dunnhq.com --></div>
</p>
<p>All the component does is reading the XML node the value exists inside and then it reads the actual value. Finally it writes the value back and promotes it. To be able to promote the value we also have to have a <a href="http://msdn2.microsoft.com/en-us/library/aa561059.aspx" target="_blank">Property Schema</a> deployed with a corresponding property name and namespace (WindowsUser and <em>http://CustomeWCFProperties.Schema</em> in this case).</p>
<p>The end results looks something like this.</p>
<p><a href="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-promoted-username-2.jpg"><img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="206" alt="Message Promoted Username" src="http://www.richardhallgren.com/wp-content/uploads/2008/04/windowslivewriterwritingbiztalkcontextpropertiestoamessag-72f4message-promoted-username-thumb.jpg" width="504" border="0" /></a> </p>
<p>The username is extracted and promoted and available for example for tracking or to for example use in a routing scenario. </p>
<p>This technique could of course be used for all kinds of scenarios where you like to add information to the context properties and could potentially replace a lot of the classic scenarios for custom pipelines.</p>
<p>All kind of comments are of course more than welcome!</p>
<p><a href="http://www.richardhallgren.com/blogfiles/CustomWCFProperties.zip" target="_blank">Download the sample solution&#160; here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.richardhallgren.com/writing-biztalk-context-properties-to-a-message-from-a-wcf-service-using-behaviors/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
