<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>ploeh blog : Services</title><link>http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx</link><description>Tags: Services</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Faking The Windows Azure Table Service</title><link>http://blogs.msdn.com/ploeh/archive/2009/01/24/faking-the-windows-azure-table-service.aspx</link><pubDate>Sat, 24 Jan 2009 13:21:36 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9373882</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9373882.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9373882</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9373882</wfw:comment><description>&lt;p&gt;When presented with a new technology, one of my primary evaluation criteria is its testability, so it should come as no surprise to the regular reader of this blog that this was also foremost in my mind when &lt;a href="http://www.microsoft.com/azure"&gt;Windows Azure&lt;/a&gt; was presented at PDC. Much of the Azure Services Platform is just .NET, so testability is really up to you, just like with all other .NET code.&lt;/p&gt; &lt;p&gt;However, when it comes to the Windows Azure storage, the developer story is different. The Windows Azure Table Service is essentially a REST-based service, and to use it, you'll need to install the Azure Visual Studio SDK.&lt;/p&gt; &lt;p&gt;That's not a problem in itself, but it seems to be implemented as a REST service on top of SQL Server Express. While that may be good enough for casual development, that's quite heavyweight for unit testing. Imagine having to somehow deal with populating and cleaning this database between each test case. That will likely involve a fair amount of complex and undocumented &lt;a href="http://xunitpatterns.com/Back%20Door%20Manipulation.html"&gt;Back Door Manipulation&lt;/a&gt;. As the service is out of process in relation to the test, this is brittle and slow, so a better approach is warranted.&lt;/p&gt; &lt;p&gt;Fortunately, I've already demonstrated &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;how to Fake a REST-based Data Service&lt;/a&gt;, so faking the Windows Azure Table Service turns out to be fairly easy, and generally follows the same steps as previously outlined.&lt;/p&gt; &lt;p&gt;In the following example, I'm working with a simple service that echoes messages back to the caller. The Delay method saves the input message in table storage and returns the &lt;em&gt;previous&lt;/em&gt; message from storage. This test verifies that the input message is being correctly saved:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DelayWillSaveMessage()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  expectedMessage = \cf6 "Anonymous text"\cf0 ;\par ??\par ??    \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/EchoService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 MessageContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 MessageContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 StorageAccountInfo\cf0  account = \par ??            \cf3 EchoFacadeTest\cf0 .GetAccount();\par ??        \cf3 EchoFacade\cf0  sut = \cf4 new\cf0  \cf3 EchoFacade\cf0 (() =&amp;gt; account);\par ??        \cf5 // Exercise system\par ??\cf0         sut.Delay(expectedMessage);\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedMessage,\par ??            service.Container.GetStore&amp;lt;\cf3 FakeMessage\cf0 &amp;gt;().\par ??            First().Message, \cf6 "Delay"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; DelayWillSaveMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;string&lt;/span&gt; expectedMessage = &lt;span style="color: #a31515"&gt;"Anonymous text"&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/EchoService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt; service = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeDataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;&amp;gt;(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt;());&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; account = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.GetAccount();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt; sut = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;EchoFacade&lt;/span&gt;(() =&amp;gt; account);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.Delay(expectedMessage);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.AreEqual&amp;lt;&lt;span style="color: blue"&gt;string&lt;/span&gt;&amp;gt;(expectedMessage,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; service.Container.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; First().Message, &lt;span style="color: #a31515"&gt;"Delay"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;In the EchoFacade service, I've used the SDK sample StorageClient library that extents the ADO.NET Data Services Client API to deal with the particulars of Windows Azure Table Storage. One difference from regular ADO.NET Data Services lies in how StorageClient deals with the base URIs of the REST interface. This is a challenge, because a URI like &lt;em&gt;http://localhost/EchoService&lt;/em&gt; is interpreted as a service at the address &lt;em&gt;localhost&lt;/em&gt; belonging to the account &lt;em&gt;EchoService&lt;/em&gt;.&lt;/p&gt;
&lt;p&gt;This has the implication that I can't just pass the complete address directly to the client API (i.e. EchoFacade), since it would need to decompose it before it can make proper use of it. Fortunately, such a decomposition already exists in the form of the StorageAccountInfo class, so I just need to pass a properly configured instance to my &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; to make it work.&lt;/p&gt;
&lt;p&gt;The test accomplishes this task like this:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 private\cf0  \cf1 static\cf0  \cf4 StorageAccountInfo\cf0  GetAccount()\par ??\{\par ??    \cf4 Uri\cf0  address = \cf1 new\cf0  \cf4 Uri\cf0 (\cf5 "http://localhost/"\cf0 );\par ??    \cf1 return\cf0  \cf1 new\cf0  \cf4 StorageAccountInfo\cf0 (address, \cf1 true\cf0 ,\par ??        \cf5 "EchoService"\cf0 , \cf4 EchoFacadeTest\cf0 .TableKey);\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt; GetAccount()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;StorageAccountInfo&lt;/span&gt;(address, &lt;span style="color: blue"&gt;true&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #a31515"&gt;"EchoService"&lt;/span&gt;, &lt;span style="color: #2b91af"&gt;EchoFacadeTest&lt;/span&gt;.TableKey);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;When the service is hosted in the full system (whether in the Development Fabric or on Azure proper), the StorageAccountInfo instance is instead initialized using the static StorageAccountInfo.GetDefaultTableStorageAccountFromConfiguration method.&lt;/p&gt;
&lt;p&gt;If you are using the StorageClient library for your data access code, you might as well also take advantage of it when defining the fake service. First of all, you can derive the entities exposed from the Fake service from TableStorageEntity, as this gives you the key properties for free.&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 FakeMessage\cf0  : \cf4 TableStorageEntity\par ??\cf0 \{\par ??    \cf1 public\cf0  FakeMessage()\par ??    \{\par ??        \cf1 this\cf0 .CreationTime = \cf4 DateTime\cf0 .Now;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 DateTime\cf0  CreationTime \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf1 string\cf0  Message \{ \cf1 get\cf0 ; \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;TableStorageEntity&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; FakeMessage()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.CreationTime = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt; CreationTime { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;string&lt;/span&gt; Message { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;The DataContainer then becomes equally simple:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 MessageContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 FakeMessage\cf0 &amp;gt; Messages\par ??    \{\par ??        \cf1 get\par ??\cf0         \{ \par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 FakeMessage\cf0 &amp;gt;().\par ??                AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MessageContainer&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataContainer&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;IQueryable&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt; Messages&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; { &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.GetStore&amp;lt;&lt;span style="color: #2b91af"&gt;FakeMessage&lt;/span&gt;&amp;gt;().&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; AsQueryable(); &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;Using a Fake ADO.NET Data Service enables you unit test clients of the Windows Azure Table Service in a light-weight manner.&lt;/p&gt;
&lt;p&gt;In this Fake, I have chosen not to deal with the Windows Azure Table Service's particular way of partitioning and sorting data according to PartitionKey and RowKey. Instead, I just rely on the Fixture of each test to insert the data entities in the expected order.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9373882" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>A General-Purpose Fake ADO.NET Data Service</title><link>http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx</link><pubDate>Tue, 20 Jan 2009 10:10:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9343868</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9343868.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9343868</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9343868</wfw:comment><description>&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx"&gt;previous post&lt;/A&gt;, I discussed how to implement a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service for use with unit testing, showing how you can develop a one-off service that addresses specific needs, such as querying parents and children in the example.&lt;/P&gt;
&lt;P&gt;As I hinted in that post, it's possible to create a reusable Fake ADO.NET Data Service that takes care of implementing &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt; based on in-memory data storage. To save you the trouble of doing that, I've created one that you can use, if you should so desire.&lt;/P&gt;
&lt;P&gt;Following the standard ADO.NET Data Service coding idiom, the Fake ADO.NET Data Service consists of two classes:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;FakeDataService&amp;lt;T&amp;gt;, which derives from DataService&amp;lt;T&amp;gt;. This is simply the counterpart of MyDataService from the previous example. You can use this class as is. 
&lt;LI&gt;DataContainer, which implements IUpdatable. Although I haven't made this class abstract, it only becomes useful when you derive from it and define your own queries.&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Let's say that I want to use these two classes to unit test the same service client as in the previous example. Instead of MyService (where I manually had to implement IUpdatable), I can now define my data container as simply as this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 ParentChildContainer\cf0  : \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \par ??        \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Child\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\par ??\cf0         \{\par ??            \cf1 return\cf0  \cf1 this\cf0 .GetStore&amp;lt;\cf4 Parent\cf0 &amp;gt;().AsQueryable(); \par ??        \}\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetStore&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;().AsQueryable(); &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The only thing you have to do is just derive from DataContainer and specify your queries.&lt;/P&gt;
&lt;P&gt;Although you don't have to use FakeDataService&amp;lt;T&amp;gt; to host your Fake DataContainer, it makes it easy to set up the Fake service in a unit test:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanSetupFakeService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt; service = \par ??        \cf4 new\cf0  \cf3 FakeDataService\cf0 &amp;lt;\cf3 ParentChildContainer\cf0 &amp;gt;(\par ??            \cf4 new\cf0  \cf3 ParentChildContainer\cf0 ());\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host =\par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanSetupFakeService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt; service = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;FakeDataService&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;&amp;gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ParentChildContainer&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt; host =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;WebServiceHost&lt;/SPAN&gt;(service, &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;How does this work? DataContainer stores each 'table' in a List&amp;lt;T&amp;gt;, and each list is again stored in a dictionary that maps the list to the type. Here's the part of DataContainer that doesn't contain the IUpdatable implementation:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 DataContainer\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt; stores;\par ??\par ??    \cf1 public\cf0  DataContainer()\par ??    \{\par ??        \cf1 this\cf0 .stores = \cf1 new\cf0  \cf4 Dictionary\cf0 &amp;lt;\cf4 Type\cf0 , \cf4 IList\cf0 &amp;gt;();\par ??    \}\par ??\par ??    [System.Diagnostics.CodeAnalysis.\cf4 SuppressMessage\cf0 (\cf5 "Microsoft.Design"\cf0 , \cf5 "CA1004:GenericMethodsShouldProvideTypeParameter"\cf0 , Justification = \cf5 "As has been discussed on the FxCop DL, this is fair use of generics."\cf0 )]\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 if\cf0  (!\cf1 this\cf0 .stores.ContainsKey(\cf1 typeof\cf0 (T)))\par ??        \{\par ??            \cf1 this\cf0 .AddStore&amp;lt;T&amp;gt;();\par ??        \}\par ??        \cf1 return\cf0  (\cf4 IList\cf0 &amp;lt;T&amp;gt;)\cf1 this\cf0 .stores[\cf1 typeof\cf0 (T)];\par ??    \}\par ??\par ??    \cf1 private\cf0  \cf1 void\cf0  AddStore&amp;lt;T&amp;gt;()\par ??    \{\par ??        \cf1 this\cf0 .stores.Add(\cf1 typeof\cf0 (T), \cf1 new\cf0  \cf4 List\cf0 &amp;lt;T&amp;gt;());\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataContainer&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt; stores;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; DataContainer()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Dictionary&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt; GetStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (!&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.ContainsKey(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.AddStore&amp;lt;T&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;T&amp;gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T)];&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddStore&amp;lt;T&amp;gt;()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Add(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(T), &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;T&amp;gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;When you call GetStore&amp;lt;T&amp;gt;, DataContainer implicitly creates the list for the give type if it doesn't already exist.&lt;/P&gt;
&lt;P&gt;The rest of DataContainer deals with implementing IUpdatable:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 #region\cf0  IUpdatable Members\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  AddReferenceToCollection(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  resourceToBeAdded)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Add(resourceToBeAdded);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  ClearChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf4 Type\cf0  t = \cf1 this\cf0 .ResolveType(fullTypeName);\par ??    \cf1 object\cf0  resource = \cf4 Activator\cf0 .CreateInstance(t);\par ??    \cf1 this\cf0 .stores[t].Add(resource);\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    \cf1 foreach\cf0  (\cf4 IList\cf0  list \cf1 in\cf0  \cf1 this\cf0 .stores.Values)\par ??    \{\par ??        list.Remove(targetResource);\par ??    \}\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  GetValue(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName)\par ??\{\par ??    \cf1 return\cf0  targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  RemoveReferenceFromCollection(\par ??    \cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName,\par ??    \cf1 object\cf0  resourceToBeRemoved)\par ??\{\par ??    \cf4 IList\cf0  list = (\cf4 IList\cf0 )targetResource.GetType().\par ??        GetProperty(propertyName).\par ??        GetValue(targetResource, \cf1 null\cf0 );\par ??    list.Remove(resourceToBeRemoved);\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResetResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SaveChanges()\par ??\{\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetReference(\cf1 object\cf0  targetResource,\par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    targetResource.GetType().GetProperty(propertyName).\par ??        SetValue(targetResource, propertyValue, \cf1 null\cf0 );\par ??\}\par ??\par ??\cf1 #endregion\par ??\par ??private\cf0  \cf4 Type\cf0  ResolveType(\cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  (\cf1 from\cf0  t \cf1 in\cf0  \cf1 this\cf0 .stores.Keys\par ??            \cf1 where\cf0  t.FullName == fullTypeName\par ??            \cf1 select\cf0  t).First();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#region&lt;/SPAN&gt; IUpdatable Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddReferenceToCollection(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeAdded)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Add(resourceToBeAdded);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ClearChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ResolveType(fullTypeName);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource = &lt;SPAN style="COLOR: #2b91af"&gt;Activator&lt;/SPAN&gt;.CreateInstance(t);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores[t].Add(resource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Values)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; RemoveReferenceFromCollection(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resourceToBeRemoved)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt; list = (&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)targetResource.GetType().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetValue(targetResource, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; list.Remove(resourceToBeRemoved);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResetResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SaveChanges()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetReference(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource.GetType().GetProperty(propertyName).&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; SetValue(targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; ResolveType(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; t &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stores.Keys&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; t.FullName == fullTypeName&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; t).First();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;If you find this useful, I've attached the code to this post for your downloading pleasure. As always, the standard disclaimers apply.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9343868" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/ploeh/attachment/9343868.ashx" length="81103" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Creating A Fake ADO.NET Data Service</title><link>http://blogs.msdn.com/ploeh/archive/2009/01/19/creating-a-fake-ado-net-data-service.aspx</link><pubDate>Mon, 19 Jan 2009 11:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9339649</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9339649.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9339649</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9339649</wfw:comment><description>&lt;P&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;Previously&lt;/A&gt;, I discussed unit testing ADO.NET Data Service clients using a &lt;A href="http://xunitpatterns.com/Fake%20Object.html" mce_href="http://xunitpatterns.com/Fake%20Object.html"&gt;Fake&lt;/A&gt; ADO.NET Data Service, and I promised to demonstrate how to create such a service. In this article I will continue the previous example and implement the Fake MyService class.&lt;/P&gt;
&lt;P&gt;The basics of MyService is pretty simple:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\par ??\cf0 \{\par ??    \cf1 public\cf0  MyService()\par ??    \{\par ??        \cf1 this\cf0 .ParentStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt;();\par ??        \cf1 this\cf0 .ChildStore = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; Children\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ChildStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IQueryable\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; Parents\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .ParentStore.AsQueryable(); \}\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Child\cf0 &amp;gt; ChildStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\par ??    \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf4 Parent\cf0 &amp;gt; ParentStore \{ \cf1 get\cf0 ; \cf1 private\cf0  \cf1 set\cf0 ; \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; MyService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;List&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; Children&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt; { &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.AsQueryable(); }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;&amp;gt; ChildStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;&amp;gt; ParentStore { &lt;SPAN style="COLOR: blue"&gt;get&lt;/SPAN&gt;; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;set&lt;/SPAN&gt;; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This service supports two 'tables': Parents and Children (Parents can have Children). Since this is a Fake, it only needs to store its data in memory, so I use two List&amp;lt;T&amp;gt; instances for that. To provide direct access to the service's data, MyService exposes both lists as public properties (you can make those properties internal if your Fake service is defined in the same assembly as your tests).&lt;/P&gt;
&lt;P&gt;This is all you need if you only need to support read-only scenarios. If the client you want to test only queries its service, you don't need to do anything else. On the other hand, if you also need to test Create, Update and Delete (CUD) operations, you will need to implement &lt;A href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.data.services.iupdatable.aspx"&gt;IUpdatable&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 partial\cf0  \cf1 class\cf0  \cf4 MyService\cf0  : \cf4 IUpdatable}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;partial&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IUpdatable&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Implementing this interface enables you to support CUD operations. Since it contains twelve members, I'm not going to walk you through each and every one of them, but rather present some highlights. If you are interested in more details on the methods of this interface, I'll recommend &lt;A href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx" mce_href="http://blogs.msdn.com/astoriateam/archive/2008/04/10/iupdatable-ado-net-data-services-framework.aspx"&gt;this post&lt;/A&gt; on the &lt;A href="http://blogs.msdn.com/astoriateam/default.aspx" mce_href="http://blogs.msdn.com/astoriateam/default.aspx"&gt;Project Astoria Team Blog&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;To support each of the simple CUD operations, multiple methods must be implemented, although there's an overlap.&lt;/P&gt;
&lt;P&gt;To create a new item, the first method to implement is CreateResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red163\green21\blue21;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  CreateResource(\cf1 string\cf0  containerName, \par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 switch\cf0  (containerName)\par ??    \{\par ??        \cf1 case\cf0  \cf4 "Parents"\cf0 :\par ??            \cf5 Parent\cf0  p = \cf1 new\cf0  \cf5 Parent\cf0 ();\par ??            \cf1 this\cf0 .ParentStore.Add(p);\par ??            \cf1 return\cf0  p;\par ??        \cf1 case\cf0  \cf4 "Children"\cf0 :\par ??            \cf5 Child\cf0  c = \cf1 new\cf0  \cf5 Child\cf0 ();\par ??            \cf1 this\cf0 .ChildStore.Add(c);\par ??            \cf1 return\cf0  c;\par ??        \cf1 default\cf0 :\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf5 ArgumentException\cf0 (\par ??                \cf4 "Unknown container name."\cf0 );\par ??    \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; CreateResource(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; containerName, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;switch&lt;/SPAN&gt; (containerName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Parents"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; p = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore.Add(p);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; p;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;case&lt;/SPAN&gt; &lt;SPAN style="COLOR: #a31515"&gt;"Children"&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt; c = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Child&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore.Add(c);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; c;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;default&lt;/SPAN&gt;:&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentException&lt;/SPAN&gt;(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Unknown container name."&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation simply creates a new instance of the requested item type and adds it to the in-memory list. Here, I was just being a bit lazy and simply switched on the containerName, but it's perfectly possible to create a more generic implementation using a bit of Reflection, since the fullTypeName parameter specifies which type of object to create.&lt;/P&gt;
&lt;P&gt;The CreateResource method's responsibility is to create a default instance of the requested type and add it to the underlying data store (in this case just an in-memory list). However, it doesn't assign values to the item's properties, since this is the duty of the SetValue method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  SetValue(\cf1 object\cf0  targetResource, \par ??    \cf1 string\cf0  propertyName, \cf1 object\cf0  propertyValue)\par ??\{\par ??    \cf4 Type\cf0  t = targetResource.GetType();\par ??    t.GetProperty(propertyName).SetValue(\par ??        targetResource, propertyValue, \cf1 null\cf0 );\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; SetValue(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; propertyName, &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; propertyValue)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Type&lt;/SPAN&gt; t = targetResource.GetType();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; t.GetProperty(propertyName).SetValue(&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; targetResource, propertyValue, &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;At this point, I found it easier to implement the method using Reflection than explicitly switching on both the targetResource and propertyName parameters.&lt;/P&gt;
&lt;P&gt;The last method that must be implemented to support the Create scenario is ResolveResource.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  ResolveResource(\cf1 object\cf0  resource)\par ??\{\par ??    \cf1 return\cf0  resource;\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; ResolveResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; resource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; resource;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the Fake service is already working with in-memory objects, it can just return the object itself.&lt;/P&gt;
&lt;P&gt;With these methods implemented, updating an item is not quite as involved, since it reuses the SetValue and ResolveResource methods, so I only need to implement the GetResource method.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 object\cf0  GetResource(\cf4 IQueryable\cf0  query,\par ??    \cf1 string\cf0  fullTypeName)\par ??\{\par ??    \cf1 return\cf0  query.Cast&amp;lt;\cf1 object\cf0 &amp;gt;().AsEnumerable().\par ??        FirstOrDefault();\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetResource(&lt;SPAN style="COLOR: #2b91af"&gt;IQueryable&lt;/SPAN&gt; query,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; fullTypeName)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; query.Cast&amp;lt;&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt;&amp;gt;().AsEnumerable().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; FirstOrDefault();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;This implementation requires a bit of explanation. The query argument that the method receives is actually an &lt;EM&gt;expression&lt;/EM&gt; over the service's exposed queries (i.e. Parents and Children). Since the service operates on in-memory objects, it doesn't need to transform the expression, but can simply evaluate it directly, which is what the AsEnumerable extension method does. However, &lt;A href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx"&gt;IQueryable&lt;/A&gt; doesn't have an AsEnumerable extension method, whereas &lt;A href="http://msdn.microsoft.com/en-us/library/bb351562.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb351562.aspx"&gt;IQueryable&amp;lt;T&amp;gt;&lt;/A&gt; does - hence the cast to object.&lt;/P&gt;
&lt;P&gt;Deleting an item reuses GetResource and ResolveResource, so to support this scenario, the DeleteResource method is the only extra method that must be implemented.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 void\cf0  DeleteResource(\cf1 object\cf0  targetResource)\par ??\{\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ParentStore).Remove(targetResource);\par ??    ((\cf4 IList\cf0 )\cf1 this\cf0 .ChildStore).Remove(targetResource);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DeleteResource(&lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; targetResource)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ParentStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; ((&lt;SPAN style="COLOR: #2b91af"&gt;IList&lt;/SPAN&gt;)&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ChildStore).Remove(targetResource);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Once again, I decided to keep things simple and simply hard-code knowledge of ParentStore and ChildStore into the method, but it would be easy to generalize this approach.&lt;/P&gt;
&lt;P&gt;The rest of the methods of IUpdatable address more advanced scenarios concerning references between items, and I'm not going to cover them in this post, since it's already becoming quite long. If you are interested, I've provided the entire sample code as an attachment to this post, so download and peruse it at your leisure (obviously, the usual disclaimers apply).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; I've now posted &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/20/a-general-purpose-fake-ado-net-data-service.aspx"&gt;a general-purpose Fake ADO.NET Data Service&lt;/A&gt; that should hopefully address most of your needs.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9339649" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/ploeh/attachment/9339649.ashx" length="47330" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Unit Testing ADO.NET Data Service Clients</title><link>http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx</link><pubDate>Wed, 14 Jan 2009 18:51:13 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318879</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9318879.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9318879</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9318879</wfw:comment><description>&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx"&gt;previous post&lt;/a&gt;, I discussed unit testing ADO.NET Data Services and how you can host and test the service itself. In this post, I'll take a look at the opposite scenario: You have a client of an ADO.NET Data Service, and you want to unit test the client without relying on the real, production service.&lt;/p&gt; &lt;p&gt;In other words, you want to test the real client against a &lt;a href="http://xunitpatterns.com/Test%20Double.html"&gt;Test Double&lt;/a&gt; service. Presently, I'll show you how to create and host a Fake ADO.NET Data Service that the client code can invoke. Obviously, the client in this example uses the ADO.NET Data Services Client API, so I'll demonstrate how you can use that to invoke the fake service from within the same test.&lt;/p&gt; &lt;p&gt;Before we dive into the specifics of the implementation, here's a simple test that verifies that the client can retrieve all Parent instances from the service:&lt;/p&gt; &lt;p&gt;&lt;img style="border-top-width: 0px; border-left-width: 0px; border-bottom-width: 0px; border-right-width: 0px" height="346" alt="image" src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/UnitTestingADO.NETDataServiceClients_A9EC/image_6.png" width="503" border="0"&gt; &lt;/p&gt; &lt;p&gt;As you can see from the figure, the test combines two logical tiers into one test: The service and the client.&lt;/p&gt; &lt;p&gt;The service tier is simply part of the test's &lt;a href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/a&gt;, and while I'm not concerned with testing it in this context, I still need to set it up. This part uses the ADO.NET Data Services API to define and host the service.&lt;/p&gt; &lt;p&gt;The client part uses the ADO.NET Data Services &lt;em&gt;Client&lt;/em&gt; API to query the service via its REST interface and verify the result.&lt;/p&gt; &lt;p&gt;Notice that I use an &lt;em&gt;instance&lt;/em&gt; of MyDataService to configure the host. While not strictly necessary in this test, when hosting a Fake service, it's desirable to have a reference to the fake instance itself, since that allows you to configure and query it through its &lt;a href="http://xunitpatterns.com/back%20door.html"&gt;Back Door&lt;/a&gt;. Here's a test that uses the MyDataService instance to verify that a Parent instance was deleted from the service:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ClientCanDeleteInstance()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf3 MyDataService\cf0  service = \cf4 new\cf0  \cf3 MyDataService\cf0 ();\par ??    \cf4 using\cf0  (\cf3 WebServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 WebServiceHost\cf0 (service, \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??\par ??        \cf3 MyDataServiceContext\cf0  ctx =\par ??            \cf4 new\cf0  \cf3 MyDataServiceContext\cf0 (address);\par ??        \cf4 var\cf0  victim = (\cf4 from\cf0  p \cf4 in\cf0  ctx.Parents\par ??                      \cf4 where\cf0  p.Id == 3\par ??                      \cf4 select\cf0  p).Single();\par ??        \cf5 // Exercise system\par ??\cf0         ctx.DeleteObject(victim);\par ??        ctx.SaveChanges();\par ??        \cf5 // Verify outcome\par ??\cf0         \cf3 Assert\cf0 .IsFalse(service.Data.Parents.Any(\par ??            p =&amp;gt; p.Id == victim.Id), \cf6 "Deleted"\cf0 );\par ??        \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt; &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;TestMethod&lt;/span&gt;]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; ClientCanDeleteInstance()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Fixture setup&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt; address = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Uri&lt;/span&gt;(&lt;span style="color: #a31515"&gt;"http://localhost/MyDataService"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; service = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;using&lt;/span&gt; (&lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt; host = &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;WebServiceHost&lt;/span&gt;(service, &lt;span style="color: blue"&gt;new&lt;/span&gt;[] { address }))&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt; ctx =&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataServiceContext&lt;/span&gt;(address);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;var&lt;/span&gt; victim = (&lt;span style="color: blue"&gt;from&lt;/span&gt; p &lt;span style="color: blue"&gt;in&lt;/span&gt; ctx.Parents&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;where&lt;/span&gt; p.Id == 3&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;select&lt;/span&gt; p).Single();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Exercise system&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.DeleteObject(victim);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ctx.SaveChanges();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Verify outcome&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;Assert&lt;/span&gt;.IsFalse(service.Data.Parents.Any(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; p =&amp;gt; p.Id == victim.Id), &lt;span style="color: #a31515"&gt;"Deleted"&lt;/span&gt;);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: green"&gt;// Teardown&lt;/span&gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As in the previous example, all &lt;a href="http://xunitpatterns.com/fixture%20setup.html"&gt;Fixture Setup&lt;/a&gt; before the declaration of the MyDataServiceContext instance is part of the logical service tier, while the remaining Fixture Setup code, as well as the execution of the &lt;a href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/a&gt; is part of the logical client tier. However, notice that the verification now takes place on the logical service tier, since I'm using the fake service instance as a Back Door, instead of relying on the REST interface to query the service.&lt;/p&gt;
&lt;p&gt;Since &lt;a href="http://msdn.microsoft.com/en-us/library/system.data.services.dataservicehost.aspx"&gt;DataServiceHost&lt;/a&gt; doesn't include a constructor that takes a single instance as a parameter, I use its base class &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webservicehost.aspx"&gt;WebServiceHost&lt;/a&gt; instead. &lt;a href="http://www.red-gate.com/products/reflector/"&gt;Reflector&lt;/a&gt; shows that DataServiceHost really adds no behavior to its base class, so using WebServiceHost as a host should be safe - for now (let's hope it stays that way, or even better, that it gets a constructor overload that accepts a service instance).&lt;/p&gt;
&lt;p&gt;In normal cases, I consider it best practice to explicitly load data into the fake service as part of the Fixture Setup phase, but to keep these examples simple, I decided to implicitly load some test data as part of creating the MyDataService instance. That is why, in the above example, I can successfully retrieve a Parent instance with an Id of 3. Please be aware that this is not a strategy I endorse - I only did it to keep the example a bit less complex.&lt;/p&gt;
&lt;p&gt;Here's the MyDataService class in its entirety:&lt;/p&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red163\green21\blue21;}??\fs20 [\cf3 ServiceBehavior\cf0 (IncludeExceptionDetailInFaults = \cf4 true\cf0 , \par ??    InstanceContextMode = \cf3 InstanceContextMode\cf0 .Single)]\par ??\cf4 public\cf0  \cf4 class\cf0  \cf3 MyDataService\cf0  : \cf3 DataService\cf0 &amp;lt;\cf3 MyService\cf0 &amp;gt;\par ??\{\par ??    \cf4 private\cf0  \cf4 readonly\cf0  \cf3 MyService\cf0  service_;\par ??\par ??    \cf4 public\cf0  MyDataService()\par ??    \{\par ??        \cf4 this\cf0 .service_ = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Parent\cf0 &amp;gt; ps = \cf4 this\cf0 .service_.ParentStore;\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 1, Text = \cf5 "Ploeh"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 2, Text = \cf5 "Fnaah"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 3, Text = \cf5 "Nd\u248 ?h"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 4, Text = \cf5 "Foo"\cf0  \});\par ??        ps.Add(\cf4 new\cf0  \cf3 Parent\cf0 () \{ Id = 5, Text = \cf5 "Bar"\cf0  \});\par ??\par ??        \cf3 IList\cf0 &amp;lt;\cf3 Child\cf0 &amp;gt; cs = \cf4 this\cf0 .service_.ChildStore;\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 1, Text = \cf5 "Child 1"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 2, Text = \cf5 "Child 2"\cf0  \});\par ??        cs.Add(\cf4 new\cf0  \cf3 Child\cf0 () \{ Id = 3, Text = \cf5 "Child 3"\cf0  \});\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf3 MyService\cf0  Data\par ??    \{\par ??        \cf4 get\cf0  \{ \cf4 return\cf0  \cf4 this\cf0 .service_; \}\par ??    \}\par ??\par ??    \cf4 public\cf0  \cf4 static\cf0  \cf4 void\cf0  InitializeService(\par ??        \cf3 IDataServiceConfiguration\cf0  config)\par ??    \{\par ??        config.SetEntitySetAccessRule(\cf5 "*"\cf0 ,\par ??            \cf3 EntitySetRights\cf0 .All);\par ??        config.UseVerboseErrors = \cf4 true\cf0 ;\par ??    \}\par ??\par ??    \cf4 protected\cf0  \cf4 override\cf0  \cf3 MyService\cf0  CreateDataSource()\par ??    \{\par ??        \cf4 return\cf0  \cf4 this\cf0 .service_;\par ??    \}\par ??\}}
--&gt;
&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;&lt;pre style="margin: 0px"&gt;[&lt;span style="color: #2b91af"&gt;ServiceBehavior&lt;/span&gt;(IncludeExceptionDetailInFaults = &lt;span style="color: blue"&gt;true&lt;/span&gt;, &lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; InstanceContextMode = &lt;span style="color: #2b91af"&gt;InstanceContextMode&lt;/span&gt;.Single)]&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;class&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyDataService&lt;/span&gt; : &lt;span style="color: #2b91af"&gt;DataService&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;&amp;gt;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;private&lt;/span&gt; &lt;span style="color: blue"&gt;readonly&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; MyDataService()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_ = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt;();&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;&amp;gt; ps = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ParentStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Ploeh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Fnaah"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Ndøh"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 4, Text = &lt;span style="color: #a31515"&gt;"Foo"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ps.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Parent&lt;/span&gt;() { Id = 5, Text = &lt;span style="color: #a31515"&gt;"Bar"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IList&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;&amp;gt; cs = &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_.ChildStore;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 1, Text = &lt;span style="color: #a31515"&gt;"Child 1"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 2, Text = &lt;span style="color: #a31515"&gt;"Child 2"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cs.Add(&lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;Child&lt;/span&gt;() { Id = 3, Text = &lt;span style="color: #a31515"&gt;"Child 3"&lt;/span&gt; });&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; Data&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;get&lt;/span&gt; { &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;public&lt;/span&gt; &lt;span style="color: blue"&gt;static&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; InitializeService(&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;IDataServiceConfiguration&lt;/span&gt; config)&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.SetEntitySetAccessRule(&lt;span style="color: #a31515"&gt;"*"&lt;/span&gt;,&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: #2b91af"&gt;EntitySetRights&lt;/span&gt;.All);&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; config.UseVerboseErrors = &lt;span style="color: blue"&gt;true&lt;/span&gt;;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: #2b91af"&gt;MyService&lt;/span&gt; CreateDataSource()&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;span style="color: blue"&gt;return&lt;/span&gt; &lt;span style="color: blue"&gt;this&lt;/span&gt;.service_;&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/pre&gt;&lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;&lt;/div&gt;
&lt;p&gt;As I described, I use the constructor to set up some implicit test data. While I want to reiterate that this is not something I would recommend, I chose to include this code to make it clear what I was talking about.&lt;/p&gt;
&lt;p&gt;The service is decorated with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx"&gt;ServiceBehavior&lt;/a&gt; attribute, which sets its &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.instancecontextmode.aspx"&gt;InstanceContextMode&lt;/a&gt; to &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.instancecontextmode.aspx"&gt;Single&lt;/a&gt; to allow it to be hosted as a Singleton service by WebServiceHost. This, again, is what allows me to query the fake service's data via its Back Door.&lt;/p&gt;
&lt;p&gt;Since MyService simply stores data in memory, MyDataService needs to keep the same instance around for each request, so it overrides CreateDataSource to ensure that the same data container instance is always being referenced for the lifetime of the instance.&lt;/p&gt;
&lt;p&gt;In a later post, I will describe how to create the fake data service, MyService.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318879" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Unit Testing ADO.NET Data Services</title><link>http://blogs.msdn.com/ploeh/archive/2009/01/13/unit-testing-ado-net-data-services.aspx</link><pubDate>Tue, 13 Jan 2009 15:05:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9314606</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9314606.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9314606</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9314606</wfw:comment><description>&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/data/bb931106.aspx" mce_href="http://msdn.microsoft.com/en-us/data/bb931106.aspx"&gt;ADO.NET Data Services&lt;/A&gt; enables you to expose data (including, but not limited to, relational data) as REST services. Since it's built on top of WCF, it can be tested &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx"&gt;utilizing similar techniques&lt;/A&gt;, but allow me to elaborate a bit on the subject.&lt;/P&gt;
&lt;P&gt;As with WCF, you might be interested in testing one or both tiers:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The service 
&lt;LI&gt;The client&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;Testing the service is the simplest undertaking, so I'll start with that and return to testing the client in a later post.&lt;/P&gt;
&lt;P&gt;Why would you want to unit test an ADO.NET Data Service?&lt;/P&gt;
&lt;P&gt;The mainstream scenario for ADO.NET Data Services is to expose a relational database using an ADO.NET Entity Model. Is it relevant to unit test such a service? Perhaps not. It depends on the amount of code you inject into the generated (partial) classes. If the amount of custom code is minimal, it may not make much sense to test simple CRUD operations.&lt;/P&gt;
&lt;P&gt;Conversely, if you have a greater degree of custom code, or if you implement an ADO.NET Data Service using non-relational data, it makes a lot of sense.&lt;/P&gt;
&lt;P&gt;My loyal readers will probably (correctly) suspect that I would tend to expose a Façade over a Domain Model as a data service, which implies going the non-relational route (even if the underlying data store is, in fact, relational). That is also the context for the rest of this post.&lt;/P&gt;
&lt;P&gt;First of all, you should keep in mind that the data service is just a normal class, so &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;you can test much of its functionality by just creating it and start using it&lt;/A&gt;:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  ServiceCanReturnRequestedItem()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 int\cf0  anonymousId = 3;\par ??    \cf4 string\cf0  expectedText = \cf6 "Ploeh"\cf0 ;\par ??\par ??    \cf3 Parent\cf0  anonymousParent = \cf4 new\cf0  \cf3 Parent\cf0 ();\par ??    anonymousParent.Id = anonymousId;\par ??    anonymousParent.Text = expectedText;\par ??\par ??    \cf3 MyService\cf0  sut = \cf4 new\cf0  \cf3 MyService\cf0 ();\par ??    sut.ParentStore.Add(anonymousParent);\par ??    \cf5 // Exercise system\par ??\cf0     \cf3 Parent\cf0  result = (\cf4 from\cf0  p \cf4 in\cf0  sut.Parents\par ??                     \cf4 where\cf0  p.Id == anonymousId\par ??                     \cf4 select\cf0  p).Single();\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedText, result.Text,\par ??        \cf6 "Text"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ServiceCanReturnRequestedItem()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;int&lt;/SPAN&gt; anonymousId = 3;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedText = &lt;SPAN style="COLOR: #a31515"&gt;"Ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; anonymousParent = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Id = anonymousId;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; anonymousParent.Text = expectedText;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;MyService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.ParentStore.Add(anonymousParent);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Parent&lt;/SPAN&gt; result = (&lt;SPAN style="COLOR: blue"&gt;from&lt;/SPAN&gt; p &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt; sut.Parents&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;where&lt;/SPAN&gt; p.Id == anonymousId&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;select&lt;/SPAN&gt; p).Single();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedText, result.Text,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Text"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Notice how most of the test just contains code setting up the &lt;A href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html" mce_href="http://xunitpatterns.com/test%20fixture%20-%20xUnit.html"&gt;Fixture&lt;/A&gt; by adding the relevant data to the service's underlying data store, while execution and verification is quickly accomplished.&lt;/P&gt;
&lt;P&gt;At some point, however, you will probably want to test the service through its REST interface. To do this, you must host the service within the test and then invoke it.&lt;/P&gt;
&lt;P&gt;Hosting the service in a test is easy:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  TestCanHostService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf3 Uri\cf0  address = \cf4 new\cf0  \cf3 Uri\cf0 (\cf6 "http://localhost/MyDataService"\cf0 );\par ??    \cf4 using\cf0  (\cf3 DataServiceHost\cf0  host = \par ??        \cf4 new\cf0  \cf3 DataServiceHost\cf0 (\cf4 typeof\cf0 (\cf3 MyDataService\cf0 ),\par ??            \cf4 new\cf0 [] \{ address \}))\par ??    \{\par ??        host.Open();\par ??        \cf5 // Exercise system\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Verify outcome\par ??\cf0         \cf5 // ...\par ??\cf0         \cf5 // Teardown\par ??\cf0     \}\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; TestCanHostService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt; address = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"http://localhost/MyDataService"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;using&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt; host = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DataServiceHost&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;MyDataService&lt;/SPAN&gt;),&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt;[] { address }))&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; host.Open();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// ...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the service is exposed through a REST interface, you can use more than one technology to invoke it. The ADO.NET Data Services client API is a natural fit, and I'll cover this in a later post. For now, just imagine that the test above contains client code that invokes the service and verifies the result.&lt;/P&gt;
&lt;P&gt;As it turns out, unit testing an ADO.NET Data Service isn't particularly difficult. Unit testing ADO.NET Data Service Clients is a bit more involved, so I'll cover that topic in a separate post. &lt;STRONG&gt;Update:&lt;/STRONG&gt; This post is now available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2009/01/14/unit-testing-ado-net-data-service-clients.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9314606" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Creating Azure Tables From Script</title><link>http://blogs.msdn.com/ploeh/archive/2008/12/04/creating-azure-tables-from-script.aspx</link><pubDate>Thu, 04 Dec 2008 15:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9174903</guid><dc:creator>ploeh</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9174903.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9174903</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9174903</wfw:comment><description>&lt;P&gt;When working with the &lt;A href="http://www.microsoft.com/azure/default.mspx" mce_href="http://www.microsoft.com/azure/default.mspx"&gt;Windows Azure&lt;/A&gt; Storage service, you must create the tables before you can use them; in essence, defining the 'schema' of your Azure storage service.&lt;/P&gt;
&lt;P&gt;On his blog, &lt;A href="http://blog.smarx.com/" mce_href="http://blog.smarx.com/"&gt;Steve Marx&lt;/A&gt; &lt;A href="http://blog.smarx.com/posts/try-to-create-tables-only-once" mce_href="http://blog.smarx.com/posts/try-to-create-tables-only-once"&gt;writes&lt;/A&gt;:&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;"Probably the best solution is to have separate initialization code that creates your tables.&amp;nbsp; This is analogous to the pattern of having &lt;CODE&gt;CREATE TABLE&lt;/CODE&gt; commands scripted in T-SQL which you run once to set up the database."&lt;/P&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I can only agree, but if you are relying on the CreateTablesFromModel method from the StorageClient data access API (from the Azure SDK), how can you do that?&lt;/P&gt;
&lt;P&gt;The first thing to realize is that since you can connect to your Azure Storage Service from anywhere, &lt;EM&gt;the script does not need to execute in the cloud&lt;/EM&gt;. You can just as well run this script from your local development machine, as long as you can connect to your storage account.&lt;/P&gt;
&lt;P&gt;You could obviously write a little utility that references StorageClient and your custom TableStorageDataServiceContext.&lt;/P&gt;
&lt;P&gt;Another, in my opinion, better option for such a one-off script is a PowerShell script. Here's my first take on such a script:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof1252\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;}??\fs20 $address = "http://table.core.windows.net"\par ??$account = "mbis"\par ??$accessKey = "SJ1EqZCUGoVejikA9GdPbMv+IAfpjDI9iOGGfAOMT0NPqXcheT+Vt+RS2o0N04lUG3rEna04+eRbDoJU4ocPOQ=="\par ??\par ??$storageClientPath = "C:\\MAG1_MBIS\\Spikes\\AzureServer\\Document Service\\MobileServerAzureDataAccess\\bin\\Debug\\StorageClient.dll"\par ??$contextPath = "C:\\MAG1_MBIS\\Spikes\\AzureServer\\Document Service\\MobileServerAzureDataAccess\\bin\\Debug\\Microsoft.Dynamics.Mobile.Server.AzureDataAccess.dll"\par ??\par ??[System.Reflection.Assembly]::LoadFrom($storageClientPath)\par ??[System.Reflection.Assembly]::LoadFrom($contextPath)\par ??\par ??$ai = New-Object Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo($address, $null, $account, $accessKey)\par ??\par ??$dataContext = New-Object Microsoft.Dynamics.Mobile.Server.AzureDataAccess.MobileServerDataServiceContext($ai)\par ??$t = $dataContext.GetType()\par ??\par ??$ts = [Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::Create($ai)\par ??$ts.ListTables() | % \{ $ts.DeleteTable($_) \}\par ??\par ??[Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::CreateTablesFromModel($t, $ai)\par ??}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;$address = "http://table.core.windows.net"&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$account = &amp;lt;accountName&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$accessKey = &amp;lt;accessKey&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$storageClientPath = &amp;lt;storageClientPath&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$contextPath = &amp;lt;contextPath&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[System.Reflection.Assembly]::LoadFrom($storageClientPath)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[System.Reflection.Assembly]::LoadFrom($contextPath)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ai = New-Object Microsoft.Samples.ServiceHosting.StorageClient.StorageAccountInfo($address, $null, $account, $accessKey)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$dataContext = New-Object &amp;lt;customContext&amp;gt;($ai)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$t = $dataContext.GetType()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ts = [Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::Create($ai)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;$ts.ListTables() | % { $ts.DeleteTable($_) }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;[Microsoft.Samples.ServiceHosting.StorageClient.TableStorage]::CreateTablesFromModel($t, $ai)&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;First of all, you will need to fill in the values that I've enclosed in angle brackets (remember quotes):&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;STRONG&gt;accountName&lt;/STRONG&gt; is the name of your Windows Azure Storage account &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;accessKey&lt;/STRONG&gt; is your Windows Azure Storage access key &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;storageClientPath&lt;/STRONG&gt; is the full path to the StorageClient assembly, e.g. "C:\Program Files\Windows Azure SDK\v1.0\Samples\StorageClient\Lib\bin\Debug\StorageClient.dll" &lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;contextPath&lt;/STRONG&gt; is the full path to the assembly that contains your custom TableStorageDataServiceContext&lt;/LI&gt;
&lt;LI&gt;&lt;STRONG&gt;customContext&lt;/STRONG&gt; is the fully qualified type name of your custom context (see the update comment below)&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;This script does the following:&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;It loads the StorageClient and your custom assembly into memory so types from those assemblies can be consumed by the script. &lt;/LI&gt;
&lt;LI&gt;It loops through all the tables currently in storage and &lt;EM&gt;deletes&lt;/EM&gt; them (delete the penultimate line if you don't want it to do this)! &lt;/LI&gt;
&lt;LI&gt;It creates the tables from your data model. &lt;/LI&gt;&lt;/OL&gt;
&lt;P&gt;Currently, the script has one limitation: Deleting a table using the StorageClient API only marks the table for deletion, so the operation returns much to soon. This means that if you are trying to recreate a table by the same name, a conflict will occur, and the table will not be created. You can work around this limitation by waiting a little while and then run the script again.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update:&lt;/STRONG&gt; In the original version of this post, I accidentally forgot to remove the specific type name of the custom context class that I'd used to create the script. Since you will not have access to that class, it doesn't make sense, so I replaced it with the &lt;EM&gt;customContext&lt;/EM&gt; placeholder.&lt;/P&gt;
&lt;P&gt;In case you were wondering, the original line of code was:&lt;/P&gt;
&lt;P&gt;$dataContext = New-Object Microsoft.Dynamics.Mobile.Server.AzureDataAccess.MobileServerDataServiceContext($ai)&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9174903" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>UAC Privileges Flow Across WCF Boundaries</title><link>http://blogs.msdn.com/ploeh/archive/2008/10/25/uac-privileges-flow-across-wcf-boundaries.aspx</link><pubDate>Sat, 25 Oct 2008 01:05:16 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9015307</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/9015307.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=9015307</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=9015307</wfw:comment><description>&lt;p&gt;Today, I just spent an entire afternoon troubleshooting a problem in one of my WCF services. When the solution finally dawned on me, it was so simple that I wanted to kick myself, so I thought that by sharing my experience, I might spare you the agony.&lt;/p&gt;  &lt;p&gt;In short, the setup was this: I had just developed a custom &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.serviceauthorizationmanager.aspx"&gt;ServiceAuthorizationManager&lt;/a&gt; that basically just used &lt;a href="http://msdn.microsoft.com/en-us/library/fs485fwh.aspx"&gt;WindowsPrincipal.IsInRole&lt;/a&gt; to test whether the caller (authenticated with Windows Authentication) was a member of &lt;em&gt;.\Administrators&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;I had already set it up on three of my four services, and it just worked as it should. On the fourth, however, it didn't.&lt;/p&gt;  &lt;p&gt;It was the same code running on all four services, and using the debugger, I could tell that in all four cases, the WindowsPrincipal in question represented my own account. However, in the first three services, IsInRole(&amp;quot;.\Administrators&amp;quot;) returned true as expected, whereas it returned false in the fourth service.&lt;/p&gt;  &lt;p&gt;Even more strange, the fourth service exhibited expected behavior if IsInRole was called against &lt;em&gt;Domain Users&lt;/em&gt;, or similar groups.&lt;/p&gt;  &lt;p&gt;As far as I could tell, there was no significant differences in the WCF bindings and behaviors for the four services, but since WCF is pretty complex, I spent a &lt;em&gt;lot&lt;/em&gt; of time comparing these without finding the reason.&lt;/p&gt;  &lt;p&gt;It finally occurred to me that I was running my little test application for the fourth service in non-elevated mode, and since I was running on Windows Server 2008, the &lt;em&gt;process&lt;/em&gt; didn't have administrator privileges because of UAC. That I knew, but it had never crossed my mind that this non-elevated mode flows across WCF boundaries, so even though the &lt;em&gt;service&lt;/em&gt; wasn't impacted by UAC itself, the Windows &lt;em&gt;credentials&lt;/em&gt; that represented my account was.&lt;/p&gt;  &lt;p&gt;When I restarted my test tool in elevated mode, everything worked as expected.&lt;/p&gt;  &lt;p&gt;It was my luck that I already had three working services when I ran into this issue; had I hit it on the first service, I might have concluded that my entire approach was unsound.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9015307" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>N-Tier Synchronization With The Sync Framework And WCF ChannelFactory (Part 2)</title><link>http://blogs.msdn.com/ploeh/archive/2008/08/27/n-tier-synchronization-with-the-sync-framework-and-wcf-channelfactory-part-2.aspx</link><pubDate>Wed, 27 Aug 2008 21:23:17 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8901015</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8901015.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8901015</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8901015</wfw:comment><description>&lt;p&gt;In my &lt;a href="http://blogs.msdn.com/ploeh/archive/2008/08/26/n-tier-synchronization-with-the-sync-framework-and-wcf-channelfactory-part-1.aspx"&gt;previous post&lt;/a&gt;, I discussed using &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.serversyncproviderproxy.aspx"&gt;ServerSyncProviderProxy&lt;/a&gt; with &lt;a href="http://msdn.microsoft.com/en-us/library/ms576132.aspx"&gt;ChannelFactory&amp;lt;T&amp;gt;&lt;/a&gt;, and explained how you can get up and running with that combination. While the described modification is definitely necessary, there's at least one other caveat that I've encountered so far.&lt;/p&gt;  &lt;p&gt;ChannelFactory&amp;lt;T&amp;gt; implements both &lt;a href="http://msdn.microsoft.com/en-us/library/system.servicemodel.icommunicationobject.aspx"&gt;ICommunicationObject&lt;/a&gt; and &lt;a href="http://msdn.microsoft.com/en-us/library/system.idisposable.aspx"&gt;IDisposable&lt;/a&gt;, and it's necessary to properly close an instance. A ChannelFactory&amp;lt;T&amp;gt; instance and any proxies that it creates are connected, and closing the ChannelFactory&amp;lt;T&amp;gt; instance will also close the proxies. However, that's not true the other way around.&lt;/p&gt;  &lt;p&gt;ServerSyncProviderProxy itself implements IDisposable, and it properly disposes of the proxy passed to it, but since it has no knowledge of the ChannelFactory used to create the proxy, it's not going to dispose of that.&lt;/p&gt;  &lt;p&gt;This causes the ServerSyncProviderProxy to hang (and ultimately time out) when you try to dispose of it.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Lesson #2&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;When using proxies created by &lt;a href="http://msdn.microsoft.com/en-us/library/ms585867.aspx"&gt;ChannelFactory&amp;lt;T&amp;gt;.CreateChannel&lt;/a&gt;, you must manually ensure that the ChannelFactory is properly disposed when the &lt;a href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.syncagent.aspx"&gt;SyncAgent&lt;/a&gt; disposes. Fortunately, that's not very hard to do; it just requires you to hold a reference to it in your derived SyncAgent and create it like this:&lt;/p&gt; &lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 this\cf0 .syncServiceFactory = \par ??    \cf1 new\cf0  \cf4 ChannelFactory\cf0 &amp;lt;\cf4 ISyncService\cf0 &amp;gt;(binding, serviceAddress.ToString());\par ??\cf4 ISyncService\cf0  proxy = \cf1 this\cf0 .syncServiceFactory.CreateChannel();\par ??\cf1 this\cf0 .RemoteProvider = \cf1 new\cf0  \cf4 ServerSyncProviderProxy\cf0 (proxy);}
--&gt;  &lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;   &lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.syncServiceFactory = &lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ChannelFactory&lt;/span&gt;&amp;lt;&lt;span style="color: #2b91af"&gt;ISyncService&lt;/span&gt;&amp;gt;(binding, serviceAddress.ToString());&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: #2b91af"&gt;ISyncService&lt;/span&gt; proxy = &lt;span style="color: blue"&gt;this&lt;/span&gt;.syncServiceFactory.CreateChannel();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;this&lt;/span&gt;.RemoteProvider = &lt;span style="color: blue"&gt;new&lt;/span&gt; &lt;span style="color: #2b91af"&gt;ServerSyncProviderProxy&lt;/span&gt;(proxy);&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Subsequently, you need to ensure that it's being properly disposed together with the SyncAgent itself:&lt;/p&gt;
&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 protected\cf0  \cf1 override\cf0  \cf1 void\cf0  Dispose(\cf1 bool\cf0  disposing)\par ??\{\par ??    \cf1 base\cf0 .Dispose(disposing);\par ??\par ??    \cf1 if\cf0  (disposing)\par ??    \{\par ??        \cf1 this\cf0 .syncServiceFactory.Close();\par ??    \}\par ??\}}
--&gt;

&lt;div style="font-size: 10pt; background: white; color: black; font-family: courier new"&gt;
  &lt;pre style="margin: 0px"&gt;&lt;span style="color: blue"&gt;protected&lt;/span&gt; &lt;span style="color: blue"&gt;override&lt;/span&gt; &lt;span style="color: blue"&gt;void&lt;/span&gt; Dispose(&lt;span style="color: blue"&gt;bool&lt;/span&gt; disposing)&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;{&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;base&lt;/span&gt;.Dispose(disposing);&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;if&lt;/span&gt; (disposing)&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; {&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; &lt;span style="color: blue"&gt;this&lt;/span&gt;.syncServiceFactory.Close();&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;&amp;#160;&amp;#160;&amp;#160; }&lt;/pre&gt;

  &lt;pre style="margin: 0px"&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This will allow the SyncAgent and, implicitly, the ServerSyncProviderProxy to close immediately.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8901015" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>N-Tier Synchronization With The Sync Framework And WCF ChannelFactory (Part 1)</title><link>http://blogs.msdn.com/ploeh/archive/2008/08/26/n-tier-synchronization-with-the-sync-framework-and-wcf-channelfactory-part-1.aspx</link><pubDate>Tue, 26 Aug 2008 22:01:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8898124</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8898124.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8898124</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8898124</wfw:comment><description>&lt;P&gt;&lt;A href="http://msdn.microsoft.com/en-us/library/bb726002.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb726002.aspx"&gt;Sync Services for ADO.NET 2.0&lt;/A&gt; allows a proper n-tier architecture where you can synchronize a client with a back-end data store via a web service. To do this, your web service must expose a simple interface consisting of four operations. As &lt;A href="http://msdn.microsoft.com/en-us/library/bb902831.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb902831.aspx"&gt;described in the documentation&lt;/A&gt;, one of these operations must have the following signature:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 [\cf3 OperationContract\cf0 ]\par ??\cf3 SyncSchema\cf0  GetSchema(\cf3 Collection\cf0 &amp;lt;\cf4 string\cf0 &amp;gt; tableNames, \cf3 SyncSession\cf0  syncSession);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;OperationContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SyncSchema&lt;/SPAN&gt; GetSchema(&lt;SPAN style="COLOR: #2b91af"&gt;Collection&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt; tableNames, &lt;SPAN style="COLOR: #2b91af"&gt;SyncSession&lt;/SPAN&gt; syncSession);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;When used in a &lt;A href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.syncagent.aspx" mce_href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.syncagent.aspx"&gt;SyncAgent&lt;/A&gt;, a proxy is used with &lt;A href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.serversyncproviderproxy.aspx" mce_href="http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.serversyncproviderproxy.aspx"&gt;ServerSyncProviderProxy&lt;/A&gt; like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 this\cf0 .RemoteProvider = \cf1 new\cf0  \cf4 ServerSyncProviderProxy\cf0 (proxy);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.RemoteProvider = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ServerSyncProviderProxy&lt;/SPAN&gt;(proxy);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;One thing to notice about the ServerSyncProviderProxy constructor is that it takes as input &lt;EM&gt;any&lt;/EM&gt; object (that is, an instance of System.Object). Obviously, you can't just pass in any object and expect it to work at run-time, so there's actually a sort of duck typing in play here.&lt;/P&gt;
&lt;P&gt;As far as I can tell, ServerSyncProviderProxy uses Reflection to invoke the expected methods, such as the GetSchema method shown above.&lt;/P&gt;
&lt;P&gt;That probably works well for a WCF proxy generated by Visual Studio (I don't know, because I haven't tried), but fails if you try to reference the service interface (let's call it ISyncService) directly and create a proxy using &lt;A href="http://msdn.microsoft.com/en-us/library/ms575250.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms575250.aspx"&gt;ChannelFactory&amp;lt;T&amp;gt;.CreateChannel&lt;/A&gt;.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 ChannelFactory\cf0 &amp;lt;\cf1 ISyncService\cf0 &amp;gt; cf = \par ??    \cf4 new\cf0  \cf1 ChannelFactory\cf0 &amp;lt;\cf1 ISyncService\cf0 &amp;gt;(binding, serviceAddress.ToString());\par ??\cf1 ISyncService\cf0  proxy = cf.CreateChannel();\par ??\cf4 this\cf0 .RemoteProvider = \cf4 new\cf0  \cf1 ServerSyncProviderProxy\cf0 (proxy);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ISyncService&lt;/SPAN&gt;&amp;gt; cf = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ISyncService&lt;/SPAN&gt;&amp;gt;(binding, serviceAddress.ToString());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;ISyncService&lt;/SPAN&gt; proxy = cf.CreateChannel();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.RemoteProvider = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ServerSyncProviderProxy&lt;/SPAN&gt;(proxy);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;If you do that, you will get a run-time exception. What happens is that ServerSyncProviderProxy expects the GetSchema method to take a string[], not a Collection&amp;lt;string&amp;gt;. Although I have no authoritative explanation for this, it's not that surprising, since the default proxy code generator for WCF (svcutil, Visual Studio) will create proxies with lists of elements represented by arrays - you have to explicitly state that you want collections instead, if that's the case.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Lesson #1&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;If you want to create a proxy instance using ChannelFactory&amp;lt;T&amp;gt;, you will have to change the signature of the GetSchema method to&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 [\cf3 OperationContract\cf0 ]\par ??\cf3 SyncSchema\cf0  GetSchema(\cf4 string\cf0 [] tableNames, \cf3 SyncSession\cf0  syncSession);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;OperationContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;SyncSchema&lt;/SPAN&gt; GetSchema(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;[] tableNames, &lt;SPAN style="COLOR: #2b91af"&gt;SyncSession&lt;/SPAN&gt; syncSession);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;That will enable you to use a proxy created by ChannelFactory&amp;lt;T&amp;gt;, but will obviously break for proxies generated by svcutil or Visual Studio. If you must support both scenarios, you will need to derive from ServerSyncProviderProxy and override the GetSchema method to do something more intelligent.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update (2008-08-27):&lt;/STRONG&gt; Part 2 is now available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/08/27/n-tier-synchronization-with-the-sync-framework-and-wcf-channelfactory-part-2.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/08/27/n-tier-synchronization-with-the-sync-framework-and-wcf-channelfactory-part-2.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8898124" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>An Overview of Unit Testing Duplex WCF Services and Clients</title><link>http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx</link><pubDate>Sat, 12 Jul 2008 08:50:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8722659</guid><dc:creator>ploeh</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8722659.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8722659</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8722659</wfw:comment><description>&lt;P&gt;In the last couple of posts, I've demonstrated how to isolate implementation from WCF contract definition and behavior in a duplex communication scenario. These posts have been rather detailed, so it occurred to me that you might benefit from an overview.&lt;/P&gt;
&lt;P&gt;The main goal was to ensure testability of implementations of both service and client. While the contracts reference WCF (System.ServiceModel), the implementations themselves only reference the contracts, and not WCF.&lt;/P&gt;
&lt;P&gt;&lt;IMG style="BORDER-TOP-WIDTH: 0px; BORDER-LEFT-WIDTH: 0px; BORDER-BOTTOM-WIDTH: 0px; BORDER-RIGHT-WIDTH: 0px" height=511 alt=DependencyGraph src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/AnOverviewofUnitTestingDuplexWCFServices_BE1D/DependencyGraph_3.png" width=520 border=0 mce_src="http://blogs.msdn.com/blogfiles/ploeh/WindowsLiveWriter/AnOverviewofUnitTestingDuplexWCFServices_BE1D/DependencyGraph_3.png"&gt; &lt;/P&gt;
&lt;P&gt;This diagram illustrates dependencies between the various libraries. The left side contains server-side components, while the right side contains client-side components. The only shared components are WCF and the Contract library.&lt;/P&gt;
&lt;P&gt;Note that neither StuffServer nor StuffAgent has any dependency on System.ServiceModel, although they have a dependency on Contract. The same is true for both unit test projects.&lt;/P&gt;
&lt;P&gt;The top boxes on both sides (MyServer and MyClient) are hypothetical top-level executables such as I've &lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx"&gt;discussed before&lt;/A&gt;, although never shown any code from. These &lt;A href="http://xunitpatterns.com/Humble%20Object.html" mce_href="http://xunitpatterns.com/Humble%20Object.html"&gt;Humble Executables&lt;/A&gt; tie everything together by referencing all relevant libraries, but if you prefer, you can just as well use a configurable Dependency Injection container - &lt;A href="http://orand.blogspot.com/" mce_href="http://orand.blogspot.com/"&gt;Oran Dennison&lt;/A&gt; explains &lt;A href="http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html" mce_href="http://orand.blogspot.com/2006/10/wcf-service-dependency-injection.html"&gt;how to do this on Spring.NET&lt;/A&gt;, while &lt;A href="http://www.ayende.com/Blog/Default.aspx" mce_href="http://www.ayende.com/Blog/Default.aspx"&gt;Ayende&lt;/A&gt; has a &lt;A href="http://www.ayende.com/Blog/archive/2007/06/12/WCF-Windsor-Integration.aspx" mce_href="http://www.ayende.com/Blog/archive/2007/06/12/WCF-Windsor-Integration.aspx"&gt;Windsor ServiceHostFactory&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;Here are the main posts on the subject:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx"&gt;Unit Testing Duplex WCF Services&lt;/A&gt;&lt;/LI&gt;
&lt;LI&gt;&lt;A href="http://blogs.msdn.com/ploeh/archive/2008/07/08/unit-testing-duplex-wcf-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/07/08/unit-testing-duplex-wcf-clients.aspx"&gt;Unit Testing Duplex WCF Clients&lt;/A&gt;&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;If you would like to take a closer look at my sample code, I've attached it to this post. As usual, the standard disclaimers apply.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8722659" width="1" height="1"&gt;</description><enclosure url="http://blogs.msdn.com/ploeh/attachment/8722659.ashx" length="41601" type="application/x-zip-compressed" /><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Unit Testing Duplex WCF Clients</title><link>http://blogs.msdn.com/ploeh/archive/2008/07/08/unit-testing-duplex-wcf-clients.aspx</link><pubDate>Wed, 09 Jul 2008 00:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8709795</guid><dc:creator>ploeh</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8709795.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8709795</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8709795</wfw:comment><description>&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx"&gt;previous post&lt;/A&gt;, I explained how to unit test a WCF service with callbacks. Since the scenario involves duplex communication, the service is also a client, and vice versa, so it's only reasonable to look at the 'client' as well, to examine how to best approach unit testing such a beast.&lt;/P&gt;
&lt;P&gt;Since both ends both act as sender and receiver, they are each both client and service. None the less, there's still a clear distinction, since the &lt;EM&gt;client&lt;/EM&gt; is the party that initiates and terminates a session, whereas the &lt;EM&gt;service&lt;/EM&gt; is always ready and available for whatever client that may come along.&lt;/P&gt;
&lt;P&gt;Ensuring proper isolation and testability is most difficult for the service, which was why I decided to tackle that issue first. Making the client testable is much easier, and I'll describe how to do that here by continuing the example from my former post.&lt;/P&gt;
&lt;P&gt;When writing the client, the only thing I really need to abstract away is the creation of the WCF proxy and its callback instance.&lt;/P&gt;
&lt;P&gt;When you look at the standard WCF documentation for duplex clients, a duplex proxy can be created by a &lt;A href="http://msdn.microsoft.com/en-us/library/ms576164.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms576164.aspx"&gt;DuplexChannelFactory&amp;lt;TChannel&amp;gt;&lt;/A&gt; like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 DuplexChannelFactory\cf0 &amp;lt;\cf1 IStuffService\cf0 &amp;gt; cf = \par ??    \cf4 new\cf0  \cf1 DuplexChannelFactory\cf0 &amp;lt;\cf1 IStuffService\cf0 &amp;gt;(callbackService,\par ??        binding, remoteAddress);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;DuplexChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt; cf = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DuplexChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt;(callbackService,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; binding, remoteAddress);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;You can then invoke &lt;A href="http://msdn.microsoft.com/en-us/library/ms575250.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms575250.aspx"&gt;CreateChannel&lt;/A&gt; on the cf instance to get an instance of an IStuffService proxy.&lt;/P&gt;
&lt;P&gt;However, now that you have decided to implement your client without any reference to WCF, the use of DuplexChannelFactory&amp;lt;TChannel&amp;gt; and its ilk from within that library is not permitted (and should indeed be impossible, since you don't have a reference to System.ServiceModel).&lt;/P&gt;
&lt;P&gt;What the client really needs is something that can create an instance of an IStuffService proxy, while itself receiving an instance of IStuffCallbackService (which is the type of the callbackService parameter above). In other words, I'll need to define an abstraction that takes IStuffCallbackService as input and returns IStuffService.&lt;/P&gt;
&lt;P&gt;In a move very similar to the approach described &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx"&gt;previously&lt;/A&gt;, a Func&amp;lt;IStuffCallbackService, IStuffService&amp;gt; is just what we need. Hence, a rudimentary implementation of StuffClient would be similar to this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 StuffClient\cf0  : \cf4 IStuffCallbackService\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 , \cf4 IStuffService\cf0 &amp;gt; createProxy_;\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 List\cf0 &amp;lt;\cf1 string\cf0 &amp;gt; stuffResults_;\par ??\par ??    \cf1 public\cf0  StuffClient(\cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 , \cf4 IStuffService\cf0 &amp;gt; proxyCreator)\par ??    \{\par ??        \cf1 if\cf0  (proxyCreator == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "proxyCreator"\cf0 );\par ??        \}\par ??\par ??        \cf1 this\cf0 .createProxy_ = proxyCreator;\par ??        \cf1 this\cf0 .stuffResults_ = \cf1 new\cf0  \cf4 List\cf0 &amp;lt;\cf1 string\cf0 &amp;gt;();\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  DoStuff(\cf1 string\cf0  stuff)\par ??    \{\par ??        \cf4 IStuffService\cf0  proxy = \cf1 this\cf0 .createProxy_(\cf1 this\cf0 );\par ??        proxy.DoStuff(stuff);\par ??\par ??        \cf6 // Remeber to safely dispose of proxy\par ??\cf0         \cf4 IDisposable\cf0  disp = proxy \cf1 as\cf0  \cf4 IDisposable\cf0 ;\par ??        \cf1 if\cf0  (disp != \cf1 null\cf0 )\par ??        \{\par ??            disp.Dispose();\par ??        \}\par ??    \}\par ??\par ??    \cf6 // Other members, including IStuffCallbackService Members...\par ??\par ??\cf0     \cf1 public\cf0  \cf4 IList\cf0 &amp;lt;\cf1 string\cf0 &amp;gt; StuffResults\par ??    \{\par ??        \cf1 get\cf0  \{ \cf1 return\cf0  \cf1 this\cf0 .stuffResults_; \}\par ??    \}\par ??\par ??\cf1     #region\cf0  IStuffCallbackService Members\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  StuffWasDone(\cf1 string\cf0  result)\par ??    \{\par ??        \cf1 this\cf0 .stuffResults_.Add(result);\par ??    \}\par ??\par ??\cf1     #endregion\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt; createProxy_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; StuffClient(&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt; proxyCreator)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (proxyCreator == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"proxyCreator"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.createProxy_ = proxyCreator;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuff(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuff)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt; proxy = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.createProxy_(&lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; proxy.DoStuff(stuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Remeber to safely dispose of proxy&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Other members, including IStuffCallbackService Members...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The abstract proxy creator is required by way of Constructor Injection, and is used in the DoStuff method to create an IStuffService proxy instance, which may or may not be a WCF proxy.&lt;/P&gt;
&lt;P&gt;If the proxy instance &lt;EM&gt;is&lt;/EM&gt; a real WCF proxy, it will implement IDisposable and should be properly closed. There are several strategies for dealing with this situation, but to keep the example as simple as possible, I've chosen to omit this particular aspect, and leave it as an exercise for the interested reader :)&lt;/P&gt;
&lt;P&gt;Although I have yet to show the actual implementation, note that StuffClient implements IStuffCallbackService, so it can return itself to the createProxy_ delegate instance as the callback service. Obviously, if you use the same object as both sender and receiver, you need to do so in a thread-safe manner, but since I can, in this particular context, bask in the luxury of writing sample code, I choose to ignore thread-safety completely :)&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;}??\fs20 \cf1 #region\cf0  IStuffCallbackService Members\par ??\par ??\cf1 public\cf0  \cf1 void\cf0  StuffWasDone(\cf1 string\cf0  result)\par ??\{\par ??    \cf1 this\cf0 .stuffResults_.Add(result);\par ??\}\par ??\par ??\cf1 #endregion}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#region&lt;/SPAN&gt; IStuffCallbackService Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; StuffWasDone(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; result)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.stuffResults_.Add(result);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;#endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;where stuffResults_ is a List&amp;lt;string&amp;gt;.&lt;/P&gt;
&lt;P&gt;Unit testing StuffClient is now fairly simple. We'll need an implementation of IStuffService to act as a &lt;A href="http://msdn.microsoft.com/en-us/magazine/cc163358.aspx" mce_href="http://msdn.microsoft.com/en-us/magazine/cc163358.aspx"&gt;Test Double&lt;/A&gt;, so I've created a simple SpyStuffService class that records all the &lt;EM&gt;stuff&lt;/EM&gt; strings in a List&amp;lt;string&amp;gt; called Stuffs.&lt;/P&gt;
&lt;P&gt;First of all, we need to ensure that the proxyCreator delegate instance receives a proper instance of IStuffCallbackService as input:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DoStuffWillCreateProxyByPassingNonNullCallback()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousStuff = \cf6 "ploeh"\cf0 ;\par ??    \cf3 SpyStuffService\cf0  dummy = \cf4 new\cf0  \cf3 SpyStuffService\cf0 ();\par ??    \cf3 StuffClient\cf0  sut = \cf4 new\cf0  \cf3 StuffClient\cf0 (callbackService =&amp;gt;\par ??        \{\par ??            \cf3 Assert\cf0 .IsNotNull(callbackService,\par ??                \cf6 "Callback service"\cf0 );\par ??            \cf4 return\cf0  dummy;\par ??        \});\par ??    \cf5 // Exercise system\par ??\cf0     sut.DoStuff(anonymousStuff);\par ??    \cf5 // Verify outcome (done by inline mock);\par ??\cf0     \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuffWillCreateProxyByPassingNonNullCallback()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; anonymousStuff = &lt;SPAN style="COLOR: #a31515"&gt;"ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt; dummy = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt;(callbackService =&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.IsNotNull(callbackService,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #a31515"&gt;"Callback service"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; dummy;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.DoStuff(anonymousStuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome (done by inline mock);&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;A bit unusually, test verification happens inside the delegate defined by the lambda expression passed to the constructor of StuffClient - a technique I call an &lt;EM&gt;Inline Mock&lt;/EM&gt;. Since C# is strongly typed, I can be assured that if callbackService is not null, it &lt;EM&gt;will&lt;/EM&gt; be an instance of IStuffCallbackService.&lt;/P&gt;
&lt;P&gt;Next, we may want to verify that when DoStuff is invoked, data will be transferred to the service:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DoStuffWillSendStuffToService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  expectedStuff = \cf6 "ploeh"\cf0 ;\par ??    \cf3 SpyStuffService\cf0  spy = \cf4 new\cf0  \cf3 SpyStuffService\cf0 ();\par ??    \cf3 StuffClient\cf0  sut =\par ??        \cf4 new\cf0  \cf3 StuffClient\cf0 (callbackService =&amp;gt; spy);\par ??    \cf5 // Exercise system\par ??\cf0     sut.DoStuff(expectedStuff);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedStuff, \par ??        spy.Stuffs.First(), \cf6 "Stuff"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuffWillSendStuffToService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedStuff = &lt;SPAN style="COLOR: #a31515"&gt;"ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt; spy = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt; sut =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt;(callbackService =&amp;gt; spy);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.DoStuff(expectedStuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedStuff, &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; spy.Stuffs.First(), &lt;SPAN style="COLOR: #a31515"&gt;"Stuff"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The Spy is passed to the &lt;A href="http://xunitpatterns.com/SUT.html" mce_href="http://xunitpatterns.com/SUT.html"&gt;SUT&lt;/A&gt; in the constructor, and later inspected in the verification phase. Pretty classic four-phase unit test setup...&lt;/P&gt;
&lt;P&gt;Finally, we may want to test the SUT's implementation of IStuffCallbackService:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  StuffWasDoneWillAddResultToStuffResultList()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  expectedStuff = \cf6 "ploeh"\cf0 ;\par ??    \cf3 SpyStuffService\cf0  dummy = \cf4 new\cf0  \cf3 SpyStuffService\cf0 ();\par ??    \cf3 StuffClient\cf0  sut = \par ??        \cf4 new\cf0  \cf3 StuffClient\cf0 (callbackService =&amp;gt; dummy);\par ??    \cf5 // Exercise system\par ??\cf0     sut.StuffWasDone(expectedStuff);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedStuff,\par ??        sut.StuffResults.First(), \cf6 "Stuff result"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; StuffWasDoneWillAddResultToStuffResultList()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedStuff = &lt;SPAN style="COLOR: #a31515"&gt;"ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt; dummy = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt; sut = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt;(callbackService =&amp;gt; dummy);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.StuffWasDone(expectedStuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedStuff,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.StuffResults.First(), &lt;SPAN style="COLOR: #a31515"&gt;"Stuff result"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;At this point, the SpyStuffService instance is not used by the SUT, but we need it for the Func&amp;lt;IStuffCallbackService, IStuffService&amp;gt; instance used in the StuffClient constructor, so it's a classic example of a Test Dummy.&lt;/P&gt;
&lt;P&gt;The test invokes the StuffWasDone method and subsequently verifies that the string passed to it becomes available in the StuffResults list, so again, it's really just a standard four-phase unit test.&lt;/P&gt;
&lt;P&gt;If you've managed to read this far, I owe it to you to show how to set up StuffClient with a real WCF proxy. Remember, this must be done in another library that can reference System.ServiceModel, since the library where StuffClient is implemented must not reference WCF. The most natural place to do this is in a &lt;A href="http://xunitpatterns.com/Humble%20Object.html" mce_href="http://xunitpatterns.com/Humble%20Object.html"&gt;Humble Object&lt;/A&gt;, such as a top-level executable, as described &lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red43\green145\blue175;\red255\green255\blue255;\red0\green0\blue0;\red0\green0\blue255;}??\fs20 \cf1 StuffClient\cf0  client = \cf4 new\cf0  \cf1 StuffClient\cf0 (callbackService =&amp;gt;\par ??    \{\par ??        \cf1 DuplexChannelFactory\cf0 &amp;lt;\cf1 IStuffService\cf0 &amp;gt; cf = \par ??            \cf4 new\cf0  \cf1 DuplexChannelFactory\cf0 &amp;lt;\cf1 IStuffService\cf0 &amp;gt;(callbackService,\par ??                binding, remoteAddress);\par ??        \cf4 return\cf0  cf.CreateChannel();\par ??    \});}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt; client = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffClient&lt;/SPAN&gt;(callbackService =&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;DuplexChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt; cf = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;DuplexChannelFactory&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&amp;gt;(callbackService,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; binding, remoteAddress);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; cf.CreateChannel();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; });&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Note that this is the place where WCF reappears after its long absence. The StuffClient implementation has no dependency on WCF, and testability is ensured.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update (2008-07-12):&lt;/STRONG&gt; I've just posted an &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx"&gt;overview of the solution, as well as all the sample code&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8709795" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Unit Testing Duplex WCF Services</title><link>http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx</link><pubDate>Sat, 28 Jun 2008 15:44:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8663475</guid><dc:creator>ploeh</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8663475.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8663475</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8663475</wfw:comment><description>&lt;P&gt;One of my readers &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#8643548" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/04/IntegrationTestingWcfServices.aspx#8643548"&gt;recently asked&lt;/A&gt; me about unit testing WCF services when they have callbacks. Given that I strongly believe that you should attempt to &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/26/implementing-wcf-services-without-referencing-wcf.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/26/implementing-wcf-services-without-referencing-wcf.aspx"&gt;implement your services without referencing WCF at all&lt;/A&gt;, but duplex WCF services require you to get the callback instance from &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx"&gt;OperationContext.Current&lt;/A&gt;, how can these two forces be reconciled?&lt;/P&gt;
&lt;P&gt;Fortunately, it's really not that hard. All you have to do is to replace the call to &lt;A href="http://msdn.microsoft.com/en-us/library/ms575542.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms575542.aspx"&gt;OperationContext.GetCallbackChannel&amp;lt;T&amp;gt;&lt;/A&gt; with something abstract. On .NET 3.5, the easiest abstraction is &lt;A href="http://msdn.microsoft.com/en-us/library/bb534960.aspx" mce_href="http://msdn.microsoft.com/en-us/library/bb534960.aspx"&gt;Func&amp;lt;TResult&amp;gt;&lt;/A&gt;, which has the same signature, but if you are on .NET 3.0, you can always define a similar delegate type of your own.&lt;/P&gt;
&lt;P&gt;Let's say that your contracts look like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 [\cf3 ServiceContract\cf0 (CallbackContract = \cf4 typeof\cf0 (\cf3 IStuffCallbackService\cf0 ))]\par ??\cf4 public\cf0  \cf4 interface\cf0  \cf3 IStuffService\par ??\cf0 \{\par ??    [\cf3 OperationContract\cf0 ]\par ??    \cf4 void\cf0  DoStuff(\cf4 string\cf0  stuff);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;ServiceContract&lt;/SPAN&gt;(CallbackContract = &lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;))]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;OperationContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuff(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 interface\cf0  \cf4 IStuffCallbackService\par ??\cf0 \{\par ??    [\cf4 OperationContract\cf0 ]\par ??    \cf1 void\cf0  StuffWasDone(\cf1 string\cf0  result);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;OperationContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; StuffWasDone(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; result);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since it would ruin testability of the IStuffService implementation if it was to use OperationContext.GetCallbackChannel&amp;lt;T&amp;gt; to create a new instance of IStuffCallbackService, it needs an instance of Func&amp;lt;IStuffCallbackService&amp;gt; instead. As I favor Constructor Injection, the complete implementation looks like this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 StuffService\cf0  : \cf4 IStuffService\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; createCallbackChannel_;\par ??\par ??    \cf1 public\cf0  StuffService(\cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; callbackCreator)\par ??    \{\par ??        \cf1 if\cf0  (callbackCreator == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "callbackCreator"\cf0 );\par ??        \}\par ??        \cf1 this\cf0 .createCallbackChannel_ = callbackCreator;\par ??    \}\par ??\par ??\cf1     #region\cf0  IStuffService Members\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  DoStuff(\cf1 string\cf0  stuff)\par ??    \{\par ??        \cf6 // Implementation goes here...\par ??\cf0         \cf1 string\cf0  stuffResult =\par ??            \cf1 new\cf0  \cf1 string\cf0 (stuff.ToCharArray().Reverse().ToArray());\par ??\par ??        \cf1 this\cf0 .createCallbackChannel_().StuffWasDone(stuffResult);\par ??    \}\par ??\par ??\cf1     #endregion\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&amp;gt; createCallbackChannel_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; StuffService(&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&amp;gt; callbackCreator)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (callbackCreator == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"callbackCreator"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.createCallbackChannel_ = callbackCreator;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IStuffService Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuff(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuff)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Implementation goes here...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuffResult =&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;(stuff.ToCharArray().Reverse().ToArray());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.createCallbackChannel_().StuffWasDone(stuffResult);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Such an implementation is imminently testable, as this test demonstrates:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;\red0\green128\blue0;\red163\green21\blue21;}??\fs20 [\cf3 TestMethod\cf0 ]\par ??\cf4 public\cf0  \cf4 void\cf0  DoStuffWillInvokeCallbackService()\par ??\{\par ??    \cf5 // Fixture setup\par ??\cf0     \cf4 string\cf0  anonymousStuff = \cf6 "ploeh"\cf0 ;\par ??    \cf4 string\cf0  expectedResult = \par ??        \cf4 new\cf0  \cf4 string\cf0 (anonymousStuff.ToCharArray().Reverse().ToArray());\par ??\par ??    \cf3 SpyStuffCallbackService\cf0  spy = \cf4 new\cf0  \cf3 SpyStuffCallbackService\cf0 ();\par ??\par ??    \cf3 StuffService\cf0  sut = \cf4 new\cf0  \cf3 StuffService\cf0 (() =&amp;gt; spy);\par ??    \cf5 // Exercise system\par ??\cf0     sut.DoStuff(anonymousStuff);\par ??    \cf5 // Verify outcome\par ??\cf0     \cf3 Assert\cf0 .AreEqual&amp;lt;\cf4 string\cf0 &amp;gt;(expectedResult,\par ??        spy.StuffResults.First(), \cf6 "Callback result"\cf0 );\par ??    \cf5 // Teardown\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;TestMethod&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuffWillInvokeCallbackService()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Fixture setup&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; anonymousStuff = &lt;SPAN style="COLOR: #a31515"&gt;"ploeh"&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; expectedResult = &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;(anonymousStuff.ToCharArray().Reverse().ToArray());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffCallbackService&lt;/SPAN&gt; spy = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SpyStuffCallbackService&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt; sut = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt;(() =&amp;gt; spy);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Exercise system&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; sut.DoStuff(anonymousStuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Verify outcome&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Assert&lt;/SPAN&gt;.AreEqual&amp;lt;&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt;&amp;gt;(expectedResult,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; spy.StuffResults.First(), &lt;SPAN style="COLOR: #a31515"&gt;"Callback result"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Teardown&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The SpyStuffCallbackService class is a simple &lt;A href="http://xunitpatterns.com/Test%20Spy.html" mce_href="http://xunitpatterns.com/Test%20Spy.html"&gt;Test Spy&lt;/A&gt; that records all the callbacks in the StuffResults collection.&lt;/P&gt;
&lt;P&gt;When you let WCF host the service, you need to tell WCF to use OperationContext.GetCallbackChannel&amp;lt;IStuffCallbackService&amp;gt; as the delegate instance to the StuffService constructor. In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx"&gt;previous post&lt;/A&gt;, I demonstrated how to do that (that's what StuffInstancingBehavior.GetInstance does).&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update (2008-07-12):&lt;/STRONG&gt; I've just posted an &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx"&gt;overvview of the solution, as well as all the sample code&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8663475" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Modifying Behavior of WCF-Free Service Implementations</title><link>http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx</link><pubDate>Fri, 27 Jun 2008 23:55:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8662832</guid><dc:creator>ploeh</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8662832.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8662832</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8662832</wfw:comment><description>&lt;P&gt;In my &lt;A href="http://blogs.msdn.com/ploeh/archive/2008/06/26/implementing-wcf-services-without-referencing-wcf.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/26/implementing-wcf-services-without-referencing-wcf.aspx"&gt;previous post&lt;/A&gt;, I explained how to implement a WCF service without referencing WCF. In simple cases, it works as described, but you may soon find yourself in a situation where you need to modify the behavior of the service when it's hosted by WCF.&lt;/P&gt;
&lt;P&gt;Perhaps you need to control the service's &lt;A title=ConcurrencyMode href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.concurrencymode.aspx"&gt;ConcurrencyMode&lt;/A&gt;, or perhaps you need to set &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.usesynchronizationcontext.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.usesynchronizationcontext.aspx"&gt;UseSynchronizationContext&lt;/A&gt;. These options are typically controlled by the &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx"&gt;ServiceBehaviorAttribute&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;You may also want to provide an &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.dispatcher.iinstanceprovider.aspx"&gt;IInstanceProvider&lt;/A&gt; via a custom attribute that implements &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.icontractbehavior.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.icontractbehavior.aspx"&gt;IContractBehavior&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;However, you can't set these attributes on the service implementation itself, since it mustn't have a reference to System.ServiceModel.&lt;/P&gt;
&lt;P&gt;In the rest of this post, I'll provide an example of how to achieve such results while still keeping WCF out of the service implementation.&lt;/P&gt;
&lt;P&gt;Consider this service implementation:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 StuffService\cf0  : \cf4 IStuffService\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; createCallbackChannel_;\par ??\par ??    \cf1 public\cf0  StuffService(\cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; callbackCreator)\par ??    \{\par ??        \cf1 if\cf0  (callbackCreator == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "callbackCreator"\cf0 );\par ??        \}\par ??        \cf1 this\cf0 .createCallbackChannel_ = callbackCreator;\par ??    \}\par ??\par ??\cf1     #region\cf0  IStuffService Members\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  DoStuff(\cf1 string\cf0  stuff)\par ??    \{\par ??        \cf6 // Implementation goes here...\par ??\cf0         \cf1 string\cf0  stuffResult =\par ??            \cf1 new\cf0  \cf1 string\cf0 (stuff.ToCharArray().Reverse().ToArray());\par ??\par ??        \cf1 this\cf0 .createCallbackChannel_().StuffWasDone(stuffResult);\par ??    \}\par ??\par ??\cf1     #endregion\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&amp;gt; createCallbackChannel_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; StuffService(&lt;SPAN style="COLOR: #2b91af"&gt;Func&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&amp;gt; callbackCreator)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;if&lt;/SPAN&gt; (callbackCreator == &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;throw&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;ArgumentNullException&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #a31515"&gt;"callbackCreator"&lt;/SPAN&gt;);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.createCallbackChannel_ = callbackCreator;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// IStuffService members here...&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;There are many cases (Dependency Injection is a common reason) where a a non-default constructor is desirable, so if you are wondering about this weird-looking Func&amp;lt;IStuffCallbackService&amp;gt; parameter, just consider that it might as well have been some other type of dependency. In a later post (edit 2008.06.29: available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/28/unit-testing-duplex-wcf-services.aspx"&gt;here&lt;/A&gt;), it will become apparent just why the constructor takes this specific input - for now, the important point is that the service implementation only has one, non-default constructor, and that it will throw if passed a null value.&lt;/P&gt;
&lt;P&gt;Since WCF by default manages a service instance's lifetime, and the default behavior is to create a new instance per request, WCF requires the service to have a default constructor; that is, unless an IInstanceProvider is provided.&lt;/P&gt;
&lt;P&gt;IInstanceProvider is defined in System.ServiceModel, so I can't implement this interface directly in the service library; instead, I create a new library that defines the WCF hosting behavior for the service. This library references &lt;EM&gt;both&lt;/EM&gt; System.ServiceModel and the service library (the library that contains StuffService), so essentially, it's the &lt;A href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2007/05/30/CodeAsDependencyConfiguration.aspx"&gt;library that sits on top of the dependency hierarchy and wires everything together&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In this library, I can implement IInstanceProvider via an &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.icontractbehavior.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.description.icontractbehavior.aspx"&gt;IContractBehavior&lt;/A&gt; to provide new instances of StuffService:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 StuffInstancingBehavior\cf0  :\par ??    \cf4 IContractBehavior\cf0 , \cf4 IInstanceProvider\par ??\cf0 \{\par ??\cf1     #region\cf0  IInstanceProvider Members\par ??\par ??    \cf1 public\cf0  \cf1 object\cf0  GetInstance(\cf4 InstanceContext\cf0  instanceContext,\par ??        \cf4 Message\cf0  message)\par ??    \{\par ??        \cf1 return\cf0  \cf1 this\cf0 .GetInstance(instanceContext);\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 object\cf0  GetInstance(\cf4 InstanceContext\cf0  instanceContext)\par ??    \{\par ??        \cf1 return\cf0  \cf1 new\cf0  \cf4 StuffService\cf0 (() =&amp;gt;\par ??            \cf4 OperationContext\cf0 .Current.\par ??            GetCallbackChannel&amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt;());\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  ReleaseInstance(\cf4 InstanceContext\cf0  instanceContext,\par ??        \cf1 object\cf0  instance) \{ \}\par ??\par ??\cf1     #endregion\par ??\par ??    #region\cf0  IContractBehavior Members\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  AddBindingParameters(\cf4 ContractDescription\cf0  description,\par ??        \cf4 ServiceEndpoint\cf0  endpoint,\par ??        \cf4 BindingParameterCollection\cf0  bindingParameters) \{ \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  ApplyClientBehavior(\cf4 ContractDescription\cf0  description,\par ??        \cf4 ServiceEndpoint\cf0  endpoint, \cf4 ClientRuntime\cf0  clientRuntime) \{ \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  ApplyDispatchBehavior(\cf4 ContractDescription\cf0  description,\par ??        \cf4 ServiceEndpoint\cf0  endpoint, \cf4 DispatchRuntime\cf0  dispatchRuntime)\par ??    \{\par ??        dispatchRuntime.InstanceProvider = \cf1 this\cf0 ;\par ??    \}\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  Validate(\cf4 ContractDescription\cf0  description,\par ??        \cf4 ServiceEndpoint\cf0  endpoint) \{ \}\par ??\par ??\cf1     #endregion\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffInstancingBehavior&lt;/SPAN&gt; :&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;IContractBehavior&lt;/SPAN&gt;, &lt;SPAN style="COLOR: #2b91af"&gt;IInstanceProvider&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IInstanceProvider Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetInstance(&lt;SPAN style="COLOR: #2b91af"&gt;InstanceContext&lt;/SPAN&gt; instanceContext,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;Message&lt;/SPAN&gt; message)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.GetInstance(instanceContext);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; GetInstance(&lt;SPAN style="COLOR: #2b91af"&gt;InstanceContext&lt;/SPAN&gt; instanceContext)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt;(() =&amp;gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;OperationContext&lt;/SPAN&gt;.Current.&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; GetCallbackChannel&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;IStuffCallbackService&lt;/SPAN&gt;&amp;gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ReleaseInstance(&lt;SPAN style="COLOR: #2b91af"&gt;InstanceContext&lt;/SPAN&gt; instanceContext,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;object&lt;/SPAN&gt; instance) { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IContractBehavior Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; AddBindingParameters(&lt;SPAN style="COLOR: #2b91af"&gt;ContractDescription&lt;/SPAN&gt; description,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ServiceEndpoint&lt;/SPAN&gt; endpoint,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;BindingParameterCollection&lt;/SPAN&gt; bindingParameters) { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ApplyClientBehavior(&lt;SPAN style="COLOR: #2b91af"&gt;ContractDescription&lt;/SPAN&gt; description,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ServiceEndpoint&lt;/SPAN&gt; endpoint, &lt;SPAN style="COLOR: #2b91af"&gt;ClientRuntime&lt;/SPAN&gt; clientRuntime) { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; ApplyDispatchBehavior(&lt;SPAN style="COLOR: #2b91af"&gt;ContractDescription&lt;/SPAN&gt; description,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ServiceEndpoint&lt;/SPAN&gt; endpoint, &lt;SPAN style="COLOR: #2b91af"&gt;DispatchRuntime&lt;/SPAN&gt; dispatchRuntime)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; dispatchRuntime.InstanceProvider = &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; Validate(&lt;SPAN style="COLOR: #2b91af"&gt;ContractDescription&lt;/SPAN&gt; description,&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: #2b91af"&gt;ServiceEndpoint&lt;/SPAN&gt; endpoint) { }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;There are really only two lines of code of interest in this code listing: ApplyDispatchBehavior associates this IInstanceProvider implementation with the DispatchRuntime, and the GetInstance method creates the StuffService instance itself.&lt;/P&gt;
&lt;P&gt;Notice that while we will not accept the use of &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx"&gt;OperationContext.Current&lt;/A&gt; in the service library, it's perfectly legal to use it here.&lt;/P&gt;
&lt;P&gt;Applying an IEndpointBehavior can be done either via configuration or by programmatically adding it to the &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicehost.aspx"&gt;ServiceHost&lt;/A&gt; before opening it.&lt;/P&gt;
&lt;P&gt;Since WCF will not be able to create an instance of StuffService without StuffInstancingBehavior, it's not really &lt;EM&gt;optional&lt;/EM&gt;, so making it configurable doesn't make a whole lot of sense. This means that I should add it programmatically.&lt;/P&gt;
&lt;P&gt;To support hosting in as wide a range of scenarios as possible, the best way to ensure that the ServiceHost is always created correctly (even when hosted in IIS) is by implementing a dedicated &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.servicehostfactory.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.activation.servicehostfactory.aspx"&gt;ServiceHostFactory&lt;/A&gt; (let's call it StuffServiceHostFactory). The &lt;A href="http://msdn.microsoft.com/en-us/library/ms554316.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms554316.aspx"&gt;CreateServiceHost&lt;/A&gt; method can be implemented as simply as this:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 return\cf0  \cf1 new\cf0  \cf4 StuffServiceHost\cf0 (baseAddresses);}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffServiceHost&lt;/SPAN&gt;(baseAddresses);&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Obviously, the real behavior configuration takes place in the StuffServiceHost class, which derives from ServiceHost. The entire implementation takes place in the constructor:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;}??\fs20 \cf1 public\cf0  StuffServiceHost(\cf1 params\cf0  \cf4 Uri\cf0 [] baseAddresses)\par ??    : \cf1 base\cf0 (\cf1 typeof\cf0 (\cf4 StuffService\cf0 ), baseAddresses)\par ??\{\par ??    \cf1 foreach\cf0  (\cf4 ContractDescription\cf0  cd \cf1 in\par ??\cf0         \cf1 this\cf0 .ImplementedContracts.Values)\par ??    \{\par ??        cd.Behaviors.Add(\cf1 new\cf0  \cf4 StuffInstancingBehavior\cf0 ());\par ??    \}\par ??\par ??    \cf1 this\cf0 .Description.Behaviors.Find&amp;lt;\cf4 ServiceBehaviorAttribute\cf0 &amp;gt;().\par ??        ConcurrencyMode = \cf4 ConcurrencyMode\cf0 .Reentrant;\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; StuffServiceHost(&lt;SPAN style="COLOR: blue"&gt;params&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;Uri&lt;/SPAN&gt;[] baseAddresses)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; : &lt;SPAN style="COLOR: blue"&gt;base&lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;typeof&lt;/SPAN&gt;(&lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt;), baseAddresses)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;foreach&lt;/SPAN&gt; (&lt;SPAN style="COLOR: #2b91af"&gt;ContractDescription&lt;/SPAN&gt; cd &lt;SPAN style="COLOR: blue"&gt;in&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.ImplementedContracts.Values)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cd.Behaviors.Add(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffInstancingBehavior&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.Description.Behaviors.Find&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;ServiceBehaviorAttribute&lt;/SPAN&gt;&amp;gt;().&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ConcurrencyMode = &lt;SPAN style="COLOR: #2b91af"&gt;ConcurrencyMode&lt;/SPAN&gt;.Reentrant;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;It adds StuffInstancingBehavior to all contracts, which will cause WCF to use its implementation of IInstanceProvider each time it wants to create a new instance of the StuffService. It also modifies the service's ConcurrencyMode; again, &lt;EM&gt;why&lt;/EM&gt; it does that will become apparent in a future post, but I just wanted to show how you can programmatically achieve the same result as decorating your service with the ServiceBehaviorAttribute, since you can't do that directly on StuffService because it's being implemented in a library that doesn't reference WCF.&lt;/P&gt;
&lt;P&gt;If you want to host StuffService in IIS, you just need to specify StuffServiceHostFactory in the .svc file's Factory attribute; if you want to host StuffService in your own process, you can just create a new instance of StuffServiceHost directly (or use an instance of StuffServiceHostFactory to create it).&lt;/P&gt;
&lt;P&gt;All this behavior modification is taking place without StuffService being aware of it at all, and you can concentrate on implementing the service in whatever way you prefer, while keeping it totally testable and WCF runtime services far away from it. That's a very clean separation of concerns in my book.&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Update (2008-07-12):&lt;/STRONG&gt; Sample code is available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/07/12/an-overview-of-unit-testing-duplex-wcf-services-and-clients.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8662832" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Dependency+Injection/default.aspx">Dependency Injection</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Implementing WCF Services Without Referencing WCF</title><link>http://blogs.msdn.com/ploeh/archive/2008/06/26/implementing-wcf-services-without-referencing-wcf.aspx</link><pubDate>Thu, 26 Jun 2008 10:24:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8655246</guid><dc:creator>ploeh</dc:creator><slash:comments>4</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8655246.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8655246</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8655246</wfw:comment><description>&lt;P&gt;More than a year ago, I wrote my first post on &lt;A href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2006/12/03/UnitTestingWCFServices.aspx"&gt;unit testing WCF services&lt;/A&gt;. One of my points back then was that you have to be careful that the service implementation doesn't use any of the services provided by the WCF runtime environment (if you want to keep the service testable). As soon as you invoke something like &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.operationcontext.current.aspx"&gt;OperationContext.Current&lt;/A&gt;, your code is not going to work in a unit testing scenario, but only when hosted by WCF.&lt;/P&gt;
&lt;P&gt;This point is as true today as it was one and a half year ago.&lt;/P&gt;
&lt;P&gt;So how do you ensure that you don't accidentally invoke WCF runtime services in the service code?&lt;/P&gt;
&lt;P&gt;By being very, very careful..?&lt;/P&gt;
&lt;P&gt;Unless you are very disciplined and work alone on the service, that's probably not a very good solution. While &lt;EM&gt;you&lt;/EM&gt; may be skillful enough to pull it off, Joe Junior Programmer is going to come along during your one week of vacation and mess it all up.&lt;/P&gt;
&lt;P&gt;Fortunately, a more robust solution exists: You can actually implement a WCF service without referencing WCF at all. How about that?&lt;/P&gt;
&lt;P&gt;Obviously, when &lt;EM&gt;declaring&lt;/EM&gt; the service contract, you will need to reference System.ServiceModel to put the right attributes on the contract:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue0;\red255\green255\blue255;\red43\green145\blue175;\red0\green0\blue255;}??\fs20 [\cf3 ServiceContract\cf0 (CallbackContract = \cf4 typeof\cf0 (\cf3 IStuffCallbackService\cf0 ))]\par ??\cf4 public\cf0  \cf4 interface\cf0  \cf3 IStuffService\par ??\cf0 \{\par ??    [\cf3 OperationContract\cf0 ]\par ??    \cf4 void\cf0  DoStuff(\cf4 string\cf0  stuff);\par ??\}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;[&lt;SPAN style="COLOR: #2b91af"&gt;ServiceContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;interface&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; [&lt;SPAN style="COLOR: #2b91af"&gt;OperationContract&lt;/SPAN&gt;]&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuff(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuff);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;The contract library contains only the contract, but no implementation. The concrete implementation can be placed in a &lt;EM&gt;separate&lt;/EM&gt; library that &lt;EM&gt;doesn't&lt;/EM&gt; reference System.ServiceModel. As long as you have a reference to the contract library, you can still implement the service:&lt;/P&gt;&lt;!--
{\rtf1\ansi\ansicpg\lang1024\noproof65001\uc1 \deff0{\fonttbl{\f0\fnil\fcharset0\fprq1 Courier New;}}{\colortbl;??\red0\green0\blue255;\red255\green255\blue255;\red0\green0\blue0;\red43\green145\blue175;\red163\green21\blue21;\red0\green128\blue0;}??\fs20 \cf1 public\cf0  \cf1 class\cf0  \cf4 StuffService\cf0  : \cf4 IStuffService\par ??\cf0 \{\par ??    \cf1 private\cf0  \cf1 readonly\cf0  \cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; createCallbackChannel_;\par ??\par ??    \cf1 public\cf0  StuffService(\cf4 Func\cf0 &amp;lt;\cf4 IStuffCallbackService\cf0 &amp;gt; callbackCreator)\par ??    \{\par ??        \cf1 if\cf0  (callbackCreator == \cf1 null\cf0 )\par ??        \{\par ??            \cf1 throw\cf0  \cf1 new\cf0  \cf4 ArgumentNullException\cf0 (\cf5 "callbackCreator"\cf0 );\par ??        \}\par ??        \cf1 this\cf0 .createCallbackChannel_ = callbackCreator;\par ??    \}\par ??\par ??\cf1     #region\cf0  IStuffService Members\par ??\par ??    \cf1 public\cf0  \cf1 void\cf0  DoStuff(\cf1 string\cf0  stuff)\par ??    \{\par ??        \cf6 // Implementation goes here...\par ??\cf0         \cf1 string\cf0  stuffResult =\par ??            \cf1 new\cf0  \cf1 string\cf0 (stuff.ToCharArray().Reverse().ToArray());\par ??\par ??        \cf1 this\cf0 .createCallbackChannel_().StuffWasDone(stuffResult);\par ??    \}\par ??\par ??\cf1     #endregion\par ??\cf0 \}}
--&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: courier new"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;StuffService&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;IStuffService&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{ &lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #region&lt;/SPAN&gt; IStuffService Members&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;void&lt;/SPAN&gt; DoStuff(&lt;SPAN style="COLOR: blue"&gt;string&lt;/SPAN&gt; stuff)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: green"&gt;// Implementation goes here...&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; #endregion&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;
&lt;P&gt;Since the entire WCF part of the contract is specified using attributes, you don't need a reference to System.ServiceModel in the implementation library even though the contract library requires this reference.&lt;/P&gt;
&lt;P&gt;Now you can implement the service without fear of accidentally adding WCF dependencies, since you don't have a reference to WCF at all.&lt;/P&gt;
&lt;P&gt;To prevent other developers from accidentally adding a reference to System.ServiceModel while you are away, you can actively &lt;A href="http://feeds.feedburner.com/~r/AyendeRahien/~3/283774939/Actively-enforce-your-conventions.aspx" mce_href="http://feeds.feedburner.com/~r/AyendeRahien/~3/283774939/Actively-enforce-your-conventions.aspx"&gt;enforce that convention&lt;/A&gt;, as exemplified in &lt;A href="http://codebetter.com/blogs/glenn.block/default.aspx" mce_href="http://codebetter.com/blogs/glenn.block/default.aspx"&gt;Glenn&lt;/A&gt;'s &lt;A href="http://codebetter.com/blogs/glenn.block/archive/2008/05/04/prismshouldnotreferenceunity.aspx" mce_href="http://codebetter.com/blogs/glenn.block/archive/2008/05/04/prismshouldnotreferenceunity.aspx"&gt;post&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;In very simple cases, this is all you need to do, but I'll admit that this example is a bit naive; as soon as you need to apply behaviors or other WCF-specific modifications to the service, it becomes more complex. In many cases, this is done by applying attributes to the service implementation (such as &lt;A href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.servicebehaviorattribute.aspx"&gt;ServiceBehaviorAttribute&lt;/A&gt;), but this is not possible when you don't have a reference to System.ServiceModel.&lt;/P&gt;
&lt;P&gt;In a future post, I'll explain how to get around some of these issues. &lt;STRONG&gt;Update:&lt;/STRONG&gt; This post is now available &lt;A class="" href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx" mce_href="http://blogs.msdn.com/ploeh/archive/2008/06/27/modifying-behavior-of-wcf-free-service-implementations.aspx"&gt;here&lt;/A&gt;.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8655246" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Unit+Testing/default.aspx">Unit Testing</category><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item><item><title>Declarative Use of Custom SecurityTokenParameters</title><link>http://blogs.msdn.com/ploeh/archive/2008/05/01/DeclarativeUseOfCustomSecurityTokenParameters.aspx</link><pubDate>Thu, 01 May 2008 23:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:8447435</guid><dc:creator>ploeh</dc:creator><slash:comments>5</slash:comments><comments>http://blogs.msdn.com/ploeh/comments/8447435.aspx</comments><wfw:commentRss>http://blogs.msdn.com/ploeh/commentrss.aspx?PostID=8447435</wfw:commentRss><wfw:comment>http://blogs.msdn.com/ploeh/rsscomments.aspx?PostID=8447435</wfw:comment><description>&lt;P&gt;It's not the first time I've stated this, but one of the reasons I love WCF is that it's so wonderfully extensible. You can even implement your own custom security token, as &lt;A class="" href="http://msdn.microsoft.com/en-us/library/ms731872.aspx" mce_href="http://msdn.microsoft.com/en-us/library/ms731872.aspx"&gt;this article explains&lt;/A&gt;. The only problem with this article is that it uses imperative code to create a custom Binding, and it doesn't explain how you can implement a custom security token mechanism in a declarative way (i.e. using app.config).&lt;/P&gt;
&lt;P&gt;The offending part is the custom SecurityTokenParameters, which you can't specify declaratively, but have to attach to a SecurityBindingElement in some way.&lt;/P&gt;
&lt;P&gt;So if you still want to be able to specify the use of your custom security token in app.config, how can you implement that?&lt;/P&gt;
&lt;P&gt;A simple solution I've found involves creating a&amp;nbsp;custom BindingElement that contains all the custom security token implementation, including the custom SecurityTokenParameters. This &lt;A class="" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.bindingelement.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.channels.bindingelement.aspx"&gt;BindingElement&lt;/A&gt; additionally acts as a Decorator for whatever SecurityBindingElement you really want to use:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;class&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CreditCardSecurityBindingElement&lt;/SPAN&gt; : &lt;SPAN style="COLOR: #2b91af"&gt;BindingElement&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;readonly&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SymmetricSecurityBindingElement&lt;/SPAN&gt; innerBindingElement_;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; CreditCardSecurityBindingElement()&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerBindingElement_ = &lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;SymmetricSecurityBindingElement&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerBindingElement_.EndpointSupportingTokenParameters.SignedEncrypted.Add(&lt;SPAN style="COLOR: blue"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: #2b91af"&gt;CreditCardTokenParameters&lt;/SPAN&gt;());&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;        //..&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: green"&gt;    //..&lt;/SPAN&gt;&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;To fully implement your custom BindingElement, remember to override all its virtual methods to delegate the functionality to the inner SecurityBindingElement, like this:&lt;/P&gt;
&lt;DIV style="FONT-SIZE: 10pt; BACKGROUND: white; COLOR: black; FONT-FAMILY: Courier New"&gt;&lt;PRE style="MARGIN: 0px"&gt;&lt;SPAN style="COLOR: blue"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;override&lt;/SPAN&gt; T GetProperty&amp;lt;T&amp;gt;(&lt;SPAN style="COLOR: #2b91af"&gt;BindingContext&lt;/SPAN&gt; context)&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;{&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN style="COLOR: blue"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: blue"&gt;this&lt;/SPAN&gt;.innerBindingElement_.GetProperty&amp;lt;T&amp;gt;(context);&lt;/PRE&gt;&lt;PRE style="MARGIN: 0px"&gt;}&lt;/PRE&gt;&lt;/DIV&gt;&lt;!--EndFragment--&gt;
&lt;P&gt;To be able to use your custom BindingElement (CreditCardSecurityBindingElement) declaratively as part of a custom binding in app.config, you need to implement a &lt;A class="" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.bindingelementextensionelement.aspx" mce_href="http://msdn.microsoft.com/en-us/library/system.servicemodel.configuration.bindingelementextensionelement.aspx"&gt;BindingElementExtensionElement&lt;/A&gt; that creates it. Once you have done that, you should be good to go.&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=8447435" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/ploeh/archive/tags/Services/default.aspx">Services</category></item></channel></rss>