<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:copyright="http://blogs.law.harvard.edu/tech/rss" xmlns:image="http://purl.org/rss/1.0/modules/image/">
    <channel>
        <title>C#</title>
        <link>http://blogs.imeta.co.uk/dgray/category/25.aspx</link>
        <description>C#</description>
        <language>en-GB</language>
        <copyright>Daniel Gray</copyright>
        <generator>Subtext Version 2.1.1.1</generator>
        <item>
            <title>Using two test frameworks - NUnit &amp;amp; MSTest</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/364.aspx</link>
            <description>&lt;p&gt;Following on from Neil's post about switching between unit test frameworks, I was hitting some problems with his described method. The answer (for me at least, your mileage may vary) was to change around the using block to :&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="preproc"&gt;#if&lt;/span&gt; NUNIT&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; NUnit.Framework;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; TestClass = NUnit.Framework.TestFixtureAttribute;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; TestMethod = NUnit.Framework.TestAttribute;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; TestInitialize = NUnit.Framework.SetUpAttribute;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; TestCleanup = NUnit.Framework.TearDownAttribute;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; ClassInitialize = NUnit.Framework.TestFixtureSetUpAttribute;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; ClassCleanup = NUnit.Framework.TestFixtureTearDownAttribute;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;&lt;span class="preproc"&gt;#else&lt;/span&gt;&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;&lt;span class="kwrd"&gt;using&lt;/span&gt; Microsoft.VisualStudio.TestTools.UnitTesting;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;&lt;span class="preproc"&gt;#endif&lt;/span&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;The NUNIT symbol is only defined on debug builds, so I'm using NUnit to run my tests when I'm coding, and then MSTest for builds conducted during CI / Release builds. This gives me the perfect duality of fast turn around of tests whilst coding (including automatic test runs when I build) and the integration of MSTest into TFS during "proper" builds. As I'm using MSTest for release builds I can also enable code coverage (occasionally running the tests through MSTest in development to get a view of how much I've covered).&lt;/p&gt;

&lt;p&gt;One thing to watch out for when using Rhino Mock with code coverage is a trust issue, neatly described in this &lt;a href="http://elegantcode.com/2007/11/29/rhino-mocks-with-code-coverage/"&gt;blog entry&lt;/a&gt;.&lt;/p&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/364.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/364.aspx</guid>
            <pubDate>Wed, 10 Sep 2008 09:44:17 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/364.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/364.aspx#feedback</comments>
            <slash:comments>2</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/364.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/364.aspx</trackback:ping>
        </item>
        <item>
            <title>Rhino Mock - Same interface, different types - Revisited</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/363.aspx</link>
            <description>&lt;p&gt;Following on from yesterdays post about creating two different types from a single interface - I have my answer. In fact I've got two... one's dirty, one's clean. Thanks to &lt;a href="http://blogs.imeta.co.uk/hhariri/"&gt;Hadi&lt;/a&gt; for the clean answer, which is what I've gone with.&lt;/p&gt;  &lt;h3&gt;Dirty Answer&lt;/h3&gt;  &lt;p&gt;Those of a sensitive disposition may want to look away now, or at least scroll to the next paragraph, because this isn't going to be pretty. I dug into the Rhino Mock assembly looking for the point where, maybe, I'd be able to switch the default behaviour to want I wanted. In my quest I found this method on MockRepository:&lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;&lt;span class="kwrd"&gt;protected&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; ProxyGenerator GetProxyGenerator(Type type)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;    &lt;span class="kwrd"&gt;if&lt;/span&gt; (!generatorMap.ContainsKey(type))&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;    {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;        generatorMap[type] = &lt;span class="kwrd"&gt;new&lt;/span&gt; ProxyGenerator();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;    }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;    &lt;span class="kwrd"&gt;return&lt;/span&gt; generatorMap[type];&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;"Oh yes!" thinks I. All I have to do is inherit MockRepository as MultiTypeMockRepository, override GetProxyGenerator and then always return a new ProxyGenerator. Alas, this was not to be, somehow the people behind Rhino have managed to expose an internal type (ProxyGenerator) as a return value on a protected method of a public class. For a brief moment I contemplated grabbing the Rhino source and rolling my own, but the overhead for a few simple tests wasn't worth it. Finding this method had exposed to me the cache of ProxyGenerators that Rhino are using. My next line of thought was to use reflection to tinker with the innards of MockRepository (told you this was the dirty answer):&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MyMockRepository&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;   {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;static&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; FlushProxyGenerators()&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;      {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;         Type mrType = &lt;span class="kwrd"&gt;typeof&lt;/span&gt;(MockRepository);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;         FieldInfo generatorMapField = mrType.GetField(&lt;span class="str"&gt;"generatorMap"&lt;/span&gt;, BindingFlags.NonPublic | BindingFlags.Static);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt;         &lt;span class="kwrd"&gt;object&lt;/span&gt; generatorMap = generatorMapField.GetValue(&lt;span class="kwrd"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;         &lt;span class="kwrd"&gt;lock&lt;/span&gt; (generatorMap)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;         {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;            Type generatorMapType = generatorMap.GetType();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;            PropertyInfo keys = generatorMapType.GetProperty(&lt;span class="str"&gt;"Keys"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;            MethodInfo remove = generatorMapType.GetMethod(&lt;span class="str"&gt;"Remove"&lt;/span&gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;            List&amp;lt;Type&amp;gt; types = &lt;span class="kwrd"&gt;new&lt;/span&gt; List&amp;lt;Type&amp;gt;(keys.GetValue(generatorMap, &lt;span class="kwrd"&gt;null&lt;/span&gt;) &lt;span class="kwrd"&gt;as&lt;/span&gt; IEnumerable&amp;lt;Type&amp;gt;);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt;            &lt;span class="kwrd"&gt;foreach&lt;/span&gt; (Type t &lt;span class="kwrd"&gt;in&lt;/span&gt; types) remove.Invoke(generatorMap, &lt;span class="kwrd"&gt;new&lt;/span&gt; &lt;span class="kwrd"&gt;object&lt;/span&gt;[] { t });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;         }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;      }&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;   }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[




.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;Guess what? It worked. Although my unit test code now had a rather odd call to FlushProxyGenerators in between the creation of the two outbound classes. That and I'd broken pretty much every rule in the book... dang.

&lt;h4&gt;&lt;/h4&gt;

&lt;h3&gt;Clean Answer&lt;/h3&gt;

&lt;p&gt;Hadi had suggested that I create a couple of stub concrete classes from my interface, set the SendMessage method as virtual and then mock from them. The resultant code looks thus:&lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MockEmailService : IOutboundService&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;   {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;      &lt;span class="preproc"&gt;#region&lt;/span&gt; IOutboundService Members&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendMessage(Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; data, IEnumerable&amp;lt;IServicePolicy&amp;gt; policies)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;      {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;         &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;      }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;      &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   }&lt;/pre&gt;
&lt;/div&gt;

&lt;div class="csharpcode"&gt;
  &lt;p&gt;
    &lt;/p&gt;&lt;div class="csharpcode"&gt; &lt;/div&gt;
  
&lt;/div&gt;

&lt;div class="csharpcode"&gt;&lt;/div&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;   &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;class&lt;/span&gt; MockFtpService : IOutboundService, IInboundService&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;   {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;      &lt;span class="preproc"&gt;#region&lt;/span&gt; IOutboundService Members&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;virtual&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendMessage(Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; data, IEnumerable&amp;lt;IServicePolicy&amp;gt; policies)&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;      {&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;         &lt;span class="kwrd"&gt;throw&lt;/span&gt; &lt;span class="kwrd"&gt;new&lt;/span&gt; NotImplementedException();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;      }&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;      &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt;      &lt;span class="preproc"&gt;#region&lt;/span&gt; IInboundService Members&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler&amp;lt;OrderReceivedEventArgs&amp;gt; OrderReceived;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler&amp;lt;OrderNotificationReceivedEventArgs&amp;gt; OrderNotificationReceived;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler&amp;lt;OrderAssociationReceivedEventArgs&amp;gt; OrderAssociationReceived;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;event&lt;/span&gt; EventHandler&amp;lt;OrderActivationReceivedEventArgs&amp;gt; OrderActivationReceived;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;      &lt;span class="preproc"&gt;#endregion&lt;/span&gt;&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;   }&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;      [TestMethod]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;      &lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendMessage_TwoOutbound_RoutedToTwoCorrectly()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;      {&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;         IOutboundService outbound1 = _mockRepository.StrictMock&amp;lt;MockEmailService&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;         IOutboundService outbound2 = _mockRepository.StrictMock&amp;lt;MockFtpService&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;         ProcessOutboundRouter router = &lt;span class="kwrd"&gt;new&lt;/span&gt; ProcessOutboundRouter();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;         router.Add(outbound1);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;         router.Add(outbound2);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;         IServicePolicy policy1 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;         IServicePolicy policy2 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;         Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; data = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;         Expect.Call(policy1.ServiceType).Return(outbound1.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;         Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound1.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;         Expect.Call(policy2.ServiceType).Return(outbound2.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;         Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound2.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy2 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;         _mockRepository.ReplayAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;         router.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1, policy2 });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;         _mockRepository.VerifyAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;      }&lt;/pre&gt;
&lt;/div&gt;
&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Note : I've changed the name of some of the interfaces.&lt;/p&gt;

&lt;p&gt;Much neater in the test, and without the pangs of guilt I get when I do something &lt;em&gt;really&lt;/em&gt; nasty.&lt;/p&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/363.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/363.aspx</guid>
            <pubDate>Wed, 10 Sep 2008 08:46:07 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/363.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2008/09/10/363.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/363.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/363.aspx</trackback:ping>
        </item>
        <item>
            <title>Rhino Mock - Same interface, different types</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2008/09/09/362.aspx</link>
            <description>&lt;p&gt;I'm trying out Rhino mock on a new project at the moment, trying to get into the TDD groove (which, thanks to NUnit, I am). I've started my codebase with interfaces and a couple of implementations, writing my tests &lt;em&gt;before&lt;/em&gt; the classes like a good boy. All was going well, Rhino mock proving to be adaptable and reasonably concise in its operations when I hit upon a scenario that had me stumped.&lt;/p&gt;  &lt;p&gt;I have an ProcessOutboundRouter class that implements the interface IProcessOutbound, this router wraps up calls to a list of other IProcessOutbound instances, forwarding messages to the correct instance on the basis of policies (defined by the IServicePolicy interface). The router does this by matching the value of a ServiceType property on the policy to the type of the IProcessOutbound instance. When it came to testing I'd originally setup my test thus:&lt;/p&gt;  &lt;p&gt; &lt;/p&gt;  &lt;div class="csharpcode"&gt;   &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[Test]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendMessage_TwoOutbound_RoutedToTwoCorrectly()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;   IProcessOutbound outbound1 = _mockRepository.StrictMock&amp;lt;IProcessOutbound&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;   IProcessOutbound outbound2 = _mockRepository.StrictMock&amp;lt;IProcessOutbound&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;   ProcessOutboundRouter router = &lt;span class="kwrd"&gt;new&lt;/span&gt; ProcessOutboundRouter();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;   router.Add(outbound1);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;   router.Add(outbound2);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;   IServicePolicy policy1 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   IServicePolicy policy2 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;   Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; data = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;   Expect.Call(policy1.ServiceType).Return(outbound1.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;   Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound1.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;   Expect.Call(policy2.ServiceType).Return(outbound2.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;   Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound2.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy2 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;   _mockRepository.ReplayAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;   router.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1, policy2 });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;   _mockRepository.VerifyAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Alas, 'twas not to be... I'd hoped that the StrictMock&amp;lt;T&amp;gt; method would return different mocks with different types when creating multiple mocks of the same interface. Understandably it doesn't, both outbound1 and outbound2 are of the same proxy type generated by Rhino mock. Creating a new MockRepository instance doesn't help either. So I've, somewhat unfortunately, ended up with the following:&lt;/p&gt;

&lt;p&gt; &lt;/p&gt;

&lt;div class="csharpcode"&gt;
  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   1:  &lt;/span&gt;[Test]&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   2:  &lt;/span&gt;&lt;span class="kwrd"&gt;public&lt;/span&gt; &lt;span class="kwrd"&gt;void&lt;/span&gt; SendMessage_TwoOutbound_RoutedToTwoCorrectly()&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   3:  &lt;/span&gt;{&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   4:  &lt;/span&gt;   IProcessOutbound outbound1 = _mockRepository.StrictMock&amp;lt;IProcessOutbound&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   5:  &lt;/span&gt;   IProcessOutbound outbound2 = _mockRepository.StrictMock&amp;lt;IMockProcessOutbound&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   6:  &lt;/span&gt;   ProcessOutboundRouter router = &lt;span class="kwrd"&gt;new&lt;/span&gt; ProcessOutboundRouter();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   7:  &lt;/span&gt;   router.Add(outbound1);&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;   8:  &lt;/span&gt;   router.Add(outbound2);&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;   9:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  10:  &lt;/span&gt;   IServicePolicy policy1 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  11:  &lt;/span&gt;   IServicePolicy policy2 = _mockRepository.StrictMock&amp;lt;IServicePolicy&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  12:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  13:  &lt;/span&gt;   Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt; data = &lt;span class="kwrd"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span class="kwrd"&gt;string&lt;/span&gt;, &lt;span class="kwrd"&gt;string&lt;/span&gt;&amp;gt;();&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  14:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  15:  &lt;/span&gt;   Expect.Call(policy1.ServiceType).Return(outbound1.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  16:  &lt;/span&gt;   Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound1.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  17:  &lt;/span&gt;   Expect.Call(policy2.ServiceType).Return(outbound2.GetType());&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  18:  &lt;/span&gt;   Expect.Call(&lt;span class="kwrd"&gt;delegate&lt;/span&gt; { outbound2.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy2 }); });&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  19:  &lt;/span&gt; &lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  20:  &lt;/span&gt;   _mockRepository.ReplayAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  21:  &lt;/span&gt;   router.SendMessage(data, &lt;span class="kwrd"&gt;new&lt;/span&gt; IServicePolicy[] { policy1, policy2 });&lt;/pre&gt;

  &lt;pre&gt;&lt;span class="lnum"&gt;  22:  &lt;/span&gt;   _mockRepository.VerifyAll();&lt;/pre&gt;

  &lt;pre class="alt"&gt;&lt;span class="lnum"&gt;  23:  &lt;/span&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;/p&gt;&lt;style type="text/css"&gt;&lt;![CDATA[

.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }]]&gt;&lt;/style&gt;

&lt;p&gt;Notice the second IProcessOutbound instance is a mock for a &lt;em&gt;different &lt;/em&gt;interface (IMockProcessOutbound, which merely inherits IProcessOutbound without adding anything). Whilst this works, it's nowhere near a final solution, what happens if I need a third instance (again of a different type)? Another interface? Not a good pattern.&lt;/p&gt;

&lt;p&gt;Has anyone managed to achieve the desired effect without having to resort to the new interface trick?&lt;/p&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/362.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2008/09/09/362.aspx</guid>
            <pubDate>Tue, 09 Sep 2008 13:10:05 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/362.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2008/09/09/362.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/362.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/362.aspx</trackback:ping>
        </item>
        <item>
            <title>Extracting public certificates from code signed assemblies</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2007/01/16/236.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Handy method for retrieving the public portion of certificate used in the digital signing of assemblies:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#2b91af&gt;Assembly&lt;/FONT&gt; assm = &lt;FONT color=#2b91af&gt;Assembly&lt;/FONT&gt;.GetAssembly(&lt;FONT color=#0000ff&gt;typeof&lt;/FONT&gt;(&lt;FONT color=#2b91af&gt;CertificateAccess&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;));&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#0000ff&gt;string&lt;/FONT&gt;&lt;FONT face="Courier New"&gt; location = assm.Location;&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#2b91af&gt;X509Certificate&lt;/FONT&gt; cert = &lt;FONT color=#2b91af&gt;X509Certificate2&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;.CreateFromSignedFile(location);&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New" color=#0000ff&gt;byte&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;[] raw = cert.GetRawCertData();&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT size=2&gt;&lt;FONT face="Courier New"&gt;&lt;FONT color=#2b91af&gt;X509Certificate2&lt;/FONT&gt; cert2 = &lt;FONT color=#0000ff&gt;new&lt;/FONT&gt; &lt;FONT color=#2b91af&gt;X509Certificate2&lt;/FONT&gt;&lt;/FONT&gt;&lt;FONT face="Courier New"&gt;();&lt;/FONT&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;cert2.Import(raw);&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;sVerificationCertificate = cert2;&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/236.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2007/01/16/236.aspx</guid>
            <pubDate>Tue, 16 Jan 2007 09:37:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/236.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2007/01/16/236.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/236.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/236.aspx</trackback:ping>
        </item>
        <item>
            <title>Custom extraction rules for Web Tests</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2006/09/19/221.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;To help test MiVoice&amp;nbsp;I've put together a Web Test that votes continously for a random candidate. I tried using the Form Field Extraction rule to select a candidate from the options returned (as a radio button list), but it wasn't up to the job (didn't seem to know what a radio button list was and couldn't randomly select a value). After nosing around in the documentation you can convert a bog standard web test into a coded web test and then from there hook in custom extraction rules. Our custom rule looks like :&lt;/FONT&gt;&lt;FONT size=2&gt;&lt;/P&gt;&lt;/FONT&gt;&lt;PRE&gt;using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.VisualStudio.TestTools.WebTesting;
using System.Text.RegularExpressions;
namespace MiVoiceLoadTest
{
    public class GetCandidateOidExtractionRule : ExtractionRule
    {
        private string mFieldName = string.Empty;
        private static Random sRandom = new Random();
        public override string RuleName
        {
            get { return "GetCandidateOidExtractionRule"; }
        }
        public string FieldName
        {
            get { return mFieldName; }
            set { mFieldName = value; }
        }
        public override void Extract(object sender, ExtractionEventArgs e)
        {
            List&lt;STRING&gt; candidateOids = new List&lt;STRING&gt;();
            string regexString = "type=\"radio\" name=\"" + FieldName.Replace("$", "\\$") + "\" value=\"([a-z0-9\\-]{36})\"";
            Regex r = new Regex(regexString);
            MatchCollection mc = r.Matches(e.Response.BodyString);
            foreach (Match m in mc)
            {
                string v = m.Groups[1].Value;
                if (!string.IsNullOrEmpty(v))
                {
                    candidateOids.Add(v);
                }
            }
            int i = sRandom.Next(candidateOids.Count);
            string oid = candidateOids[i];
            &lt;FONT color=#008000&gt;&lt;FONT color=#0000ff&gt;e.WebTest.Context.Add("CandidateOid", oid);&lt;/FONT&gt;
&lt;/FONT&gt;            e.Success = true;
            e.Message = "Passed";
        }
    }
}
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Notice the e.WebTest.Context.Add("CandidateOid"), this puts the value we selected into the context for the next request to use. This rule is then hooked into the&amp;nbsp;request that's&amp;nbsp;going to return the radio button list&amp;nbsp;as&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;&lt;/PRE&gt;&lt;PRE&gt;GetCandidateOidExtractionRule candidates = new GetCandidateOidExtractionRule();
candidates.FieldName = "ctl00$pageContent$vpVote$wizVotingProcess$rblCandidates";
candidates.ContextParameterName = "1"; // ! THIS BIT IS VITAL, NULLRFERENCEEXCEPTION OTHERWISE
request2.ExtractValues += new EventHandler&lt;EXTRACTIONEVENTARGS&gt;(candidates.Extract);
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Then the value we've set into the context is consumed in a subsequent request thus&lt;/FONT&gt; &lt;/P&gt;&lt;PRE&gt;request3Body.FormPostParameters.Add("ctl00$pageContent$vpVote$wizVotingProcess$rblCandidates", this.Context["CandidateOid"].ToString());
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Worked out very nicely.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/221.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2006/09/19/221.aspx</guid>
            <pubDate>Tue, 19 Sep 2006 15:28:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/221.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2006/09/19/221.aspx#feedback</comments>
            <slash:comments>1</slash:comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/221.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/221.aspx</trackback:ping>
        </item>
        <item>
            <title>lock(typeof(MyLockedType)) is bad, mmmm'ok?</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2006/08/02/218.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Seems that MS have been promoting bad practice in some of their documentation...&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/askgui06032003.asp"&gt;&lt;FONT face=Tahoma size=2&gt;http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraskdr/html/askgui06032003.asp&lt;/FONT&gt;&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Long and the short is don't lock on a type, so:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;public class MyLockedType
{
  public static void DoWork()
  {
     lock(typeof(MyLockedType))
     {
       ...do something here
     }
  }
}
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;is bad and should be rewritten as:&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;public class MyLockedType
{
  private static object mLockObj = new object();

  public static void DoWork()
  {
     lock(mLockObj)
     {
       ...do something here
     }
  }
}
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;There's more details in the article, but the main problem boils down to not having control over the MyLockedType to the extent that code outside of the class can lock on the type. Not only does this break the encapsulation but could lead to some very fun debugging.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/218.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2006/08/02/218.aspx</guid>
            <pubDate>Wed, 02 Aug 2006 08:47:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/218.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2006/08/02/218.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/218.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/218.aspx</trackback:ping>
        </item>
        <item>
            <title>ZedGraph - open source charting component</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2006/07/24/216.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Just started using a rather nifty little charting component at home by the name of &lt;/FONT&gt;&lt;A href="http://www.zedgraph.org/"&gt;&lt;FONT face=Tahoma size=2&gt;ZedGraph&lt;/FONT&gt;&lt;/A&gt;&lt;FONT face=Tahoma size=2&gt;. It's released under the LGPL which means it can be linked to without "infecting" commerical apps with the license (one of the criticisms oft leveled at the GPL). There's a handy set of tutorials / examples available &lt;A href="http://zedgraph.org/wiki/index.php?title=Sample_Graphs"&gt;online&lt;/A&gt;&amp;nbsp;and it works better than a lot of commercial components I've tried (built in panning / zooming / save as image, etc).&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;It's .NET 2.0 only unfortunately but is capable of some fairly impressive visuals (for something that's free) and runs in both WinForms and ASP.NET.&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/216.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2006/07/24/216.aspx</guid>
            <pubDate>Mon, 24 Jul 2006 14:38:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/216.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2006/07/24/216.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/216.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/216.aspx</trackback:ping>
        </item>
        <item>
            <title>Tracing between AppDomains</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2006/07/20/215.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;STRONG&gt;Problem :&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;AppDomain 1 creates a new AppDomain (2) to run tasks within, tracing from AppDomain 2 needs to be conducted in AppDomain 1 to allow external app (NUnit) to access the Console where it's all being written.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;&lt;STRONG&gt;Solution :&lt;/STRONG&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;4 new classes :&lt;/FONT&gt;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;RemoteAppDomainEventHandlers - serializable class containing the event handling code to run in AppDomain 1&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;RemoteTraceEventArgs - serializable event args used to transport tracing info between AppDomains&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;RemoteTraceListener - sub class of TraceListener, wraps a RemoteTracing instance&lt;/FONT&gt; 
&lt;LI&gt;&lt;FONT face=Tahoma size=2&gt;RemoteTracing - MarshalByRefObject that receives trace messages from RemoteTraceListener and raises events back to AppDomain 1&lt;/FONT&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;When AppDomain 2 is created an instance of RemoteTracing is created and unwrapped into AppDomain 1, EstablishRemoteTracing is called which sets up a RemoteTraceListener in AppDomain 2, finally the trace event is wired up to the RemoteTrace handler on an instance of RemoteAppDomainEventHandlers.&lt;/FONT&gt;&lt;/P&gt;&lt;PRE&gt;// we need to hook in tracing from the app domain back to here
m_remoteTracing = (RemoteTracing)m_appDomain.CreateInstanceAndUnwrap("com.iMeta.ApplicationUpdate","com.iMeta.ApplicationUpdate.Remote.RemoteTracing");
m_remoteTracing.EstablishRemoteTracing();
m_remoteTracing.Trace+=new TraceEventHandler(m_eventHandlers.RemoteTrace);
&lt;/PRE&gt;&lt;PRE&gt;public void EstablishRemoteTracing()
{
   // clear any existing trace listeners
   System.Diagnostics.Trace.Listeners.Clear();
   // create one of our remote listeners that wraps this RemoteTracing object
   RemoteTraceListener rtl = new RemoteTraceListener(this);
   System.Diagnostics.Trace.Listeners.Add(rtl);
}
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Then when a call is made to Trace.Write in AppDomain 2 it hits the following code in the RemoteTraceListener&lt;/FONT&gt;&lt;FONT face="Courier New" size=2&gt;&lt;/FONT&gt;&lt;PRE&gt;public override void Write(string message, string category)
{
   m_remote.SendTraceToClient(message,category);
}
&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Which in turn calls the RemoteTracing instance it was created with:&lt;/FONT&gt; &lt;PRE&gt;public void SendTraceToClient(string message, string category)
{
   // create a RemoteTraceEventArgs object and raise event with it
   RemoteTraceEventArgs rtea = new RemoteTraceEventArgs(message,category);
   OnTrace(rtea);
}
&lt;/PRE&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;An event gets raised in AppDomain 2 and AppDomain 1 handles it by writing the trace in it's domain.&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;Originally the event handler for AppDomain 1 was on the class that loaded the AppDomain, this didn't seem to work too well as the event handlers were running in AppDomain 2 which caused some fun StackOverflowExceptions!&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/215.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2006/07/20/215.aspx</guid>
            <pubDate>Thu, 20 Jul 2006 13:22:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/215.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2006/07/20/215.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/215.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/215.aspx</trackback:ping>
        </item>
        <item>
            <title>Forcing metaSite references from a new metaCore model</title>
            <link>http://blogs.imeta.co.uk/dgray/archive/2006/05/31/172.aspx</link>
            <description>&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;If you're using the new metaCore/metaSite you'll need to reference the metaSite.Core assembly from your metaCore model. But the compiler will helpfully remove any references it considers redundant, so you'll need to add a piece of code to force the compiler to include a reference to metaSite. We had this early on in MiVoice - Vote, but have just had a resurfacing of the issue when doing a release build. Previously we had the following included in the model assembly:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using System;&lt;BR&gt;using System.Collections.Generic;&lt;BR&gt;using System.Text;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;namespace com.iMeta.MiVoice.Vote&lt;BR&gt;{&lt;BR&gt;   static class metaSiteReferenceForce&lt;BR&gt;   {&lt;BR&gt;      private static void Nothing()&lt;BR&gt;      {&lt;BR&gt;         com.iMeta.metaSite.schema.msURL m = null;&lt;BR&gt;      }&lt;BR&gt;   }&lt;BR&gt;}&lt;/FONT&gt; &lt;/P&gt;
&lt;P&gt;&lt;FONT face=Tahoma size=2&gt;which worked fine for debug builds. Having been fighting with release builds we hit an issue where the compiler does further optimisations and completely removed the null reference above (very cleverly). So we've ended up with:&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;using System;&lt;BR&gt;using System.Collections.Generic;&lt;BR&gt;using System.Text;&lt;/FONT&gt;&lt;/P&gt;
&lt;P&gt;&lt;FONT face="Courier New" size=2&gt;namespace com.iMeta.MiVoice.Vote&lt;BR&gt;{&lt;BR&gt;   static class metaSiteReferenceForce&lt;BR&gt;   {&lt;BR&gt;      private static void Nothing()&lt;BR&gt;      {&lt;BR&gt;         com.iMeta.metaSite.schema.msURL m = com.iMeta.metaSite.schema.msURL.Factory.Create();&lt;BR&gt;      }&lt;BR&gt;   }&lt;BR&gt;}&lt;/FONT&gt;&lt;/P&gt;&lt;img src="http://blogs.imeta.co.uk/dgray/aggbug/172.aspx" width="1" height="1" /&gt;</description>
            <dc:creator>Daniel Gray</dc:creator>
            <guid>http://blogs.imeta.co.uk/dgray/archive/2006/05/31/172.aspx</guid>
            <pubDate>Wed, 31 May 2006 10:30:00 GMT</pubDate>
            <wfw:comment>http://blogs.imeta.co.uk/dgray/comments/172.aspx</wfw:comment>
            <comments>http://blogs.imeta.co.uk/dgray/archive/2006/05/31/172.aspx#feedback</comments>
            <wfw:commentRss>http://blogs.imeta.co.uk/dgray/comments/commentRss/172.aspx</wfw:commentRss>
            <trackback:ping>http://blogs.imeta.co.uk/dgray/services/trackbacks/172.aspx</trackback:ping>
        </item>
    </channel>
</rss>