<?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>Basildon Coder &#187; .Net</title>
	<atom:link href="http://basildoncoder.com/blog/category/development/coding/dotnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://basildoncoder.com/blog</link>
	<description>Incoherent and disjointed opinionated drivel from somewhere near London</description>
	<lastBuildDate>Fri, 11 Dec 2009 13:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Marshalling a Variable-Length Array From Unmanaged Code In C#</title>
		<link>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/</link>
		<comments>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/#comments</comments>
		<pubDate>Tue, 31 Mar 2009 19:03:38 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Data Structures]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/</guid>
		<description><![CDATA[I recently spent time working on some C# code to interact with a simple DNS-SD system. This requires using DNS TXT records, which are not supported in the System.Net.Dns class. After a few google searches failed to turn up a pure .Net client library that met my needs, I settled on an approach based around [...]]]></description>
			<content:encoded><![CDATA[<p>I recently spent time working on some C# code to interact with a simple <a href="http://files.dns-sd.org/draft-cheshire-dnsext-dns-sd.txt">DNS-SD</a> system. This requires using <a href="http://en.wikipedia.org/wiki/List_of_DNS_record_types">DNS TXT records</a>, which are not supported in the <a href="http://msdn.microsoft.com/en-us/library/system.net.dns.aspx">System.Net.Dns</a> class. After a few google searches failed to turn up a pure .Net client library that met my needs, I settled on an approach based around p/invoking the Win32 <a href="http://msdn.microsoft.com/en-us/library/ms682016(VS.85).aspx">DnsQuery</a> function.</p>
<p>And quickly ran into problems.</p>
<p>For DNS TXT records, DnsQuery returns a <a href="http://msdn.microsoft.com/en-us/library/ms682109(VS.85).aspx">DNS_TXT_DATA</a> structure in the Data field of the <a href="http://msdn.microsoft.com/en-us/library/ms682082(VS.85).aspx">DNS_RECORD</a> structure. DNS_TXT_DATA is declared like this:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">typedef</span> <span style="color: #993333;">struct</span> <span style="color: #009900;">&#123;</span>
    DWORD dwStringCount<span style="color: #339933;">;</span>
    PWSTR pStringArray<span style="color: #009900;">&#91;</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> DNS_TXT_DATA<span style="color: #339933;">,</span>
    <span style="color: #339933;">*</span>PDNS_TXT_DATA<span style="color: #339933;">;</span></pre></div></div>

<p>Using the very handy <a href="http://clrinterop.codeplex.com/">P/Invoke Interop Assistant</a>, we see that this struct can be represented like this in managed code:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>StructLayout<span style="color: #000000;">&#40;</span>LayoutKind.<span style="color: #0000FF;">Sequential</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">struct</span> DNS_TXT_DATA <span style="color: #000000;">&#123;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// DWORD-&gt;unsigned int</span>
    <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">uint</span> dwStringCount<span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">/// PWSTR[1]</span>
    <span style="color: #000000;">&#91;</span>MarshalAs<span style="color: #000000;">&#40;</span>UnmanagedType.<span style="color: #0000FF;">ByValArray</span>,
            SizeConst<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span>,
            ArraySubType<span style="color: #008000;">=</span>UnmanagedType.<span style="color: #0000FF;">SysUInt</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span>
    <span style="color: #0600FF;">public</span> IntPtr<span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> pStringArray<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>There is a problem with pStringArray, unfortunately. The <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal.aspx">System.Runtime.InteropServices.Marshal</a> class cannot marshal a variable length array, as it needs to know in advance how big the array is in order to allocate memory. That&#8217;s why the managed structure needs SizeConst specified in the MarshalAs attribute.</p>
<p>However, if the DNS TXT record data contains multiple quoted strings separated by whitespace, DnsQuery will return a structure with a variable number of elements in pStringArray. Since SizeConst is set at compile-time, when we marshal this into the managed struct defined above, we only get the first element in our single-element array. Rats.</p>
<p>More googling turned up very little info on dealing with this, though I found indications that others had run into the same problem without finding a satisfactory conclusion. DnsQuery is not the only Win32 function that returns variable-length arrays, and p/invoking any of the others has the same issue.</p>
<p>Simply declaring SizeConst to be bigger than we need &#8211; &#8220;hey, I know I&#8217;ll never get more than 10 or so strings back, so why not declare SizeConst to be 128?&#8221; &#8211; is inelegant (hardcoded upper limits, ugh) and doesn&#8217;t work properly anyway. Since the struct layout is sequential the marshaller will copy over (e.g.) 128*sizeof(IntPtr) sequential bytes (a total of 512 bytes, in this case). That much memory was never allocated on the unmanaged side, so we end up with a load of junk in the tail of pStringArray, and more often than not the marshaller chokes on this junk and throws an AccessViolationException. Fun.</p>
<p>There IS a way to get round the problem, though. I&#8217;m not sure it&#8217;s the best way, but it works and seems stable, so I thought I&#8217;d throw it out there in case anyone else can use it (or maybe explain to me why it&#8217;s an unsafe stupid thing to do&#8230;)</p>
<p>Basically, since we&#8217;re dealing with sequential memory, we can use Marshal.PtrToStructure to marshal the DNS_TXT_DATA structure as defined above, then use pointer arithmetic to gain access to any further data that needs marshalling.</p>
<p>Pointer arithmetic? Oh yes, even in the safe and secure world of managed code it&#8217;s sometimes still necessary to get our hands dirty, and situations like this illustrate that it will always be valuable to have some hard-earned Assembly/C/C++ war wounds.</p>
<p>So, assuming we have valid p/invoke declarations and data structures (I&#8217;ve included a complete source program below), DnsQuery is called like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var pServers <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
var ppQueryResultsSet <span style="color: #008000;">=</span> IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #008000;">;</span>
var ret <span style="color: #008000;">=</span> DnsQuery<span style="color: #000000;">&#40;</span>domain,
        DnsRecordType.<span style="color: #0000FF;">TEXT</span>,
        DnsQueryType.<span style="color: #0000FF;">STANDARD</span>,
        pServers,
        <span style="color: #0600FF;">ref</span> ppQueryResultsSet,
        IntPtr.<span style="color: #0000FF;">Zero</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>ret <span style="color: #008000;">!=</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
    <span style="color: #0600FF;">throw</span> <span style="color: #008000;">new</span> ApplicationException<span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;DnsQuery failed: &quot;</span> <span style="color: #008000;">+</span> ret<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>If we examine the memory location of ppQueryResultsSet (Ctrl-Alt-M,1 or Debug-&gt;Windows-&gt;Memory-&gt;Memory1 in Visual Studio) we&#8217;ll see something like the following (actual address locations may vary &#8211; just copy the int value of ppQueryResultsSet to the Address bar of the memory window):</p>
<pre>0x049E0878  00 00 00 00  ....
0x049E087C  b8 09 9e 04  ¸.ž.
0x049E0880  10 00 20 00  .. .
0x049E0884  19 30 00 00  .0..
0x049E0888  00 00 00 00  ....
0x049E088C  00 00 00 00  ....
0x049E0890  06 00 00 00  ....
0x049E0894  b8 08 9e 04  ¸.ž.
0x049E0898  d8 08 9e 04  Ø.ž.
0x049E089C  f8 08 9e 04  ø.ž.
0x049E08A0  28 09 9e 04  (.ž.
0x049E08A4  68 09 9e 04  h.ž.
0x049E08A8  88 09 9e 04  ˆ.ž.</pre>
<p>I&#8217;ve set the column size to 4 here, as most of the values we are dealing with are 4 bytes in size. This effectively shows one value per line.</p>
<p>The first 6 rows (24 bytes) correspond to the DNS_RECORD structure up until (but not including) the DNS_TXT_DATA structure in DNS_RECORD&#8217;s Data union. We can marshal this first structure without problem:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var dnsRecord <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DnsRecord<span style="color: #000000;">&#41;</span> Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>
        ppQueryResultsSet, <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>DnsRecord<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The DNS_TXT_DATA structure starts at address 0x049E0890 in my example. Having already marshalled the DNS_RECORD structure, now I want a pointer to the DNS_TXT_DATA structure. I can do this by creating a new pointer at the address of ppQueryResultsSet plus 24 bytes, and marshalling again:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var ptr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#40;</span>
        ppQueryResultsSet.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> Marshal.<span style="color: #008000;">SizeOf</span><span style="color: #000000;">&#40;</span>dnsRecord<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
var txtData <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>DNS_TXT_DATA<span style="color: #000000;">&#41;</span> Marshal.<span style="color: #0000FF;">PtrToStructure</span><span style="color: #000000;">&#40;</span>
        ptr, <span style="color: #008000;">typeof</span> <span style="color: #000000;">&#40;</span>DNS_TXT_DATA<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Because of the definition of DNS_TXT_DATA, this only marshals 8 bytes &#8211; 4 bytes for dwStringCount, and 4 bytes for the single element in pStringArray (an IntPtr). Since we know the memory is sequential, however, this gives us everything we need &#8211; we now know how many strings have been received (6 in this case, as indicated at 0x049E0890), and the location of the pointer to the first string (0x049E0894).</p>
<p>With this info, we can marshal all the pointers into an array with a length of dwStringCount:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">ptr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#40;</span>ptr.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">sizeof</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">uint</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// move to first</span>
var ptrs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> IntPtr<span style="color: #000000;">&#91;</span>txtData.<span style="color: #0000FF;">dwStringCount</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// dest array</span>
Marshal.<span style="color: #0000FF;">Copy</span><span style="color: #000000;">&#40;</span>ptr, ptrs, <span style="color: #FF0000;">0</span>, ptrs.<span style="color: #0000FF;">Length</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>And finally we iterate through those pointers, marshalling the string pointed at by each:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var strings <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> List<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">string</span><span style="color: #008000;">&gt;</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span>var i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> ptrs.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span> <span style="color: #008000;">++</span>i<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    strings.<span style="color: #0000FF;">Add</span><span style="color: #000000;">&#40;</span>Marshal.<span style="color: #0000FF;">PtrToStringAnsi</span><span style="color: #000000;">&#40;</span>ptrs<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>While the example I&#8217;ve presented here is specific to DnsQuery, the general approach should be applicable to any situation where you need to marshal a data structure containing a variable-length array.</p>
<p><a href="http://basildoncoder.com/marshal-variable-length-array.cs">Source code</a></p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/03/31/marshalling-a-variable-length-array-from-unmanaged-code-in-c/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Project Euler Problem 8</title>
		<link>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/</link>
		<comments>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/#comments</comments>
		<pubDate>Mon, 09 Feb 2009 19:30:13 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Project Euler]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/</guid>
		<description><![CDATA[Problem 8 &#8220;Find the greatest product of five consecutive digits in the 1000-digit number. 73167176531330624919225119674426574742355349194934 96983520312774506326239578318016984801869478851843 85861560789112949495459501737958331952853208805511 12540698747158523863050715693290963295227443043557 66896648950445244523161731856403098711121722383113 62229893423380308135336276614282806444486645238749 30358907296290491560440772390713810515859307960866 70172427121883998797908792274921901699720888093776 65727333001053367881220235421809751254540594752243 52584907711670556013604839586446706324415722155397 53697817977846174064955149290862569321978468622482 83972241375657056057490261407972968652414535100474 82166370484403199890008895243450658541227588666881 16427171479924442928230863465674813919123162824586 17866458359124566529476545682848912883142607690042 24219022671055626321111109370544217506941658960408 07198403850962455444362981230987879927244284909188 84580156166097919133875499200524063689912560717606 05886116467109405077541002256983155200055935729725 71636269561882670428252483600823257530420752963450&#8243; The first step here is to find a representation for that fairly humungous number. Obviously it&#8217;s not going to fit into [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://projecteuler.net/index.php?section=problems&amp;id=8"><em><strong>Problem 8</strong></em></a></p>
<blockquote><p>&#8220;Find the greatest product of five consecutive digits in the 1000-digit number.</p>
<p>73167176531330624919225119674426574742355349194934<br />
96983520312774506326239578318016984801869478851843<br />
85861560789112949495459501737958331952853208805511<br />
12540698747158523863050715693290963295227443043557<br />
66896648950445244523161731856403098711121722383113<br />
62229893423380308135336276614282806444486645238749<br />
30358907296290491560440772390713810515859307960866<br />
70172427121883998797908792274921901699720888093776<br />
65727333001053367881220235421809751254540594752243<br />
52584907711670556013604839586446706324415722155397<br />
53697817977846174064955149290862569321978468622482<br />
83972241375657056057490261407972968652414535100474<br />
82166370484403199890008895243450658541227588666881<br />
16427171479924442928230863465674813919123162824586<br />
17866458359124566529476545682848912883142607690042<br />
24219022671055626321111109370544217506941658960408<br />
07198403850962455444362981230987879927244284909188<br />
84580156166097919133875499200524063689912560717606<br />
05886116467109405077541002256983155200055935729725<br />
71636269561882670428252483600823257530420752963450&#8243;</p></blockquote>
<p>The first step here is to find a representation for that fairly humungous number. Obviously it&#8217;s not going to fit into a paltry 32-bit int&#8230;but then we don&#8217;t need it to. The problem description requires us to think in terms of smaller (5-digit) numbers, not one giant 1000-digit number.</p>
<p>So, it is sufficient for us to consider the number as an enumerable stream of single digits, which we can conveniently represent as IEnumerable<int>. I could use a macro to convert the number into a collection initialiser, but it&#8217;s much easier to treat the string as an IEnumerable<char>&lt;char&gt; and let LINQ do the heavy lifting.</char></int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var nums <span style="color: #008000;">=</span> Enumerable.<span style="color: #0000FF;">AsEnumerable</span><span style="color: #000000;">&#40;</span>
        <span style="color: #666666;">&quot;73167176531330624919225119674426574742355349194934&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;96983520312774506326239578318016984801869478851843&quot;</span> <span style="color: #008000;">+</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// ..... etc etc ......</span>
&nbsp;
        <span style="color: #666666;">&quot;05886116467109405077541002256983155200055935729725&quot;</span> <span style="color: #008000;">+</span>
        <span style="color: #666666;">&quot;71636269561882670428252483600823257530420752963450&quot;</span>
        <span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Select</span><span style="color: #000000;">&#40;</span>x <span style="color: #008000;">=&gt;</span> Convert.<span style="color: #0000FF;">ToInt32</span><span style="color: #000000;">&#40;</span>x.<span style="color: #0000FF;">ToString</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This gives us an IEnumerable<int>&lt;int&gt; containing every digit in the 1000-digit number. Now, the &#8216;obvious&#8217; way to solve the problem is to iterate through the collection, and at each index multiply the value against the next four indexes. A simple loop should deal with it:</int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">int</span> SimpleSolver<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ints<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> max <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">for</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span> i <span style="color: #008000;">&lt;</span> ints.<span style="color: #0000FF;">Length</span> <span style="color: #008000;">-</span> <span style="color: #FF0000;">4</span><span style="color: #008000;">;</span> i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> tmp <span style="color: #008000;">=</span> ints<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span>
            <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">3</span><span style="color: #000000;">&#93;</span> <span style="color: #008000;">*</span> ints<span style="color: #000000;">&#91;</span>i <span style="color: #008000;">+</span> <span style="color: #FF0000;">4</span><span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        max <span style="color: #008000;">=</span> Math.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span>max, tmp<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #0600FF;">return</span> max<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>As ever, though, that&#8217;s pretty ugly &#8211; the loop condition and product calculation is tied to the sequence size of 5, and messing with an index variable is tedious.</p>
<p>An alternative approach is to take advantage of LINQ&#8217;s Skip and Take methods to split the problem domain into overlapping &#8216;slices&#8217;. Similar to the for loop above, the core of the approach is to iterate through the digits, and at each digit grab a number of subsequent digits and calculate the product.</p>
<p>Lets look at the 5-digit slices available from the first 10 digits:</p>
<pre>
  7   3   1   6   7   1   7   6   5   3
|       73167       |
    |       31671       |
        |       16717       |
            |       67176       |
                |       71765       |
                    |       17653       |</pre>
<p>We can use Skip to progressively move the starting index forward, and Take to grab the 5 digits we need. So, starting with i=0, each successive slice can be sliced from the whole with:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var slice <span style="color: #008000;">=</span> ints.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span>i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>To calculate the product of the digits in the slice, we can use the Aggregate operation:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">slice.<span style="color: #0000FF;">Aggregate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>, <span style="color: #000000;">&#40;</span>curr, next<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> curr<span style="color: #008000;">*</span>next<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>We&#8217;ve met Aggregate before &#8211; it&#8217;s basically a fold, which collapses a sequence to a single item by repeatedly applying an operation to an accumulating result.</p>
<p>This can all be wrapped up as an iterator block, like so:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">private</span> <span style="color: #0600FF;">static</span> IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> EnumerateSlices<span style="color: #000000;">&#40;</span>
        IEnumerable<span style="color: #008000;">&lt;</span><span style="color: #FF0000;">int</span><span style="color: #008000;">&gt;</span> ints, <span style="color: #FF0000;">int</span> sliceSize<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">int</span> i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        var slice <span style="color: #008000;">=</span> ints.<span style="color: #0000FF;">Skip</span><span style="color: #000000;">&#40;</span>i<span style="color: #008000;">++</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Take</span><span style="color: #000000;">&#40;</span>sliceSize<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>slice.<span style="color: #0000FF;">Count</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&lt;</span> sliceSize<span style="color: #000000;">&#41;</span>
            yield break<span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// end</span>
&nbsp;
        yield <span style="color: #0600FF;">return</span> slice.<span style="color: #0000FF;">Aggregate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1</span>,
                <span style="color: #000000;">&#40;</span>curr, next<span style="color: #000000;">&#41;</span> <span style="color: #008000;">=&gt;</span> curr<span style="color: #008000;">*</span>next<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Note the termination condition &#8211; when we have enumerated every slice, our next slice will contain only 4 elements (3, 4, 5, and 0 from the end of the sequence) &#8211; that&#8217;s our cue to exit the loop.</p>
<p>Also note that this approach makes the algorithm trivial to parameterize &#8211; it will work just as well with slice sizes other than 5.</p>
<p>This iterator will produce an IEnumerable<int> containing the products of all slices, so the final step is to select the largest:</int></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">var result <span style="color: #008000;">=</span> EnumerateSlices<span style="color: #000000;">&#40;</span>nums, <span style="color: #FF0000;">5</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">Max</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/02/09/project-euler-problem-8/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Macros: You Oughta Know</title>
		<link>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/</link>
		<comments>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/#comments</comments>
		<pubDate>Fri, 06 Feb 2009 15:03:06 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Refactoring]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/</guid>
		<description><![CDATA[One of the most useful tools available in any decent text editor is the macro recorder, but it&#8217;s criminally underused. It seems most people either don&#8217;t know the functionality exists, or simply ignore it. This is a shame, since it&#8217;s a great timesaver. I don&#8217;t know why macros are so underused. It might be a [...]]]></description>
			<content:encoded><![CDATA[<p>One of the most useful tools available in any decent text editor is the macro recorder, but it&#8217;s criminally underused. It seems most people either don&#8217;t know the functionality exists, or simply ignore it. This is a shame, since it&#8217;s a great timesaver.</p>
<p>I don&#8217;t know why macros are so underused. It might be a mindset thing &#8211; it can take a little while to develop the ability to spot repetitive editing tasks quickly (i.e. not when you&#8217;re 75% of the way through thinking <em>dang, I have to do this again?</em>), so maybe many people never quite make the leap.</p>
<p>It&#8217;s worth it though, because once you get your eye in you see chances to use macros everywhere.</p>
<p>I had a useful example just yesterday, in which I needed to make a change to a colossal switch statement (220 branches! Run the cyclomatic-complexity doohickey on THAT!) and had no unit tests to fall back on.</p>
<p>If I had to modify (and hopefully refactor) such a huge construct I wanted to be able to compare before-and-after test results, but I didn&#8217;t much fancy hand-cranking a few hundred unit tests.</p>
<p>By recording a temporary macro, however, it took just a couple of minutes to cover every branch. I&#8217;ve decided to post a detailed walkthrough of the process here in the hopes that a fairly simple example will be illustrative for those that don&#8217;t already lean heavily on macros.</p>
<p>Note that <em>this is not an advanced tutorial</em>. Please refrain from leaving snarky comments about how macros are so much more powerful than this &#8211; I&#8217;m just doing some introductory material here <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Here is a representative snippet of the C# source. It&#8217;s part of a legacy permissioning system that, under certain circumstances, needs to check for the existence of a permission represented by an enum against a permission table containing a string-based hierarchy (application/role/permission). The code I was modifying did the appropriate conversion:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityRoleManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;RoleManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityRoleManagementModify</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;RoleManagement&quot;</span>, <span style="color: #666666;">&quot;Modify&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityUserManagementDelete</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;UserManagement&quot;</span>, <span style="color: #666666;">&quot;Delete&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityPermissionManagementAdd</span><span style="color: #008000;">:</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;PermissionManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span></pre></div></div>

<p>I needed to add a couple of branches to this, but I also wanted to tidy up the code by removing the superfluous braces, as a precursor to converting it into something a bit more robust and maintainable. I wanted unit test coverage to give me confidence that I hadn&#8217;t mucked up some logic and inadvertantly granted admin access to the helpdesk trainee role or something.</p>
<p>So, I copied the entire switch body into Notepad++ (well, vim really, but I&#8217;ll pretend it&#8217;s Notepad++ for the sake of making this post a bit more accessible) and set to work<sup>[1]</sup>.</p>
<p>Before recording my macro, I needed to do a bit of preprocessing to trim the code down to just the data I wanted to work with. The following steps show the &#8216;find&#8217; regexes I used (in each case, the value of the replace field was empty, so these are effectively deletes), and the effect on the first switch branch from the list above:</p>
<p>1) Remove opening and closing braces from every switch branch:</p>
<pre>^\s+[\{\}]$</pre>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
&nbsp;
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>2) Remove blanks &#8211; TextFX/Edit/Delete Blank Lines</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">    <span style="color: #0600FF;">case</span> PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>3) Remove case statements and leading whitespace:</p>
<pre>^\s+case\s+</pre>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #008000;">:</span>
        <span style="color: #0600FF;">return</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> <span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>4) Remove colon from end of case statement:</p>
<pre>:$</pre>
<p>5) Remove return statement and leading whitespace:</p>
<pre>^\s+return new string\[\]\s*</pre>
<p>I ended up with a sequence of couplets looking similar to this one:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span>
<span style="color: #000000;">&#123;</span><span style="color: #666666;">&quot;Security&quot;</span>, <span style="color: #666666;">&quot;ParameterManagement&quot;</span>, <span style="color: #666666;">&quot;Add&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Now the fun starts &#8211; lets walk through the process.</p>
<p>We want to convert the first couplet into a simple unit test fixture, and record the process. This will be our macro &#8211; the instructions for converting one couplet into one unit test. We can then play the macro multiple times to convert all the others effortlessly.</p>
<p>Start by moving the cursor to the start of the line, before the &#8216;P&#8217; of PermissionKey. This is the start point of the macro, so for the macro to be repeatable we must make sure that we finish recording the macro in perfect position to run it again, i.e. before the &#8216;P&#8217; of PermissionKey for the next couplet (column 0 line 3). Hit Ctrl-Shift-R to start recording.</p>
<p>It is important not to use the mouse when editing &#8211; stick to the keyboard. It&#8217;s also important not to record keystrokes that are too specific to one bit of code. For instance, don&#8217;t use the arrow keys to move left and right character-by-character, because it won&#8217;t work on longer or shorter lines.</p>
<p>Instead, use the Home and End keys to jump to the start or end of the line, and hold Ctrl whilst arrowing left or right to move a word at a time instead of a character at a time (this is one of the areas where vim&#8217;s movement commands really differentiate it from wannabes like Notepad++&#8230;but I digress). See the &#8216;Detailed Instructions&#8217; section below for more information.</p>
<p>Assume the original switch body is in a method called &#8216;LookupEnumPermission&#8217;. The couplet should be edited to look like this (without the linewrap&#8230;):</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #000000;">&#91;</span>Test<span style="color: #000000;">&#93;</span>
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> TestSecurityParameterManagementAdd<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> result <span style="color: #008000;">=</span> LookupEnumPermission<span style="color: #000000;">&#40;</span>
            PermissionKey.<span style="color: #0000FF;">SecurityParameterManagementAdd</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Security&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">0</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;ParameterManagement&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    Assert.<span style="color: #0000FF;">AreEqual</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Add&quot;</span>, result<span style="color: #000000;">&#91;</span><span style="color: #FF0000;">2</span><span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>Make sure you finish by moving the cursor into position for the next couplet, and hit Ctrl-Shift-R again to stop recording.</p>
<p>Now, hit Ctrl-Shift-P to play back the macro. If you&#8217;ve done everything right, the next couplet should magically format itself into a unit test. Hit Ctrl-Shift-P again, and the next couplet will change too. Under the Macro menu, select &#8216;Run a macro multiple times&#8230;&#8217; and you can enter a fixed number of iterations, or just apply the macro over and over again until the end of the file is reached.</p>
<p>Finally, you can copy the unit tests into a new or existing test fixture, and you&#8217;re done! In much less time (hopefully) and with fewer errors than if the tests had been written one-by-one.</p>
<p><em><strong>Detailed Instructions:</strong></em></p>
<p>These are ley-by-key instructions in Notepad++, in case something in the description above is unclear. Visual Studio should be similar. Vim will be faster once you&#8217;ve learned how, but I&#8217;ll assume if you use vim you&#8217;re already au fait with this sort of editing <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<ol>
<li>Type [Test], and hit enter to start a new line.</li>
<li> Type &#8216;public void Test&#8217; and hit Enter.</li>
<li> Type &#8216;{&#8216; and hit Enter, then Tab.</li>
<li> Hold Ctrl and tap the right arrow twice to jump over a couple of words and place the cursor at the start of the word SecurityParameterManagementAdd, then hold Ctrl-Shift and right arrow again to select the word. Ctrl-C to copy, then arrow up two lines and paste it after the word &#8216;Test&#8217; to create the full function name TestSecurityParameterManagementAdd. Type () for the empty parameter list.</li>
<li> Arrow down two lines and hit Home to jump to the start of the line. Type &#8216;string[] result = LookupEnumPermission(&#8216;, then hit End to jump to the end of the line and type &#8216;);&#8217;.</li>
<li> Arrow down one line, hit Home, then Tab. Type &#8216;Assert.Equals(&#8216; then hit Delete to remove the &#8216;{&#8216;. Hold Ctrl and move right three times (to move the cursor just past the comma) and type &#8216;result[0]);&#8217; and hit Enter.</li>
<li> Repeat variations of step 7 a couple of times to convert the next two lines. Remember to use the correct indexes (result[1] and result[2]). Hit Enter after the last line and type &#8216;}&#8217; to close the function body.</li>
<li> Arrow down one line and hit Home to place the cursor at the correct start position for the next couplet, and end the macro by hitting Ctrl-Shift-R again.</li>
</ol>
<p><sup>[1]</sup>I could have just done this in a new file in Visual Studio, but for some reason I find VS intolerably slow at running macros once recorded. So slow, in fact, that you can watch the cursor laboriously complete each step &#8211; I wind up thinking it would have been quicker to do it manually. That might just be something odd about my VS installation though, as no-one else seems to think it&#8217;s slow.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2009/02/06/macros-you-oughta-know/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Look Before You Look Before You Leap</title>
		<link>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/</link>
		<comments>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/#comments</comments>
		<pubDate>Fri, 24 Oct 2008 14:32:36 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/</guid>
		<description><![CDATA[Generally, I try to avoid turning this blog into some sort of snark-fest about other programmers or blogs. I&#8217;ve disagreed with Jeff Atwood once or twice though, and so by posting this I&#8217;m probably straying a little close to the edge&#8230;but what the hell. A couple of days ago Coding Horror carried a fluff piece [...]]]></description>
			<content:encoded><![CDATA[<p>Generally, I try to avoid turning this blog into some sort of <a href="http://www.google.co.uk/search?q=define%3Asnark">snark</a>-fest about other programmers or blogs. I&#8217;ve disagreed with Jeff Atwood <a href="http://basildoncoder.com/blog/2008/02/22/code-can-be-beautiful/">once</a> or <a href="http://basildoncoder.com/blog/2008/01/29/freedom-zero-the-all-or-nothing-fallacy/">twice</a> though, and so by posting this I&#8217;m probably straying a little close to the edge&#8230;but what the hell.</p>
<p>A couple of days ago Coding Horror carried a <a href="http://www.codinghorror.com/blog/archives/001177.html">fluff piece</a> about how all developers should be marketers too. Predictably, the article soon got posted to <a href="http://www.reddit.com/r/programming/">proggit</a> where it was <a href="http://www.reddit.com/r/programming/comments/78vq3/jeff_atwood_finally_jumps_the_shark/">ripped on by reddit&#8217;s resident Jeff-haters</a>, and even more predictably the comments were a mix of interesting insight and barely-concealed hate.</p>
<p>Apparently some of them got up Jeff&#8217;s nose a bit, and today he <a href="http://www.codinghorror.com/blog/archives/001178.html">responded</a>. The core of his rebuttal seems to be that you shouldn&#8217;t trust what you read on blogs, and should verify everything yourself. True enough, I guess, if perhaps a bit impractical given the sheer amount of information out there.</p>
<p>Then, however, Jeff goes on to give an example by referencing a <a href="http://blog.madskristensen.dk/post/Compression-and-performance-GZip-vs-Deflate.aspx">compression benchmark</a> he&#8217;d read on a blog and providing counter-analysis to show that the benchmark was wrong in claiming Deflate is faster than gzip. In doing so, much knowledge was gained.</p>
<p>Or so we are told.</p>
<p>The comment thread quickly becomes a goldmine of humour. Bugs in Jeff&#8217;s benchmarking code (not resetting the stopwatch) meant that the durations were cumulative, not independent, with inevitable distortion of the results. Another commenter pointed out that <a href="http://en.wikipedia.org/wiki/Gzip">gzip</a> cannot possibly be faster than Deflate, since the gzip algorithm IS the Deflate algorithm plus some additional computation.</p>
<blockquote><p>“gzip” is often also used to refer to the gzip file format, which is:</p>
<ul>
<li>a 10-byte header, containing a magic number, a version number and a timestamp</li>
<li>optional extra headers, such as the original file name,</li>
<li>a body, containing a DEFLATE-compressed payload</li>
<li>an 8-byte footer, containing a CRC-32 checksum and the length of the original uncompressed data</li>
</ul>
<p align="right"><a href="http://en.wikipedia.org/wiki/Gzip">http://en.wikipedia.org/wiki/Gzip</a></p>
</blockquote>
<p>With the benchmarking code fixed, we see that Deflate is indeed slightly faster than gzip.</p>
<p>All of which leads to repeated quotations from Jeff about the community being smarter than him, and some drastic toning down of language in post-publication edits to the article. I read a cached version of the RSS feed, which is markedly different to the article currently live on codinghorror.com &#8211; &#8220;on my box, GZip is twice as fast as Deflate&#8221; becomes &#8220;on my box, GZip is just as fast as Deflate&#8221;, &#8220;Deflate is way slower. It&#8217;s not even close&#8221; becomes &#8220;Deflate is nowhere near 40% faster&#8221;, etc.</p>
<p><img src="/images/codinghorror01.png" align="middle" /></p>
<p>Anyone who&#8217;s tackled a major performance problem will likely agree that profiling is a tremendously valuable technique that should always be applied before attempting to optimise (i.e. look before you leap). I think this little episode has highlighted a couple of important things to bear in mind, however:</p>
<ol>
<li>Profiling isn&#8217;t a magic wand &#8211; if you use buggy profiling code, you are leading yourself up the garden path.</li>
<li>Profiling is less useful when you can reason (in the mathematical sense) about the code. That involves <em>understanding the algorithms you are dealing with</em>. Gzip is Deflate plus a bit more processing &#8211; so unless that extra processing has a negative duration gzip must necessarily take longer. You don&#8217;t need a profiler to work that out. Look <em>before </em>you look before you leap.</li>
</ol>
<p>Anyway, enough hatcheting from me, normal service will be resumed shortly.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/10/24/look-before-you-look-before-you-leap/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamic Async Batching with PFX</title>
		<link>http://basildoncoder.com/blog/2008/08/08/dynamic-async-batching-with-pfx/</link>
		<comments>http://basildoncoder.com/blog/2008/08/08/dynamic-async-batching-with-pfx/#comments</comments>
		<pubDate>Fri, 08 Aug 2008 16:41:04 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/08/08/dynamic-async-batching-with-pfx/</guid>
		<description><![CDATA[The PFX Team blog has been posting some excellent articles recently on the subject of task batching using the June 2008 CTP release of the Task Parallel Library. It&#8217;s really cool to see some of these techniques abstracted properly in .Net, and I hope it eventually becomes part of the core libraries. I&#8217;ve been playing [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://blogs.msdn.com/pfxteam/default.aspx">PFX Team blog</a> has been posting some excellent articles recently on the subject of <a href="http://blogs.msdn.com/pfxteam/archive/2008/08/05/8835612.aspx">task batching</a> using the June 2008 CTP release of the Task Parallel Library. It&#8217;s really cool to see some of these techniques abstracted properly in .Net, and I hope it eventually becomes part of the core libraries.</p>
<p>I&#8217;ve been playing around a bit recently with the June CTP in the context of batching up web service calls, as that&#8217;s something I do quite a lot. One particular problem that comes up occasionally is a two-stage series of requests to download a complete set of paged data. I might do this if I wanted to download an entire discussion thread, for instance, or a large account statement from my online bank.</p>
<p>Typically in this situation the web service will limit the number of records I can retrieve in one request, and allow me to specify start and count parameters to the request. The response will also include a total record count, so I know how much data there is.</p>
<p>The normal use case for this is to request the first page of data, and use the total record count to display a list of page links that my user can click on to navigate the data or jump to any page. In my case, however, I want ALL the data as quickly as possible.</p>
<p>So, imagine a situation where I am using a service that lets me download a maximum of 200 records per request. My first step is to request the maximum 200 records starting from index 0, i.e. the first page of data. In the response will be a total record count &#8211; if that number is equal to the number of records I got back (i.e. &lt;= 200) I&#8217;ve got everything in one hit and can stop. But what if the total record count is, say, 1000? I need to make four more requests (since I&#8217;ve already got records 1-200, I have 800 more to get in batches of 200 each).</p>
<p>Naturally I want to do this asynchronously, using as few resources as I can. This means all webservice calls should be using the APM pattern (thus using IO completion ports, and not consuming worker threads from the thread pool or creating my own threads) and, preferably, not blocking anywhere except when I actually need some data before continuing.</p>
<p>The two-stage process can be successfully captured asynchronously by combining a future and a continuation. I encapsulate the initial request in a Future object (which is a subclass of Task), and handle the check-record-count-and-get-more-records-if-required logic in the continuation. The code for this basically looks as follows:</p>
<pre>
<span class="Type">public</span> Future&lt;List&lt;Item&gt;&gt; GetAllItemsAsync()
{
    <span class="Type">var</span> f = Create&lt;GetItemsResponse&gt;(
            ac =&gt; Service.BeginGetItems(<span class="Constant">0</span>, ac, <span class="Constant">null</span>),
            Service.EndGetItems);

    <span class="Type">var</span> start = <span class="Constant">200</span>;

    <span class="Type">var</span> resultFuture = f.ContinueWith(
        r =&gt;
            {
                <span class="Comment">// Batch retrieval here...</span>
            });

    <span class="Statement">return</span> resultFuture;
}</pre>
<p>In order to support the APM pattern neatly, I&#8217;m using the following method <a href="http://blogs.msdn.com/pfxteam/archive/2008/03/16/8272833.aspx">from the PFX blog</a>:</p>
<pre>
<span class="Type">private</span> <span class="Type">static</span> Future&lt;T&gt; Create&lt;T&gt;(
        Action&lt;AsyncCallback&gt; beginFunc,
        Func&lt;IAsyncResult, T&gt; endFunc)
{
    <span class="Type">var</span> f = Future&lt;T&gt;.Create();
    beginFunc(iar =&gt;
        {
            <span class="Statement">try</span>
            {
                f.Value = endFunc(iar);
            }
            <span class="Statement">catch</span> (Exception e)
            {
                f.Exception = e;
            }
        });
    <span class="Statement">return</span> f;
}</pre>
<p>This could be coded as an extension method, though I haven&#8217;t bothered yet as I&#8217;m hopeful this immensely useful snippet will be integrated into the library itself.</p>
<p>Now I need to make a number of calls to get the rest of the data, so I loop until I&#8217;ve made the required number of async service calls:</p>
<pre>
<span class="Type">var</span> resultFuture = f.ContinueWith(r =&gt;
    {
        <span class="Type">var</span> items = <span class="Statement">new</span> ConcurrentQueue&lt;Item&gt;();
        <span class="Type">var</span> handles = <span class="Statement">new</span> List&lt;WaitHandle&gt;();

        <span class="Statement">while</span> (start &lt; r.Value.TotalRecordCount)
        {
            <span class="Type">var</span> asyncResult = Service.BeginGetItems(<span class="Constant">200</span>,
                ar =&gt; Service.EndGetItems(ar).Items
                    .ForEach(items.Enqueue), <span class="Constant">null</span>);

            handles.Add(asyncResult.AsyncWaitHandle);
            start += <span class="Constant">200</span>;
        }

        handles.ForEach(h =&gt; h.WaitOne());
        <span class="Statement">return</span> items.ToList();
    });</pre>
<p>I&#8217;m about 85% happy with this as an approach. I&#8217;m not completely happy, however, because of the WaitOne calls, which mean that I&#8217;m blocking on a threadpool thread until all the calls complete. Given that this is all wrapped up in a future, I may not actually need to access the data until well after the calls have completed, in which case I am wastefully consuming a threadpool thread for some period of time. So the $64,000 question is, how do I get rid of it? I&#8217;m sure there&#8217;s a way to do it, but my brain has gone on a protest march about all the time I&#8217;m forcing it to spend thinking about this stuff.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/08/08/dynamic-async-batching-with-pfx/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Lexical Closures in C# 3.0</title>
		<link>http://basildoncoder.com/blog/2008/07/01/lexical-closures-in-c-30/</link>
		<comments>http://basildoncoder.com/blog/2008/07/01/lexical-closures-in-c-30/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 16:49:36 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/07/01/lexical-closures-in-c-30/</guid>
		<description><![CDATA[What does this code print...How does this change if you tweak it like this...And most importantly, why?]]></description>
			<content:encoded><![CDATA[<p>There&#8217;s a <a href="http://dobbscodetalk.com/index.php?show=The-next-big-programming-language-feature-after-closures.html">slightly weird article</a> up on <a href="http://dobbscodetalk.com/">Dobbs Code Talk</a> this week, speculating that aggregate functions are &#8220;the next big programming language feature&#8221; after closures. The slight weirdness comes from the fact that both features have been around for decades, and not just in dusty academic languages either.</p>
<p>Still, there&#8217;s some interesting discussion in the comments about whether .Net&#8217;s closures are proper first-class lexically-scoped closures. The answer is yes &#8211; but with a fun twist.</p>
<p>The twist has been around for a long time &#8211; <a href="http://blogs.msdn.com/brada/default.aspx">Brad Abrams</a> blogged about it <a href="http://blogs.msdn.com/brada/archive/2004/08/03/207164.aspx">way back in 2004</a>, for instance &#8211; but it&#8217;s probably worth going over it again, since the recent arrival of LINQ and lambda syntax in C# 3.0 will presumably lead to more people being bitten by this as the use of closures becomes more mainstream.</p>
<p>A key thing to remember is that C# lambdas are just anonymous delegates in skimpy syntax. Behind the scenes the compiler turns them into classes &#8211; if you were looking at disassembled MSIL you wouldn&#8217;t be able to tell whether the code was written with lambda syntax or anonymous delegate syntax. Therefore, the issue discussed by Brad has not gone anywhere.</p>
<p>Lets revisit the problem, with a 2008 sheen applied (i.e. I&#8217;ll use lambda syntax rather than anonymous delegate syntax). What does the following code display?</p>
<pre>
Func&lt;<span class="Type">int</span>&gt;[] funcs = <span class="Statement">new</span> Func&lt;<span class="Type">int</span>&gt;[<span class="Constant">10</span>];
<span class="Statement">for</span> (<span class="Type">int</span> i = <span class="Constant">0</span>; i &lt; <span class="Constant">10</span>; ++i)
{
    funcs[i] = () =&gt; i * i;
}

funcs.ForEach(f =&gt; Console.WriteLine(f()));</pre>
<p>If you answered something along the lines of &#8220;prints the square of every number between 0 and 9&#8243; you&#8217;d be&#8230;wrong. Really, try it out. See?</p>
<p>Now, a lexical closure is supposed to capture its environment, meaning that the lambda stored on the first loop would capture i when i==0, the second loop would capture i when i==1, and so on. If this happened, then executing all the lambdas would indeed result in the squares of the numbers 0-9 being printed. So what gives?</p>
<p>The problem stems from the fact that the lambda is binding itself to a variable that is accessible outside the closure, which is being changed in every iteration of the loop. The closure doesn&#8217;t capture the value of i, it captures a reference to i itself, which is mutable.</p>
<p>You could actually make a case that this is bad code anyway, since it gives two responsibilities to the loop index &#8211; control the loop, and act as data in the closures. If we were being pedantic, we could split the responsibilities by creating a new variable, j, to be the closure data each iteration, and let i concentrate on being an index:</p>
<pre>
<span class="Statement">for</span> (<span class="Type">int</span> i = <span class="Constant">0</span>; i &lt; <span class="Constant">10</span>; ++i)
{
    <span class="Type">int</span> j = i;
    funcs[i] = () =&gt; j * j;
}</pre>
<p>Lo and behold, the code now works! Pedantry rules! Take a look with Reflector or ildasm to see what&#8217;s going on here. The executive summary is that the compiler captures the environment (i in the first example, j in the second) by creating a member variable within the class it generates for the closure. Previously, since the same instance of i lived for the entire duration of the loop, only one instance of the generated class was created and shared. Now, however, a new instance of the generated class is created in each iteration of the loop (since j is scoped within the loop body and thus we have a new j every time round). Thus, the data is not shared and we get the expected output.</p>
<p>There are two important points to consider here:</p>
<ol>
<li>The problem goes away if you write code more declaratively. Do away with the clunky for loop and everything works OK.
<pre>
Enumerable.Range(<span class="Constant">0</span>, <span class="Constant">10</span>).Select(x =&gt; x * x);</pre>
</li>
<li>It isn&#8217;t always bad that multiple closures can capture a reference &#8211; since one closure can &#8216;see&#8217; updates made to the shared data by another closure, you could use this as a coordination mechanism.</li>
</ol>
<p>This is not an issue that&#8217;s going to crop up every day &#8211; the example above is fairly contrived &#8211; but knowing about it will save some painful debugging sessions when inevitably you do run into it. The fix is always to take a local copy of the mutable data to coerce the compiler into generating code that creates multiple instances of the class generated to represent the closure.</p>
<p>Simple, yes? <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/07/01/lexical-closures-in-c-30/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C# 3.0, Parallel LINQ, And The Betfair API &#8211; An Introduction</title>
		<link>http://basildoncoder.com/blog/2008/02/23/c-30-parallel-linq-and-the-betfair-api-an-introduction/</link>
		<comments>http://basildoncoder.com/blog/2008/02/23/c-30-parallel-linq-and-the-betfair-api-an-introduction/#comments</comments>
		<pubDate>Sat, 23 Feb 2008 21:33:42 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Patterns]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/02/23/c-30-parallel-linq-and-the-betfair-api-an-introduction/</guid>
		<description><![CDATA[My pal Jan has a habit of waxing lyrical about the wonders of Parallel LINQ (PLINQ) as soon as you make the mistake of mentioning multithreading within earshot. I’ve been playing around with .Net 3.5 recently, and I write a lot of async code day-to-day when struggling to keep desktop webservice clients responsive when making [...]]]></description>
			<content:encoded><![CDATA[<p>My pal <a href="http://londoncoder.wordpress.com/">Jan</a> has a habit of waxing lyrical about the wonders of <a href="http://blogs.msdn.com/pfxteam/default.aspx">Parallel LINQ</a> (PLINQ) as soon as you make the mistake of mentioning multithreading within earshot. I’ve been playing around with <a href="http://www.microsoft.com/downloads/details.aspx?FamilyID=333325FD-AE52-4E35-B531-508D977D32A6&amp;displaylang=en">.Net 3.5</a> recently, and I write a lot of async code day-to-day when struggling to keep desktop webservice clients responsive when making lots of webservice calls, so I thought it high time I took a closer look.</p>
<p><em><strong>The Problem</strong></em></p>
<p>A key goal for the kind of async work I do is to batch multiple calls up, so that I get all the responses at once. This is important for keeping the rest of the code clean. To illustrate, imagine you are writing an application against the <a href="http://bdp.betfair.com/">Betfair API</a>, and you have a screen that displays a market, your current profit and loss on that market, and your unmatched bets on that market. To populate this screen will require four API calls &#8211; getMarket(), getMarketPrices(), getMarketProfitAndLoss(), and getCurrentBets().</p>
<p>Now, the worst (though easiest) thing to do is make the four calls sequentially on the UI thread. The problem with this is it’s slow, and the UI freezes during the process (since you’re blocking on the UI thread), which is a lousy user experience.</p>
<p>A slightly better approach is to spin off a thread, and make the four calls there, raising an event on completion. This gets all the work off the UI thread and therefore keeps the application responsive, but it’s still slow as the calls are still sequential.</p>
<p>To speed it up, you can create a thread per call (so four threads in this case). There’s a whole lot of complexity around working out the optimum number of threads to use (depending on how many processors you have, how many simultaneous connections you are allowed to open, etc) but that’s a bit beyond the scope of this post, so for now we’ll go with the one-thread-per-task approach and assume it’s optimal.</p>
<p>So, each thread makes one webservice call, and raises an event to signify that it’s finished. Simple, right? Unfortunately, this can lead to some real headaches in collating the data.</p>
<p>Imagine a user has hundreds of bets on the market, and therefore the getCurrentBets() call takes a bit longer to execute than the other three. The user clicks on a market, and the threads responsible for getting market data and P&amp;L raise their events quickly, so you display the screen with the data you have and plan to display the bets as and when they arrive.</p>
<p>Before the bets are received, however, the user clicks on another market. Again, the market data and P&amp;L come back quickly and you display them. Then, finally, the original getCurrentBets() call completes. But wait! You’ve moved onto another market now, so you don’t care about those bets any more! So you have to write some code to make sure that each piece of data received is still relevant. This can become very onerous very quickly, as you struggle to determine your UI state and work out what data you want and what should be discarded.</p>
<p>Now imagine that your application has timers firing all over the place to update prices and P&amp;L on the market every second or two, so you have events being raised all the time.</p>
<p>I’ve worked with code that ventured down this path, and believe me, you don’t want to go there.</p>
<p><em><strong>The Solution </strong></em></p>
<p>The best approach is to batch these calls up, so that each happens on a separate thread, but only one event is raised &#8211; when all of the data has been received. That way, you can be sure that when you handle the event, all the data is consistent.</p>
<p>Since this is one of the things that PLINQ does for you, it seems like a good candidate for kicking the tyres, so to speak. First, though, I’ll do a quick run through of how to do this without PLINQ, for comparison’s sake. The task will be to display a list of all the Premiership matches available on Betfair at the time the code runs.</p>
<p><em><strong>Take Out The Old </strong></em></p>
<p>Betfair list Premiership matches grouped by fixture date, under the Barclays Premiership node in the event tree. It looks something like this:</p>
<pre>Soccer
    English Soccer
        Barclays Premiership
            Fixtures 23 February
                Fulham v West Ham
                Liverpool v Middlesbrough
                ...
            Fixtures 24 February
                Blackburn v Bolton
                Reading v Aston Villa
            Fixtures 25 February
                Man City v Everton</pre>
<p>The Barclays Premiership event node has an ID that doesn&#8217;t change (2022802), so I can jump straight to that node and save myself the bother of having to navigate the Soccer and English Soccer parent nodes.</p>
<p>I&#8217;ll assume you already know how to create Service References for Betfair&#8217;s global WSDL, and skip straight on to creating some useful helper methods. I need to be able to call getEvents(), obviously:</p>
<pre><span class="Type">private</span> GetEventsResp GetEvents(<span class="Type">int</span> parentEventID)
{
    <span class="Statement">return</span> m_global.getEvents(
            MakeEventRequest(parentEventID)).Result;
}

<span class="Type">private</span> getEventsIn MakeEventRequest(<span class="Type">int</span> parentEventID)
{
    <span class="Statement">return</span> <span class="Statement">new</span> getEventsIn(<span class="Statement">new</span> GetEventsReq()
        {
            header = <span class="Statement">new</span> APIRequestHeader()
            {
                sessionToken = m_sessionToken
            },
            eventParentId = parentEventID
        });
}</pre>
<p>If you&#8217;re not used to C# 3.0, this is taking advantage of type initialisation to create nested objects without having to create a bunch of extra local variables. You can write the exact same method without type initialisation like this:</p>
<pre><span class="Type">private</span> getEventsIn MakeEventRequest(<span class="Type">int</span> parentEventID)
{
    APIRequestHeader header = <span class="Statement">new</span> APIRequestHeader();
    header.sessionToken = m_sessionToken;
    GetEventsReq req = <span class="Statement">new</span> GetEventsReq();
    req.header = header;
    req.eventParentId = parentEventID;
    <span class="Statement">return</span> <span class="Statement">new</span> getEventsIn(req);
}</pre>
<p>The first thing I need to do is get a list of fixture nodes. I can do this by asking for child events of the Premiership node, and filtering for the events that start with the word &#8216;Fixture&#8217;. This can be achieved with a simple regex and a bit of normal LINQ:</p>
<pre><span class="Type">private</span> List&lt;BFEvent&gt; GetPremiershipFixtureEvents()
{
    <span class="Statement">return</span> GetEvents(PREMIERSHIP).eventItems.Where(
        (ev, idx) =&gt; Regex.IsMatch(ev.eventName, <span class="Constant">"^Fixtures.*"</span>)
        ).ToList();
}</pre>
<p>Assume PREMIERSHIP is a const int with the value 2022802. The Where() method works as a filter &#8211; you pass it a delegate, and it executes that delegate against each member of the list and returns a new list containing only the elements for which the delegate returned true.</p>
<p>In this case, I&#8217;m creating the delegate with a lambda expression, which returns true for elements with an event name that is matched by the regex.</p>
<p>Now I&#8217;ve got the fixture events, I need to get the child events of each, which correspond to the actual matches. I want each call to be asynchronous so that they happen in parallel, rather than sequentially. I also want to wait for all calls to complete before continuing, so I use the WaitHandle.WaitAll() method:</p>
<pre><span class="Type">private</span> List&lt;BFEvent&gt; GetMatchEvents(
<span class="Type"></span>    List&lt;BFEvent&gt; fixtureDateEvents)
{
    List&lt;BFEvent&gt; matchEvents = <span class="Statement">new</span> List&lt;BFEvent&gt;();
    <span class="Type">var</span> callbacks = (
        <span class="Statement">from</span> ev <span class="Statement">in</span> fixtureDateEvents
        <span class="Statement">select</span> StartGetEvents(ev.eventId, matchEvents)
        ).ToList();
    WaitHandle.WaitAll(callbacks.ConvertAll(
                ar =&gt; ar.AsyncWaitHandle).ToArray());
    <span class="Statement">return</span> matchEvents;
}</pre>
<p>Here, the LINQ expression and the ConvertAll() method call are doing similar things &#8211; converting all elements of a list into another type. In the case of the LINQ expression, I am effectively obtaining a list of IAsyncResult objects by calling StartGetEvents() on each event in my list and storing the return value of each call. In the case of the ConvertAll() call, I am obtaining a list of WaitHandle objects by accessing the AsyncWaitHandle property of each IAsyncResult object in the list.</p>
<p>It is perfectly possible to replace the LINQ expression with a call to ConvertAll(), or the ConvertAll() call with another LINQ expression. Which one you use in cases like this is largely a matter of preference.</p>
<p>The StartGetEvents() method needs to make an asynchronous webservice call and append the results to the provided list. Since multiple threads are accessing the list, the write must be protected with a lock:</p>
<pre><span class="Type">private</span> IAsyncResult StartGetEvents(<span class="Type">int</span> parentEventID,
    List&lt;BFEvent&gt; matchEvents)
{
    <span class="Statement">return</span> m_global.BegingetEvents(MakeEventRequest(parentEventID),
        <span class="Type">delegate</span>(IAsyncResult ar)
        {
            <span class="Statement">lock</span> (matchEvents)
            {
                matchEvents.AddRange(
                    m_global.EndgetEvents(ar).Result.eventItems);
            }
        },
        m_global);
}</pre>
<p>I am using an anonymous delegate for the callback here. All it does is lock the list and add the events contained in the response. Note that in production code you might want to be a bit more diligent about locking strategies and so on &#8211; I&#8217;ve written the code like this for conciseness, not necessarily for production-grade correctness.</p>
<p>Now the whole shebang can be invoked very simply:</p>
<pre><span class="Type">var</span> fixtures = GetPremiershipFixtureEvents();
GetMatchEvents(fixtures).ForEach(
        e =&gt; Console.WriteLine(e.eventName));</pre>
<p>Note that the calling code is very clean and simple, and doesn&#8217;t care about threads or anything like that &#8211; all that async plumbing is nicely contained in the GetMatchEvents() and StartGetEvents() methods.</p>
<p><em><strong>Bring In The New</strong></em></p>
<p>So how can PLINQ help with this? Well, it lets me get rid of those GetMatchEvents() and StartGetEvents() methods, which contain all the fiddly async code and are easily the most complex methods in the code above.</p>
<p>First, I&#8217;ll create a simple task class which represents the task of getting events for a particular ID:</p>
<pre><span class="Type">public</span> <span class="Type">class</span> GetEventsTask
{
    <span class="Type">private</span> <span class="Type">int</span> m_parentEventID;
    <span class="Type">private</span> <span class="Type">string</span> m_sessionToken;

    <span class="Type">public</span> GetEventsTask(<span class="Type">string</span> sessionToken,
            <span class="Type">int</span> parentEventID)
    {
        m_sessionToken = sessionToken;
        m_parentEventID = parentEventID;
    }

    <span class="Type">public</span> List&lt;BFEvent&gt; GetEvents()
    {
        BFGlobalService svc = <span class="Statement">new</span> BFGlobalServiceClient();
        APIRequestHeader header = <span class="Statement">new</span> APIRequestHeader()
            { sessionToken = m_sessionToken };
        <span class="Statement">return</span> <span class="Statement">new</span> List&lt;BFEvent&gt;(svc.getEvents(
            <span class="Statement">new</span> getEventsIn(<span class="Statement">new</span> GetEventsReq()
            {
                eventParentId = m_parentEventID,
                header = header
            })).Result.eventItems);
    }
}</pre>
<p>Once I&#8217;ve instantiated an instance of this class, a call to GetEvents() will get me all the child events for the specified parent node.</p>
<p>To use PLINQ, all I have to do is create an array of these task objects &#8211; one per fixture date &#8211; and use the AsParallel() extension method to specify that I want the task processing done in parallel:</p>
<pre>
    GetEventsTask[] tasks = (
            <span class="Statement">from</span> ev <span class="Statement">in</span> fixtureDateEvents
            <span class="Statement">select</span> <span class="Statement">new</span> GetEventsTask(m_sessionToken, ev.eventId)
            ).ToArray();
    <span class="Type">var</span> taskResults = (
            <span class="Statement">from</span> t <span class="Statement">in</span> tasks.AsParallel()
            <span class="Statement">select</span> t.GetEvents()
            ).ToList();</pre>
<p>Neat, eh? Note that PLINQ will also take care of deciding the optimal number of threads, neatly sidestepping the work I alluded to earlier.</p>
<p>One wrinkle is that my PLINQ statement results in a list of lists, so I need to flatten it out before returning.</p>
<pre>List&lt;BFEvent&gt; matchEvents = <span class="Statement">new</span> List&lt;BFEvent&gt;();
taskResults.ForEach(results =&gt; matchEvents.AddRange(results));</pre>
<p>Obviously this is only scratching the surface, not only of PLINQ but of LINQ itself. Much more powerful expressions can be created with a little tweaking of the objects generated from the Betfair WSDL &#8211; but that&#8217;s a topic for another article.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/02/23/c-30-parallel-linq-and-the-betfair-api-an-introduction/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Reporting on NCover Exclusions</title>
		<link>http://basildoncoder.com/blog/2008/02/20/reporting-on-ncover-exclusions/</link>
		<comments>http://basildoncoder.com/blog/2008/02/20/reporting-on-ncover-exclusions/#comments</comments>
		<pubDate>Wed, 20 Feb 2008 19:54:05 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2008/02/20/reporting-on-ncover-exclusions/</guid>
		<description><![CDATA[On a recent project, my team was set the task of achieving 100% unit test pass-rate and code coverage. If you&#8217;ve ever been in this position, you&#8217;ll know it&#8217;s a double-edged sword &#8211; whilst it&#8217;s great when the Powers That Be embrace quality instead of fixating, limpet-like, on the next deadline, it can be a [...]]]></description>
			<content:encoded><![CDATA[<p>On a recent project, my team was set the task of achieving 100% unit test pass-rate and code coverage. If you&#8217;ve ever been in this position, you&#8217;ll know it&#8217;s a double-edged sword &#8211; whilst it&#8217;s great when the Powers That Be embrace quality instead of fixating, limpet-like, on the next deadline, it can be a nightmare when that percentage figure on the weekly summary becomes the new focus for managerial concentration, especially given <a href="http://www.ericsink.com/articles/Code_Coverage.html">how difficult it can be</a> to hit 100%.</p>
<p>The problem is that achieving the magical 100% is, in many cases, neither practical nor particularly useful. It can even be a problem, if the warm fuzzy feeling you get when you see &#8220;Coverage: 100%&#8221; leads to complacency. Even with 100% coverage and pass-rate, <a href="http://www-128.ibm.com/developerworks/java/library/j-cq01316/">you don&#8217;t necessarily have quality software</a>.</p>
<p>Our high-level project architecture involved a .Net client talking to a suite of web services written in Java. The .Net client, as an application with a GUI and a web service proxy, contained a great deal of generated code and was my main concern when the targets were set.</p>
<p>Now, it&#8217;s my belief that in most cases there&#8217;s no benefit to writing tests for generated code (unless you also wrote the generator). Unless you have a very, very good reason not to, you should trust that the tools are doing their job and generating sane code. That&#8217;s what they&#8217;re there for. If the tools are flaky, you probably shouldn&#8217;t use them at all &#8211; though I suppose that if you sometimes fell foul of a particular bug you could write a test to detect it<sup>[1]</sup>.</p>
<p>The cause of my concern was that the UI and web reference code accounted for about 30-35% of the <a href="http://en.wikipedia.org/wiki/Source_lines_of_code">SLOC</a> in the application, and so any coverage report that covered the whole app would be way short of the targets we were set. There are a number of ways to deal with this:</p>
<ol>
<li>Bite the bullet and write tests for <em>everything</em>. That includes InitializeComponent(), drag &#8216;n&#8217; drop handlers, and the sync and async versions of every web service stub. Best of luck, and see you in 2017<sup>[2]</sup>.</li>
<li>Explain patiently that some code does not need testing (or at least, is on the wrong side of the productivity bell curve and subject to massively diminishing returns in terms of effort/value). Of course, then you&#8217;ll be asked to prove that you&#8217;re not pulling a fast one and that the delta of your target and actual coverage percentage can be accounted for entirely by generated code. This will be tricky if you count SLOC for the generated code and use decision points for your test coverage, and maintaining this is another administrative task that you probably don&#8217;t want to do.</li>
<li>Separate your code such that some assemblies contain <em>only</em> generated code, and the rest contain <em>only</em> business logic. Then exclude the former from your test suite so they don&#8217;t show on the coverage report. This is probably achievable, though it can lead to some fairly hideous contortions to maintain the boundary, and can even result in sensible design decisions being discarded in favour of wacky ones that have no redeeming feature other than supporting your arbitrary separation rules.</li>
<li>Swear indiscriminately and refuse. Then clear your desk, probably.</li>
</ol>
<p>None of those appealed, so we set out to find another approach. What we wanted was a more flexible variant of option 3, where we could exclude methods or classes without having to exclude the whole assembly. If we could exclude code at a fairly granular level, then it became both more realistic and useful to aim for 100% coverage of our actual business code, using all the <a href="http://homepage.mac.com/hey.you/lessons.html">normal techniques</a>.</p>
<p>It turns out that code exclusion isn&#8217;t so tough &#8211; <a href="http://www.ncover.com/">NCover</a> will ignore methods and classes tagged with an <a href="http://www.ericsink.com/articles/Code_Coverage.html">attribute named CoverageExclude in the global namespace</a><sup>[3]</sup>.</p>
<p>This still requires a little discipline &#8211; for example making sure that if Joe marks a class as excluded, Jim doesn&#8217;t add some business logic to that class a week later without removing the attribute &#8211; but nothing that can&#8217;t easily be dealt with in regular code reviews.</p>
<p>The Powers That Be are wily, alas, and when we pitched the idea to them they approved in principle but were wary of allowing bits of code to be arbitrarily dropped off the coverage reports. If a class was excluded, who excluded it and why?</p>
<p>This seemed reasonable for accountability &#8211; the information would be available in the source check-in notes, but that&#8217;s a bit fiddly since you don&#8217;t know <em>when</em> the attribute was added; our source control system doesn&#8217;t have anything analogous to subversion&#8217;s &#8216;blame&#8217; so you have to go rummaging through a potentially very long version history. A better solution would be to find a way to add the information directly to the coverage report, so that it&#8217;s right there for all to see. So, how?</p>
<p>The first step was to get the appropriate metadata into the code. The <a href="http://weblogs.asp.net/nunitaddin/archive/2006/10/04/CoverageExclude.aspx">reference implementation</a> for the CoverageExclude attribute is as follows:</p>
<pre><span class="Type">public</span> <span class="Type">class</span> CoverageExcludeAttribute : Attribute { }</pre>
<p>We wanted to capture additional information when the attribute was used, however, so we added a couple of read-only properties and did away with the default constructor.</p>
<pre><span class="Type">public</span> <span class="Type">class</span> CoverageExcludeAttribute : Attribute
{
    <span class="Type">private</span> <span class="Type">string</span> m_author;
    <span class="Type">private</span> <span class="Type">string</span> m_reason;

    <span class="Type">public</span> CoverageExcludeAttribute(<span class="Type">string</span> reason,
        <span class="Type">string</span> author)
    {
        <span class="Statement">this</span>.m_reason = reason;
        <span class="Statement">this</span>.m_author = author;
    }

    <span class="Type">public</span> <span class="Type">string</span> Author
    {
        <span class="Statement">get</span> { <span class="Statement">return</span> <span class="Statement">this</span>.m_author; }
    }

    <span class="Type">public</span> <span class="Type">string</span> Reason
    {
        <span class="Statement">get</span> { <span class="Statement">return</span> <span class="Statement">this</span>.m_reason; }
    }
}</pre>
<p>Now, when anyone uses the attribute, the compiler forces them to add some additional data.</p>
<pre>[CoverageExclude(<span class="Constant">"John Q Dev"</span>, <span class="Constant">"No testable code here, buster"</span>)]
<span class="Type">public</span> <span class="Type">void</span> MethodToBeExcluded(<span class="Type">int</span> x, <span class="Type">int</span> y)
{
    <span class="Comment">// ...</span>
}</pre>
<p>NCover can be told to pay attention to this attribute with the excludeAttributes parameter, as explained <a href="http://www.kiwidude.com/blog/2006/07/nant-and-msbuild-tasks-for-ncover.html">here</a>.</p>
<p>With the easy bit out of the way, the next task was to report on these exclusions.  Our build system, after running the test suite, used NCoverExplorer to generate a summary report. You can tell NCoverExplorer to list exclusions in reports, so we figured that would be a good place to start. The appropriate NAnt incantation is:</p>
<pre><span class="Identifier">&lt;</span><span class="Identifier">ncoverexplorer</span><span class="Identifier"> </span><span class="Type">failonerror</span>=<span class="Constant">"false"</span><span class="Constant"></span>
<span class="Identifier">  </span><span class="Type">program</span>=<span class="Constant">"C:\NCoverExplorer\NCoverExplorer.Console.exe"</span>
<span class="Identifier">  </span><span class="Type">projectName</span>=<span class="Constant">"Atmosphere Processor::LV426"</span>
<span class="Identifier">  </span><span class="Type">reportType</span>=<span class="Constant">"4"</span>
<span class="Identifier">  </span><span class="Type">xmlReportName</span>=<span class="Constant">"Report.xml"</span>
<span class="Identifier">  </span><span class="Type">mergeFileName</span>=<span class="Constant">"CoverageMerge.xml"</span>
<span class="Identifier">  </span><span class="Type">showExcluded</span>=<span class="Constant">"True"</span>
<span class="Identifier">  </span><span class="Type">satisfactoryCoverage</span>=<span class="Constant">"80"</span><span class="Identifier"> &gt;</span>
<span class="Identifier"></span>
  <span class="Identifier">&lt;</span><span class="Identifier">fileset</span><span class="Identifier">&gt;</span>
  <span class="Identifier">  </span><span class="Identifier">&lt;</span><span class="Identifier">include</span><span class="Identifier"> </span><span class="Type">name</span>=<span class="Constant">"Coverage.xml"</span><span class="Identifier">/&gt;</span>
  <span class="Identifier">&lt;/fileset&gt;</span>
  <span class="Identifier">&lt;</span><span class="Identifier">exclusions</span><span class="Identifier">&gt;</span>
  <span class="Identifier">  </span><span class="Identifier">&lt;</span><span class="Identifier">exclusion</span><span class="Identifier"> </span><span class="Type">type</span>=<span class="Constant">"Assembly"</span><span class="Identifier"> </span><span class="Type">pattern</span>=<span class="Constant">"*Tests"</span><span class="Identifier"> /&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">exclusion</span><span class="Identifier"> </span><span class="Type">type</span>=<span class="Constant">"Assembly"</span><span class="Identifier"> </span><span class="Type">pattern</span>=<span class="Constant">"*Fixtures*"</span><span class="Identifier"> /&gt;</span>
  <span class="Identifier">&lt;/exclusions&gt;</span>
<span class="Identifier">&lt;/ncoverexplorer&gt;</span></pre>
<p>Note the reportType and showExcluded attributes, which specify the summary report we want, with details of excluded code appended to the report. Note also the exclusion nodes, which specify that we want our test assemblies excluded from coverage metrics. The report will include a table like this:</p>
<p style="text-align: center"><img src="/images/ncoverexplorer-report-unmodified.png" /></p>
<p>Our goal was to somehow get our custom properties (Author and Reason) into this report. To do so, firstly we needed to modify the above table with two extra columns to hold this custom data. NCoverExplorer ships with stylesheet called CoverageReport.xsl; the table modification was achieved by tweaking the &#8216;exclusions summary&#8217; section as follows:</p>
<pre><span class="Comment">&lt;!</span><span class="Comment">-- Exclusions Summary --</span><span class="Comment">&gt;</span>
<span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:<span class="Statement">template</span><span class="Identifier"> </span><span class="Type">name</span>=<span class="Constant">"exclusionsSummary"</span><span class="Identifier">&gt;</span>
  <span class="Identifier">&lt;</span><span class="Identifier">tr</span><span class="Identifier">&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">colspan</span>=<span class="Constant">"5"</span><span class="Identifier">&gt;</span><span class="Type">&amp;</span><span class="Statement">#160</span><span class="Type">;</span><span class="Identifier">&lt;/td&gt;</span>
  <span class="Identifier">&lt;/tr&gt;</span>
  <span class="Identifier">&lt;</span><span class="Identifier">tr</span><span class="Identifier">&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"exclusionTable mainTableHeaderLeft"</span>
      <span class="Identifier"></span><span class="Type">colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
      Excluded From Coverage Results<span class="Identifier">&lt;/td&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"exclusionTable mainTableGraphHeader"</span>
      <span class="Identifier"></span><span class="Type">colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
      All Code Within<span class="Identifier">&lt;/td&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"exclusionTable mainTableGraphHeader"</span>
      <span class="Identifier"></span><span class="Type">colspan</span>=<span class="Constant">"2"</span><span class="Identifier">&gt;</span>
      Reason For Exclusion<span class="Identifier">&lt;/td&gt;</span>
  <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"exclusionTable mainTableGraphHeader"</span>
    <span class="Identifier"></span><span class="Type">  colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
      Developer<span class="Identifier">&lt;/td&gt;</span>
  <span class="Identifier">&lt;/tr&gt;</span>
  <span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">for-each</span><span class="Identifier"> </span><span class="Type">select</span>=<span class="Constant">"./exclusions/exclusion"</span><span class="Identifier">&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">tr</span><span class="Identifier">&gt;</span>
      <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"mainTableCellBottom exclusionTableCellItem"</span>
         <span class="Identifier"> </span><span class="Type">colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">value-of</span><span class="Identifier"> </span><span class="Type">select</span>=<span class="Constant">"@name"</span><span class="Identifier"> /&gt;</span><span class="Identifier">&lt;/td&gt;</span>
      <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"mainTableCellBottom mainTableCellGraph"</span>
         <span class="Identifier"> </span><span class="Type">colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">value-of</span><span class="Identifier"> </span><span class="Type">select</span>=<span class="Constant">"@category"</span><span class="Identifier"> /&gt;</span><span class="Identifier">&lt;/td&gt;</span>
      <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"mainTableCellBottom mainTableCellGraph"</span>
          <span class="Type">colspan</span>=<span class="Constant">"2"</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">value-of</span><span class="Identifier"> </span><span class="Type">select</span>=<span class="Constant">"@reason"</span><span class="Identifier"> /&gt;</span><span class="Identifier">&lt;/td&gt;</span>
      <span class="Identifier">&lt;</span><span class="Identifier">td</span><span class="Identifier"> </span><span class="Type">class</span>=<span class="Constant">"mainTableCellBottom mainTableCellGraph"</span>
          <span class="Identifier"></span><span class="Type">colspan</span>=<span class="Constant">"1"</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">value-of</span><span class="Identifier"> </span><span class="Type">select</span>=<span class="Constant">"@author"</span><span class="Identifier"> /&gt;</span><span class="Identifier">&lt;/td&gt;</span>
    <span class="Identifier">&lt;/tr&gt;</span>
  <span class="Identifier">&lt;/</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">for-each</span><span class="Identifier">&gt;</span>
<span class="Identifier">&lt;/</span><span class="Special">xsl</span><span class="Comment">:</span><span class="Statement">template</span><span class="Identifier">&gt;</span></span></pre>
<p>The next step was to actually inject our custom data into the report. This was a two-stage process:</p>
<ol>
<li>Use reflection to iterate through the application assemblies, looking for anything tagged with our attribute</li>
<li>Open the report data file generated by NCoverExplorer and shoehorn our new data into it.</li>
</ol>
<p>We created a simple little post-processor application to perform this work. To complete stage 1, we needed to iterate through a directory of assemblies, loading each one in turn. In each assembly, we iterated through the types contained therein, and looked for our custom attribute on each one. Then, we iterated through the methods on each type, and looked for the custom attribute there too. This is actually very simple &#8211; the code skeleton looks like this:</p>
<pre><span class="Statement">foreach</span> (FileInfo assemblyFile <span class="Statement">in</span> assemblies)
{
    <span class="Statement">try</span>
    {
        <span class="Comment">// Attempt to load the file as an assembly, and grab  </span>
        <span class="Comment">// all the types defined therein</span>
        Assembly assembly = Assembly.LoadFrom(
            assemblyFile.FullName);
        Type[] types = assembly.GetTypes();

        <span class="Comment">// Spin through the types, looking for classes and </span>
        <span class="Comment">// methods tagged with CoverageExclude</span>
        <span class="Statement">foreach</span> (Type type <span class="Statement">in</span> types)
        {
            <span class="Type">object</span>[] classAttributes = type.GetCustomAttributes(
                <span class="Statement">typeof</span>(CoverageExcludeAttribute), <span class="Constant">false</span>);
            <span class="Statement">foreach</span> (CoverageExcludeAttribute classAttribute
                    <span class="Statement">in</span> classAttributes)
            {
                <span class="Comment">// ...</span>
            }

            MethodInfo[] methods = type.GetMethods(
                BindingFlags.Public |
                BindingFlags.NonPublic |
                BindingFlags.Instance |
                BindingFlags.Static);
            <span class="Statement">foreach</span> (MethodInfo method <span class="Statement">in</span> methods)
            {
                <span class="Type">object</span>[] methodAttributes =
                    method.GetCustomAttributes(
                    <span class="Statement">typeof</span>(CoverageExcludeAttribute), <span class="Constant">false</span>);
                <span class="Statement">foreach</span> (CoverageExcludeAttribute methodAttribute
                    <span class="Statement">in</span> methodAttributes)
                {
                    <span class="Comment">// ...</span>
                }
            }
        }
    }
    <span class="Statement">catch</span> (Exception ex)
    {
        <span class="Comment">// Probably not a .Net assembly, do some appropriate </span>
        <span class="Comment">// complaining to the user</span>
    }
}</pre>
<p>In the loops, we cached the fully-qualified names of the types and methods tagged with the attribute.</p>
<p>Stage 2 was implemented by tweaking the XML data file NCoverExplorer generates for the report. This is straightforward too &#8211; suck the report into an XmlDocument, grab the exclusion nodes, and add a couple of attributes to each one. All the types and methods were already listed since we&#8217;d set the excludeAttributes parameter in the NAnt configuration (see above).</p>
<p>Therefore, all we needed to do was match up the FQNs we cached in stage 1 with the nodes already in the report:</p>
<pre>XmlDocument doc = <span class="Statement">new</span> XmlDocument();
doc.Load(coverageFile.FullName);

<span class="Comment">// Go through all the exclusion nodes and try to match </span>
<span class="Comment">// them up against the exclusion data we have sucked </span>
<span class="Comment">// out of the assemblies</span>
<span class="Statement">foreach</span> (XmlNode node <span class="Statement">in</span> doc.SelectNodes(<span class="Constant">"//exclusion"</span>))
{
    <span class="Statement">switch</span> (node.Attributes[<span class="Constant">"category"</span>].Value)
    {
        <span class="Statement">case</span> <span class="Constant">"Class"</span>:
            <span class="Comment">// Find and remove the first exclusion reason for </span>
            // <span class="Comment">this class</span>
            FindExclusionAndModifyNode(exclusions.ClassExclusions,
                node);
            <span class="Statement">break</span>;
        <span class="Statement">case</span> <span class="Constant">"Method"</span>:
            <span class="Comment">// Find and remove the first exclusion reason for</span>
            //<span class="Comment"> this method</span>
            FindExclusionAndModifyNode(exclusions.MethodExclusions,
                node);
            <span class="Statement">break</span>;
        <span class="Statement">default</span>:
            <span class="Comment">// Exclusion at either assembly or namespace level</span>
            <span class="Statement">break</span>;
    }
}</pre>
<p>The implementation of FindExclusionAndModifyNode simply loops through the cached FQNs to see if we have data that corresponds to the current node, and if so it creates two new attributes &#8211; one containing the name of the developer that added the CoverageExcludeAttribute to the code, and another containing their justification for doing so. Then the modified XmlDocument is written out to disk, overriding the original.</p>
<p>The end result is a report that looks something like <a href="http://basildoncoder.com/TweakedCoverage.xml" target="_blank">this</a>, with all the excluded code neatly documented to keep suspicious managers happy.</p>
<p>Since the post-processor was written as a simple command-line application, we could create a custom NAnt task for it and integrate the whole process seamlessly with our continuous integration setup.</p>
<p>[1] I&#8217;ve seen it happen a few times before, for example an XML generator (which shall remain nameless) that occasionally &#8216;forgot&#8217; our custom namespace and used a default, which caused parsers of that XML to scream in agony. It&#8217;s rare though, unless you regularly dig up tools from CodeProject and use them in your production code, in which case you deserve everything you get <img src='http://basildoncoder.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>[2] Written in 2008. So if you&#8217;re reading this on December 31st 2016, adjust accordingly and don&#8217;t come crying to me.</p>
<p>[3] Yes, I know that NCover 2.x has built-in regex-based exclusions that do all this, but a) not everyone has an NCover 2.x pro licence, and b) we weren&#8217;t using NCover 2.x as it hadn&#8217;t been released at the time.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2008/02/20/reporting-on-ncover-exclusions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Turbocharging .Net Webservice Clients</title>
		<link>http://basildoncoder.com/blog/2007/12/15/turbocharging-net-webservice-clients/</link>
		<comments>http://basildoncoder.com/blog/2007/12/15/turbocharging-net-webservice-clients/#comments</comments>
		<pubDate>Sat, 15 Dec 2007 19:51:41 +0000</pubDate>
		<dc:creator>russ</dc:creator>
				<category><![CDATA[.Net]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Software Engineering]]></category>

		<guid isPermaLink="false">http://basildoncoder.com/blog/2007/12/15/turbocharging-net-webservice-clients/</guid>
		<description><![CDATA[Since the first version of .Net and its associated toolset, Microsoft have sought to make it easy to write SOAP services and SOAP clients. And, generally, they have succeeded quite well. Whilst the open-source world has tended to prefer the simpler REST approach, MS (and Sun, and Apache) have done an admirable job of taking [...]]]></description>
			<content:encoded><![CDATA[<p>Since the first version of .Net and its associated toolset, Microsoft have sought to make it easy to write <a href="http://en.wikipedia.org/wiki/SOAP">SOAP</a> services and SOAP clients. And, generally, they have succeeded quite well. Whilst the open-source world has tended to prefer the simpler <a href="http://en.wikipedia.org/wiki/Representational_State_Transfer">REST</a> approach, MS (and Sun, and Apache) have done an admirable job of taking the large, complex SOAP protocol and making it reasonably straightforward to work with most of the time.</p>
<p>One of the areas in which things get somewhat less straightforward is high performance. Granted, most web services don&#8217;t have particularly eye-popping requirements in terms of hits or transactions, but occasionally you find an exception. <a href="http://www.betfair.com">Betfair</a>, for instance, have an <a href="http://bdp.betfair.com">API</a> that has peak rates in excess of 13,000 requests <em>per second</em>, many of which hit the database, with individual users making tens or even hundreds of requests per second. Betfair&#8217;s data changes at a breathtaking rate and there is a perceived advantage to getting hold of up-to-the-millisecond information.</p>
<p>To put that in context, the <a href="http://en.wikipedia.org/wiki/Digg_effect">Digg Effect</a> is estimated to peak at around <a href="http://www.ndesign-studio.com/blog/updates/the-digg-effect/">6K-8K hits</a> <a href="http://linuxbraindump.org/2007/08/21/the-digg-effect-a-deconstruction/">per hour</a> as I write in December 2007, which translates to a piffling couple of hits per second (8000 / 60 / 60 = 2.2). Even a more extreme prediction of <a href="http://creativebits.org/webdev/surviving_the_digg_effect">50K hits per hour</a> is only around 14 hits per second, so we&#8217;re talking about handling three orders of magnitude more requests than Digg generates.</p>
<p>If you are writing a .Net client and want to get the best out of this sort of situation, you are hamstrung unless you learn a few tricks. Read on to learn five of the best. I&#8217;ll be using the Betfair API as an example throughout, but the techniques apply to any high-performance web service where the usage profile involves frequent small (&lt;1KB) SOAP requests.</p>
<p>For those who normally turn to the back of the book for answers and don&#8217;t really care about the whys and wherefores, here&#8217;s the executive summary:</p>
<ol>
<li><strong>Switch off Expect 100 Continue</strong>. This should be done in your App.config file (see below).</li>
<li><strong>Switch off Nagle&#8217;s Algorithm</strong>. This should be done in your App.config file (see below).</li>
<li><strong>Use multithreading</strong>. Unfortunately this is not a simple configuration file setting, it is a fundamental part of your application design.</li>
<li><strong>Remove the maximum connection bottleneck</strong>. This should be done in your App.config file (see below). This is <em>vital</em> if your application is multithreaded.</li>
<li><strong>Use gzip compression</strong>. .Net 2 has this built-in if you switch it on; .Net 1.1 needs a helping hand.</li>
</ol>
<p>The XML snippet that configures these settings is shown below, and should be added to your App.config file:</p>
<pre><span class="Identifier">&lt;</span><span class="Identifier">system</span><span class="Comment">.</span><span class="Identifier">net</span><span class="Identifier">&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">connectionManagement</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Identifier">add</span><span class="Identifier"> <!--         span--><span class="Type">address</span>=<span class="Constant">"*"</span><span class="Identifier"> <!--      span--><span class="Type">maxconnection</span>=<span class="Constant">"20"</span><span class="Identifier"> /&gt;</span>
    <span class="Identifier">&lt;/connectionManagement&gt;</span>
    <span class="Identifier">&lt;</span><span class="Identifier">settings</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Identifier">servicePointManager</span>
<span class="Identifier">            </span><span class="Type">expect100Continue</span>=<span class="Constant">"false"</span><span class="Identifier">/&gt;</span>
        <span class="Identifier">&lt;</span><span class="Identifier">servicePointManager</span>
<span class="Identifier">            </span><span class="Type">useNagleAlgorithm</span>=<span class="Constant">"false"</span><span class="Identifier">/&gt;</span>
    <span class="Identifier">&lt;/settings&gt;</span>
<span class="Identifier">&lt;/system</span><span class="Comment">.</span><span class="Identifier">net&gt;</span>
</span></span></pre>
<p>For those who share my distrust of <a href="http://basildoncoder.com/blog/2007/12/09/coding-by-convention/">unexplained sorcery</a>, here&#8217;s the gory details.</p>
<p><em><strong>Expect-100 the Unexpected</strong></em>  <a href="http://www.ietf.org/rfc/rfc2616.txt"></a></p>
<p><a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a> (the specification for HTTP 1.1) includes a request header and response code that together are known as &#8216;Expect 100 Continue&#8217;. When using the Expect header, the client will send the request headers and wait for the server to respond with a response code of 100 <em>before</em> sending the request body, i.e. splitting the request into two parts with a whole round-trip in-between.</p>
<p>Why would you ever want to do this? Well, imagine you are trying to upload a 101MB file to a web server with a 100MB file size limit. If you just submit the whole thing as one request, you&#8217;ll sit there for ages waiting for the upload to complete, only to have it fail right at the last minute when you hit the 100MB limit. Using Expect 100 Continue, you submit just the headers initially, and wait for the server&#8217;s permission to continue; in our example, this gives the server a chance to look at the Content-Length parameter and identify that your file is too big, and return an error code instead of permission to continue. This way you know the upload will fail, without needlessly transmitting a single byte of the large file.</p>
<p>By default, the .Net Framework uses the Expect 100 Continue approach. Most SOAP requests are not, however, anywhere near 101MB in size, and a server designed to deal with thousands of requests per second is not likely to be returning anything other than 100 Continue if the Expect header is sent. Depending on latency, the round-trip penalty may be unacceptable.</p>
<p>For example, Betfair&#8217;s Australian exchange (physically located in Australia) contains all their Australian markets, so if you&#8217;re in the UK and want to trade on the Australian Open tennis you&#8217;re subject to a round-trip time of about 350ms. If you allow your requests to be split into two, then you have two round-trips, so your request will take 700ms plus processing time.</p>
<p><img src="/images/expect100_on.png" align="middle" height="414" width="452" /></p>
<p>You don&#8217;t want that, so switch it off. Note that the request headers and body are still sent separately, but the latter is no longer dependent on the response to the former (so they are shown as sent together in this diagram).</p>
<p><img src="/images/expect100_off.png" align="middle" height="313" width="450" /></p>
<p>The setting corresponding to this in the XML snippet above is:</p>
<pre><span class="Identifier">&lt;</span><span class="Identifier">servicePointManager</span>
<span class="Identifier">            </span><span class="Type">expect100Continue</span>=<span class="Constant">"false"</span><span class="Identifier">/&gt;</span></pre>
<p><em><strong>Nagle&#8217;s Algorithm</strong></em></p>
<p><a href="http://en.wikipedia.org/wiki/Nagle%27s_algorithm">Nagle&#8217;s algorithm</a> is a low-level algorithm that tries to minimise the number of TCP packets on the network, by trying to fill a TCP packet before sending it. TCP packets have a 40-byte header, so if you try to send a single byte you incur a lot of overhead as you are sending 41 bytes to represent 1 byte of information. This 4000% overhead causes chaos on congested networks. A TCP packet size is configurable, but is often set to 1500 bytes, and so Nagle&#8217;s algorithm will buffer outgoing data in an attempt to send a small number of full packets rather than a huge amount of mostly empty packets.</p>
<p>Nagle&#8217;s algorithm can be paraphrased in English like this:</p>
<p><span style="font-style: italic">If I have less than a full packet&#8217;s worth of data, and I have not yet received an acknowledgement from the server for the last data I sent, I will buffer new outbound data. When I get a server acknowledgement or have enough data to fill a whole packet, I will send the data across the network.</span></p>
<p>Now, with our use case (frequent SOAP requests, each &lt;1KB) a request doesn&#8217;t fill a packet, so requests are subject to buffering. Furthermore, as explained above, even with Expect 100 Continue switched off the request headers are still sent separately from the request body, so the body of the first request is buffered until the request headers reach their destination and the server sends back a TCP ACK.</p>
<p>Let us again consider a UK client communicating with Betfair&#8217;s Australian API. Your application issues two requests one for current prices and one for your current market position (Req1 and Req2 in the diagram below). Together, both these requests are less than 1500 bytes. The headers for the first request are transmitted, but Nagle&#8217;s algorithm buffers the request body, plus the whole second request, since they don&#8217;t fill a TCP packet.</p>
<p>Due to the round trip latency, the acknowledgment of the first request&#8217;s headers is not received for 350ms, so that is how long the requests are buffered for. When the requests do get sent, they too are subject to 350ms latency around the world, so again you end up with around 700ms added to each of your Australian API calls.</p>
<p><img src="/images/nagle_on.png" align="middle" /></p>
<p>Without Nagle, we dispense with the buffering and send out our smaller packets immediately, saving a round-trip.</p>
<p><img src="/images/nagle_off.png" align="middle" />  One word of warning &#8211; disabling Nagle can cause problems if you are on a highly-congested network or have overworked routers and switches, since it increases the number of network packets flying around. If you don&#8217;t own your network, or are deploying your web service client to a large number of users, you might want to think about this carefully as you won&#8217;t be popular if your software saturates the network.  The setting corresponding to this in the XML snippet above is:</p>
<pre><span class="Identifier">servicePointManager</span>
<span class="Identifier">            </span><span class="Type">useNagleAlgorithm</span>=<span class="Constant">"false"</span><span class="Identifier">/&gt;</span></pre>
<p><em><strong>Happenin&#8217; Threads</strong></em></p>
<p>This one should be pretty obvious. A single-threaded application can only do one thing at a time. A multi-threaded application can do multiple things at a time. So if, for example, you want to display some information and need to collate the results from four web service requests to build your view, calling them one after another is going to be slower than calling them all simultaneously (not to mention freezing your UI if you&#8217;re writing a WinForms application and making calls on the UI thread).</p>
<p>Explaining how to design and write multi-threaded .Net applications is way out of scope for this blog post, but any time you spend reading and learning about it either online or in books is going to be time well-spent. Go on, do it now. <strong>Also read the next section</strong>, otherwise your application will not get as much benefit as you expect.</p>
<p><em><strong>Walk and Chew Gum</strong></em></p>
<p>.Net, by default, has a bottleneck on simultaneous web service calls against the same host. This one catches loads of people out &#8211; they write clever multi-threaded applications that issue many, many requests, and never realise that beneath the application layer a lot of their requests are called in sequence rather than in parallel.</p>
<p>Tsk, damn Microsoft for unnecessarily crippling their framework, right? Well no, actually, since this is an example of Microsoft following standards and recommendations and is to be applauded, lest they go back to their old ways of &#8220;embrace and extend&#8221;. The recommendation in question is from <a href="http://www.ietf.org/rfc/rfc2616.txt">RFC 2616</a> again, and states:</p>
<blockquote><p>A single-user client SHOULD NOT maintain more than 2 connections with any server or proxy.</p>
<p align="right">(<em>RFC 2616</em>, section 8.1.4)</p>
</blockquote>
<p>.Net uses HTTP 1.1 persistent connections by default (this is a good thing &#8211; you don&#8217;t want to incur the cost of establishing a TCP connection with every request,<em> especially</em> if you have long round-trip times as well since it involves multiple round-trips), so MS have done the right thing and restricted processes to two simultaneous connections by default.</p>
<p>What does this mean for your application? It means that if you want to make 20 requests, and do so on 20 threads as recommended above, under the hood .Net only makes two at a time and queues the rest up. Therefore your 20 threads are wasted, as your throughput is no better than if you&#8217;d only created two threads, and innocent web servers are protected from overzealous clients.</p>
<p>When we know that we&#8217;re talking to an insane city-flattening Godzilla-on-steroids of a server, however, the two-connection limit is unreasonable and we can ramp-up safely. The corresponding setting in the XML snippet above is:</p>
<pre><span class="Identifier">&lt;</span><span class="Identifier">connectionManagement</span><span class="Identifier">&gt;</span>
        <span class="Identifier">&lt;</span><span class="Identifier">add</span><span class="Identifier"> <!--         span--><span class="Type">address</span>=<span class="Constant">"*"</span><span class="Identifier"> <!--      span--><span class="Type">maxconnection</span>=<span class="Constant">"20"</span><span class="Identifier"> /&gt;</span>
</span></span></pre>
<p>Sorted. You can, of course, change the number 20 to whatever is appropriate for your application.</p>
<p><strong>Warning:</strong> Do not, under any circumstances, get into the habit of configuring this for all your web service applications. With all 20 threads issuing one request per second, you are exceeding the 14-request-per-second Digg Effect example I used above; with this technique and enough bandwidth your application will be quite capable of taking a lot of websites down, and those that manage to weather the storm will probably blacklist your IP address and/or close your account. <em>Only use this if you are absolutely sure the server is up to it and such aggressive behaviour is permitted by the owners</em>.</p>
<p><em><strong>Small is Beautiful</strong></em></p>
<p>The last step is to enable compression on your responses. Depending on the nature of the service you are using, the value of this tip may vary, but it&#8217;s likely to be of some benefit given the number of requests per second we are issuing. Of course, it is dependent on the web service actually supporting compression, but it&#8217;s been a standard feature of just about every web server for years, so this shouldn&#8217;t be a problem.</p>
<p>Lets look at a worked example. The getMarketPrices response from the Betfair API is about 30KB of XML. If, as above, we have 20 threads issuing one request per second, and each thread is interested in prices from a different market, that&#8217;s about 600KB of data per second, which will quite easily saturate a lot of home broadband connections.</p>
<p>With gzip compression, however, each response comes down to about 5KB (XML compresses very well, generally, since it is just text with a lot of repetition and whitespace), so the 20 threads now demand a more manageable 100KB per second.</p>
<p>Great. So how do we use it? In .Net 2.0 and above it&#8217;s very easy &#8211; just set the EnableDecompression property of your web service proxy object (ignore IntelliSense, which incorrectly claims the value is true by default; it&#8217;s actually false by default, <a href="http://msdn2.microsoft.com/en-us/library/system.web.services.protocols.httpwebclientprotocol.enabledecompression.aspx">as stated on MSDN</a>). For example, to get compressed responses from Betfair&#8217;s <a href="https://api.betfair.com/global/v3/BFGlobalService.wsdl">global server</a>:</p>
<pre>BFGlobalService service = <span class="Statement">new</span> BFGlobalService();
service.EnableDecompression = <span class="Constant">true</span>;</pre>
<p>If you&#8217;re still using .Net 1.1, you have a bit more work to do, since support for gzip was inexplicably left out of the framework. First, you need to subclass the generated BFGlobalService proxy class, and override some key methods so you can a) include the Accept-Encoding header to tell the server that you understand gzip, and b) decompress the gzipped response before the XML deserializer sees it, otherwise it&#8217;ll choke.</p>
<pre><span class="Type"></span><span class="Type">public</span> <span class="Type">class</span> BFGlobalWithGzip : BFGlobalService
{
    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Comment">///</span><span class="Comment"> Adds compression header to request header</span>
    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;/</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Type">protected</span> <span class="Type">override</span> System.Net.WebRequest<span class="Identifier"></span>
        <span class="Comment"></span> GetWebRequest(Uri uri)
    {
         HttpWebRequest request = <span class="Comment"></span>
             (HttpWebRequest)<span class="Statement">base</span>.GetWebRequest(uri);

         <span class="Comment">// Turn on compression</span>
         request.Headers.Add(<span class="Constant">"Accept-Encoding"</span>,<span class="Identifier"></span>
             <span class="Comment"></span><span class="Constant">"gzip, deflate"</span>);
         <span class="Statement">return</span> request;
    }

    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Comment">///</span><span class="Comment"> Decompress response before the Xml serializer gets</span><span class="Identifier"></span>
    <span class="Comment">/// </span><span class="Comment">its hands on it</span>
    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;/</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Type">protected</span> <span class="Type">override</span> WebResponse GetWebResponse(<span class="Identifier"></span>
        <span class="Comment"></span>WebRequest request)
    {
        <span class="Statement">return</span> <span class="Statement">new</span> HttpWebResponseDecompressed(request);
    }

    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Comment">///</span><span class="Comment"> Need to override this method if performing</span><span class="Identifier"></span>
    <span class="Comment">/// </span><span class="Comment">asynchronous calls, </span><span class="Comment">otherwise</span><span class="Comment"> </span><span class="Comment">de-compression</span><span class="Comment"></span><span class="Identifier"></span>
    /// <span class="Comment">will not be performed </span><span class="Comment">and will throw </span><span class="Comment">an error</span>
    <span class="Comment">///</span><span class="Comment"> </span><span class="Identifier">&lt;/</span><span class="Statement">summary</span><span class="Identifier">&gt;</span>
    <span class="Type">protected</span> <span class="Type">override</span> WebResponse GetWebResponse(<span class="Identifier"></span>
        WebRequest request, IAsyncResult result)
    {
        <span class="Statement">return</span> <span class="Statement">new</span> HttpWebResponseDecompressed(<span class="Identifier"></span>
            request, result);
    }
}</pre>
<p>Next, implement the HttpWebResponseDecompressed class. This subclasses .Net&#8217;s WebResponse class and knows how to decompress a response if it has ContentEncoding &#8216;gzip&#8217;:</p>
<pre><span class="Type">public</span> <span class="Type">class</span> HttpWebResponseDecompressed : WebResponse
{
    <span class="Type">private</span> HttpWebResponse m_response;
    <span class="Type">private</span> MemoryStream m_decompressedStream;

    <span class="Type">public</span> HttpWebResponseDecompressed(WebRequest request)
    {
        m_response = (HttpWebResponse)request.GetResponse();
    }

    <span class="Type">public</span> HttpWebResponseDecompressed(WebRequest request,
        <span class="Type"></span>IAsyncResult result)
    {
        m_response = (HttpWebResponse)
            <span class="Type"></span>request.EndGetResponse(result);
    }

    <span class="Type">public</span> <span class="Type">override</span> <span class="Type">long</span> ContentLength
    {
        <span class="Statement">get</span>
        {
            <span class="Statement">if</span> (m_decompressedStream == <span class="Constant">null</span>
                <span class="Type"></span>|| m_decompressedStream.Length == <span class="Constant">0</span>)
                    <span class="Statement">return</span> m_response.ContentLength;
            <span class="Statement">else </span><span class="Statement">return</span> m_decompressedStream.Length;
        }
        <span class="Statement">set </span>{ m_response.ContentLength = <span class="Statement">value</span>; }
    }

    <span class="Type">public</span> <span class="Type">override</span> <span class="Type">string</span> ContentType
    {
        <span class="Statement">get </span>{ <span class="Statement">return</span> m_response.ContentType; }
        <span class="Statement">set </span>{ m_response.ContentType = <span class="Statement">value</span>; }
    }

    <span class="Type">public</span> <span class="Type">override</span> Uri ResponseUri
    {
        <span class="Statement">get </span>{ <span class="Statement">return</span> m_response.ResponseUri; }
    }

    <span class="Type">public</span> <span class="Type">override</span> WebHeaderCollection Headers
    {
        <span class="Statement">get </span>{ <span class="Statement">return</span> m_response.Headers; }
    }

    <span class="Type">public</span> <span class="Type">override</span> System.IO.Stream GetResponseStream()
    {
        Stream compressedStream = <span class="Constant">null</span>;

        <span class="Statement">if</span> (m_response.ContentEncoding == <span class="Constant">"gzip"</span>)
            compressedStream = <span class="Statement">new</span> GZipInputStream(
                <span class="Type"></span>m_response.GetResponseStream());
        <span class="Statement">else</span> <span class="Statement">if</span> (m_response.ContentEncoding == <span class="Constant">"deflate"</span>)
            compressedStream = <span class="Statement">new</span> InflaterInputStream(
                <span class="Type"></span>m_response.GetResponseStream());

        <span class="Statement">if</span> (compressedStream != <span class="Constant">null</span>)
        {
            m_decompressedStream = <span class="Statement">new</span> MemoryStream();
            <span class="Type">int</span> size = <span class="Constant">4096</span>;
            <span class="Type">byte</span>[] buffer = <span class="Statement">new</span> <span class="Type">byte</span>[size];
            <span class="Statement">while</span> (<span class="Constant">true</span>)
            {
                size = compressedStream.Read(
                    <span class="Type"></span>buffer, <span class="Constant">0</span>, size);

                <span class="Statement">if</span> (size &gt; <span class="Constant">0</span>)
                    m_decompressedStream.Write(
                        <span class="Type"></span>buffer, <span class="Constant">0</span>, size);
                <span class="Statement">else</span>
                    <span class="Statement">break</span>;
            }

            m_decompressedStream.Seek(<span class="Constant">0</span>, SeekOrigin.Begin);
            compressedStream.Close();
            <span class="Statement">return</span> m_decompressedStream;
        }
        <span class="Statement">else </span><span class="Statement">return</span> m_response.GetResponseStream();
    }
}</pre>
<p>To decompress the data, we need a decompression library since .Net 1.1 doesn&#8217;t provide one. In most cases, <a href="http://www.icsharpcode.net/OpenSource/SharpZipLib/">SharpZipLib</a> will do the business:</p>
<pre><span class="Statement">using</span> ICSharpCode.SharpZipLib.GZip;
<span class="Statement">using</span> ICSharpCode.SharpZipLib.Zip.Compression.Streams;</pre>
<p>Now, when creating an instance of BFGlobalService you can use the gzip-supporting subclass and everything else happens automatically.</p>
<pre>BFGlobalService service = <span class="Statement">new</span> BFGlobalServiceWithGzip();</pre>
<p><em><strong>Fin</strong></em></p>
<p>Jebus, that went on for a bit. Now, go forth and write high-performance clients at will &#8211; but heed the warnings about only doing this when you know the server is on-the-ball, because these tips really can cause havoc if used irresponsibly.</p>
<p><a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save"><img src="http://basildoncoder.com/blog/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share/Bookmark"/></a> </p>]]></content:encoded>
			<wfw:commentRss>http://basildoncoder.com/blog/2007/12/15/turbocharging-net-webservice-clients/feed/</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 1.831 seconds -->
