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

<channel>
	<title>Emil's Blog</title>
	<atom:link href="http://www.meadow.se/wordpress/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://www.meadow.se/wordpress</link>
	<description>Programming Windows, .Net, EPiServer and whatnot...</description>
	<lastBuildDate>Tue, 01 Jun 2010 21:18:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>NDepend 3</title>
		<link>http://www.meadow.se/wordpress/?p=429</link>
		<comments>http://www.meadow.se/wordpress/?p=429#comments</comments>
		<pubDate>Tue, 01 Jun 2010 21:18:23 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[.Net programming]]></category>
		<category><![CDATA[Tools]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=429</guid>
		<description><![CDATA[In case you missed it, there&#8217;s a very powerful tool called NDepend that helps you analyze your code structure and detect potential problems very easily. A new version, NDepend 3,  was released earlier this year and the biggest new feature is probably Visual Studio integration:

It still has all the code metrics you&#8217;ll probably ever [...]]]></description>
			<content:encoded><![CDATA[<p>In case you missed it, there&#8217;s a very powerful tool called <strong>NDepend</strong> that helps you analyze your code structure and detect potential problems very easily. A new version, <strong>NDepend 3</strong>,  was released earlier this year and the biggest new feature is probably Visual Studio integration:</p>
<p><a href="http://www.meadow.se/wordpress/wp-content/uploads/2010/06/NDepend3.jpg"><img src="http://www.meadow.se/wordpress/wp-content/uploads/2010/06/NDepend3-300x187.jpg" alt="" title="NDepend 3 screen shot" width="300" height="187" class="alignnone size-medium wp-image-432" /></a></p>
<p>It still has all the code metrics you&#8217;ll probably ever need, a custom made code query language, diagrams (some of which are still a little difficult to read) and graphs to help with analysis. See <a href="http://www.meadow.se/wordpress/?p=333">my post about the previous version</a> for some examples.</p>
<p>Here are a few live examples of using NDepend: <a href="http://www.ndepend.com/Features.aspx#Tour">http://www.ndepend.com/Features.aspx#Tour<br />
</a></p>
<p>If you&#8217;re into code analysis you should give NDepend a try. With this new version it&#8217;s more usable than before and it still has the same very powerful analysis engine under the hood. Check out <a href="http://www.ndepend.com">http://www.ndepend.com</a> for more info.</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=429</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Running .Net 4 RC NUnits tests in TeamCity</title>
		<link>http://www.meadow.se/wordpress/?p=423</link>
		<comments>http://www.meadow.se/wordpress/?p=423#comments</comments>
		<pubDate>Tue, 16 Mar 2010 12:57:39 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[.Net 4]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[TeamCity]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=423</guid>
		<description><![CDATA[Today, we finally found a way to run NUnit tests in .Net 4 assemblies in TeamCity! Here&#8217;s the solution:
Problem with NUnit runner when using MSBuild 4.0
/Emil
]]></description>
			<content:encoded><![CDATA[<p>Today, we finally found a way to run NUnit tests in .Net 4 assemblies in TeamCity! Here&#8217;s the solution:</p>
<p><a href="http://www.jetbrains.net/devnet/thread/286678">Problem with NUnit runner when using MSBuild 4.0</a></p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=423</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Authentication for IIS-hosted WCF services</title>
		<link>http://www.meadow.se/wordpress/?p=415</link>
		<comments>http://www.meadow.se/wordpress/?p=415#comments</comments>
		<pubDate>Tue, 02 Mar 2010 22:49:39 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=415</guid>
		<description><![CDATA[WCF is very powerful and very, very complicated to configure in many cases. Seemingly simple requirements can get really difficult to get right and security definitely falls in that category. I recently had the following need:

WCF-services hosted in IIS 7.5
Windows authentication should be used for the services
The client must not be required to be in [...]]]></description>
			<content:encoded><![CDATA[<p>WCF is very powerful and very, very complicated to configure in many cases. Seemingly simple requirements can get really difficult to get right and security definitely falls in that category. I recently had the following need:</p>
<ul>
<li>WCF-services hosted in IIS 7.5</li>
<li>Windows authentication should be used for the services</li>
<li>The client must not be required to be in the same domain as the service application</li>
</ul>
<p>Simple enough, right? Not for me it wasn't...</p>
<p>My first idea was to set the authentication settings in IIS to Windows Authentication and configure the client to use credentials from a config file, this was the method I'm used to when calling ASMX services, but it didn't work. It was really frustrating as well as it's so difficult to understand what's going on. </p>
<p>No matter what changes I did in the configuration files, I kept getting errors such as</p>
<div class="syntax_hilite">
<div id="code-4">
<div class="code">The HTTP request is unauthorized with client authentication scheme <span style="color:#CC0000;">'Anonymous'</span>.<br />
<span style="">The</span> authentication header received from the server was <span style="color:#CC0000;">'Negotiate,NTLM'</span>.</div>
</div>
</div>
<p></p>
<p>I was finally able to come up with a solution which I thuoght I'd share with you. I don't know if it's optimal, but if you think it's not then please leave a comment.</p>
<p>What I did was this:</p>
<ul>
<li>Disable IIS authentication by setting it to Anonymous in the IIS Manager</li>
<li>In the binding configuration for the services, set security mode to Message and to use Windows Authentication:
<div class="syntax_hilite">
<div id="xml-5">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;security</span> <span style="color: #000066;">mode</span>=<span style="color: #ff0000;">"Message"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp;<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;message</span> <span style="color: #000066;">clientCredentialType</span>=<span style="color: #ff0000;">"Windows"</span> <span style="font-weight: bold; color: black;">/&gt;</span></span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/security<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></li>
<li>
On the client side we now have the option to pass on the currently logged in user's credentials (requires no extra handling, just call the service) or to explicitly give the credentials to use. The latter is done like this for a ClientBase&lt;T&gt; proxy:</p>
<div class="syntax_hilite">
<div id="csharp-6">
<div class="csharp">client.<span style="color: #0000FF;">ClientCredentials</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">ClientCredential</span>.<span style="color: #0000FF;">Domain</span> = <span style="color: #808080;">"xxx"</span>;<br />
client.<span style="color: #0000FF;">ClientCredentials</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">ClientCredential</span>.<span style="color: #0000FF;">UserName</span> = <span style="color: #808080;">"yyy"</span>;<br />
client.<span style="color: #0000FF;">ClientCredentials</span>.<span style="color: #0000FF;">Windows</span>.<span style="color: #0000FF;">ClientCredential</span>.<span style="color: #0000FF;">Password</span> = <span style="color: #808080;">"zzz"</span>;<br />
client.<span style="color: #0000FF;">MyMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;</div>
</div>
</div>
<p>
</li>
<p>This solved the problem for me. I think the major difficulty I had was that IIS and WCF security settings collided, so I believe the lesson learned is to do security on only one place and that place should probably be WCF.</p>
<p>On a related note, I have also had problems with service discovery, WSDL, etc if the web application is not set to Anonymous authentication in IIS, so it seems to be a good idea to do it like that for that reason as well.</p>
<p>Also, I'm not the only one to have problems with WCF security, here are a few others:<br />
<a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/838a6572-a733-463f-97f5-eb127a93f070">Post 1</a>, <a href="http://social.msdn.microsoft.com/Forums/en-US/wcf/thread/ab7c0abf-141f-48d5-b5ba-2a3129539019">Post 2</a>.</p>
<p>/Emil</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=415</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Templates for SQL Server Management Studio</title>
		<link>http://www.meadow.se/wordpress/?p=410</link>
		<comments>http://www.meadow.se/wordpress/?p=410#comments</comments>
		<pubDate>Tue, 26 Jan 2010 08:45:55 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[SQL Server]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=410</guid>
		<description><![CDATA[Templates for SQL Server Management Studio 2008 are stored in this folder on WIndows XP:
C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql
On my new WIndows 7 machine, the location is:
C:\Users\[user]\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql
Create sub-folders and SQL files there and they show up in the Template Explorer. Really useful...
/Emil
]]></description>
			<content:encoded><![CDATA[<p>Templates for SQL Server Management Studio 2008 are stored in this folder on WIndows XP:</p>
<p><strong>C:\Documents and Settings\[user]\Application Data\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql</strong></p>
<p>On my new WIndows 7 machine, the location is:</p>
<p><strong>C:\Users\[user]\AppData\Roaming\Microsoft\Microsoft SQL Server\100\Tools\Shell\Templates\Sql</strong></p>
<p>Create sub-folders and SQL files there and they show up in the Template Explorer. Really useful...</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=410</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Script parameter parsing in PowerShell</title>
		<link>http://www.meadow.se/wordpress/?p=404</link>
		<comments>http://www.meadow.se/wordpress/?p=404#comments</comments>
		<pubDate>Thu, 21 Jan 2010 13:32:23 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[PowerShell]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=404</guid>
		<description><![CDATA[This is an example of how to do script parameter parsing in PowerShell:


switch &#40;$args&#41;
&#123;
&#160; 'salesforceservice'
&#160; &#123;
&#160; &#160; $deploy_service = $true
&#160; &#125;
&#160; 'web'
&#160; &#123;
&#160; &#160; $deploy_web = $true
&#160; &#125;
&#160; default
&#160; &#123;
&#160; &#160; "Bad parameter: " + $_
&#160; &#160; ShowHelp
&#160; &#160; exit
&#160; &#125;
&#125;



Pretty neat, huh?
/Emil
]]></description>
			<content:encoded><![CDATA[<p>This is an example of how to do script parameter parsing in PowerShell:</p>
<div class="syntax_hilite">
<div id="code-8">
<div class="code">switch <span style="color:#006600; font-weight:bold;">&#40;</span>$args<span style="color:#006600; font-weight:bold;">&#41;</span><br />
<span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; <span style="color:#CC0000;">'salesforceservice'</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; $deploy_service = $true<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; <span style="color:#CC0000;">'web'</span><br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; $deploy_web = $true<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span></p>
<p>&nbsp; default<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#123;</span><br />
&nbsp; &nbsp; <span style="color:#CC0000;">"Bad parameter: "</span> + $_<br />
&nbsp; &nbsp; ShowHelp<br />
&nbsp; &nbsp; exit<br />
&nbsp; <span style="color:#006600; font-weight:bold;">&#125;</span><br />
<span style="color:#006600; font-weight:bold;">&#125;</span></div>
</div>
</div>
<p></p>
<p>Pretty neat, huh?</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=404</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NUnit with SQLite and .Net 4.0 Beta 2</title>
		<link>http://www.meadow.se/wordpress/?p=393</link>
		<comments>http://www.meadow.se/wordpress/?p=393#comments</comments>
		<pubDate>Fri, 20 Nov 2009 11:05:41 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[.Net programming]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=393</guid>
		<description><![CDATA[SQLite and unit testing is a great combination for testing database operations without having to manage database files. You can simply create an in-memory database in your setup code and work with that. Perfect in combination with NHibernate for example.
If you want to do this in the current .Net 4.0 beta your out of luck [...]]]></description>
			<content:encoded><![CDATA[<p>SQLite and unit testing is a great combination for testing database operations without having to manage database files. You can simply create an in-memory database in your setup code and work with that. Perfect in combination with NHibernate for example.</p>
<p>If you want to do this in the current .Net 4.0 beta your out of luck though, you'll get an exception:</p>
<div class="syntax_hilite">
<div id="code-13">
<div class="code">System.<span style="">IO</span>.<span style="">FileLoadException</span>: Mixed mode assembly is built against version <span style="color:#CC0000;">'v2.0.50727'</span> of the runtime<br />
and cannot be loaded in the <span style="color:#800000;">4</span>.<span style="color:#800000;">0</span> runtime without additional configuration information.</div>
</div>
</div>
<p></p>
<p>The solution is pointed out by <a href="http://blogs.msdn.com/jomo_fisher/archive/2009/11/17/f-scripting-net-4-0-and-mixed-mode-assemblies.aspx">Jomo Fisher</a>. What you do is to include this snippet in the application config file:</p>
<div class="syntax_hilite">
<div id="xml-14">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;startup</span> useLegacyV2<span style="color: #000066;">RuntimeActivationPolicy</span>=<span style="color: #ff0000;">"true"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #ddbb00;">&lt;</span>supportedRuntime version=&quot;v4.0&quot;/<span style="color: #ddbb00;">&gt;</span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/startup<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>When unit testing assemblies that references System.Data.SQLite.DLL then you have to put that snippet in <strong>NUnit's config file</strong> (C:\Program Files\NUnit 2.5.2\bin\net-2.0\nunit.exe.config).</p>
<p>If you combine this with the tip in my post <a href="http://www.meadow.se/wordpress/?p=386">NUnit with Visual Studio 2010 Beta 2</a>, you should insert the following</p>
<div class="syntax_hilite">
<div id="xml-15">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;startup</span> useLegacyV2<span style="color: #000066;">RuntimeActivationPolicy</span>=<span style="color: #ff0000;">"true"</span><span style="font-weight: bold; color: black;">&gt;</span></span><br />
&nbsp; <span style="color: #ddbb00;">&lt;</span>supportedRuntime version=&quot;v4.0&quot;/<span style="color: #ddbb00;">&gt;</span><br />
&nbsp; <span style="color: #ddbb00;">&lt;</span>requiredRuntime version=&quot;v4.0.20506&quot; /<span style="color: #ddbb00;">&gt;</span><br />
<span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;/startup<span style="font-weight: bold; color: black;">&gt;</span></span></span></div>
</div>
</div>
<p></p>
<p>plus</p>
<div class="syntax_hilite">
<div id="xml-16">
<div class="xml"><span style="color: #ddbb00;">&lt;</span>loadFromRemoteSources enabled=&quot;true&quot; /<span style="color: #ddbb00;">&gt;</span></div>
</div>
</div>
<p></p>
<p>under the <strong>runtime</strong> tag.</p>
<p>This works for me, hopefully it will for you as well.</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=393</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>log4net configuration Xml Schema</title>
		<link>http://www.meadow.se/wordpress/?p=391</link>
		<comments>http://www.meadow.se/wordpress/?p=391#comments</comments>
		<pubDate>Thu, 19 Nov 2009 09:26:40 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[Tools]]></category>
		<category><![CDATA[log4net]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=391</guid>
		<description><![CDATA[I just found a very useful XSD for editing log4net configurations:
http://csharptest.net/?p=38
Just copy the schema file to C:\Program Files\Microsoft Visual Studio *\Xml\Schemas and add the correct namespace to the log4net element in your config file:


&#60;log4net xsi:noNamespaceSchemaLocation="http://csharptest.net/downloads/schema/log4net.xsd" 
&#160; &#160; &#160; &#160; &#160; xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&#62;



/Emil
]]></description>
			<content:encoded><![CDATA[<p>I just found a very useful XSD for editing log4net configurations:</p>
<p><a href="http://csharptest.net/?p=38">http://csharptest.net/?p=38</a></p>
<p>Just copy the schema file to <em>C:\Program Files\Microsoft Visual Studio *\Xml\Schemas</em> and add the correct namespace to the log4net element in your config file:</p>
<div class="syntax_hilite">
<div id="xml-18">
<div class="xml"><span style="color: #009900;"><span style="font-weight: bold; color: black;">&lt;log4net</span> xsi:<span style="color: #000066;">noNamespaceSchemaLocation</span>=<span style="color: #ff0000;">"http://csharptest.net/downloads/schema/log4net.xsd"</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xmlns:<span style="color: #000066;">xsi</span>=<span style="color: #ff0000;">"http://www.w3.org/2001/XMLSchema-instance"</span><span style="font-weight: bold; color: black;">&gt;</span></span></div>
</div>
</div>
<p></p>
<p>/Emil</log4net></p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=391</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NUnit with Visual Studio 2010 Beta 2</title>
		<link>http://www.meadow.se/wordpress/?p=386</link>
		<comments>http://www.meadow.se/wordpress/?p=386#comments</comments>
		<pubDate>Mon, 16 Nov 2009 12:35:44 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[.Net programming]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[NUnit]]></category>
		<category><![CDATA[Visual Studio 2010]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=386</guid>
		<description><![CDATA[Here's a tip of how to run NUnit tests in .Net 4 code:
Running NUnit 2.5 against Visual Studio 2010 .Net 4 code
Note that it's the NUnit.exe.config file in C:\Program Files\NUnit 2.5.2\bin\net-2.0 that should be updated.
]]></description>
			<content:encoded><![CDATA[<p>Here's a tip of how to run NUnit tests in .Net 4 code:</p>
<p><a href="http://vincenthomedev.wordpress.com/2009/06/18/running-nunit-2-5-against-visual-studio-2010-net-4-code/">Running NUnit 2.5 against Visual Studio 2010 .Net 4 code</a></p>
<p>Note that it's the <strong>NUnit.exe.config</strong> file in <strong>C:\Program Files\NUnit 2.5.2\bin\net-2.0</strong> that should be updated.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=386</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Twittering&#8230;</title>
		<link>http://www.meadow.se/wordpress/?p=384</link>
		<comments>http://www.meadow.se/wordpress/?p=384#comments</comments>
		<pubDate>Fri, 13 Nov 2009 22:04:50 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=384</guid>
		<description><![CDATA[I'm now on Twitter...
http://twitter.com/emilast
Wonder if it's gonna be useful or a time-stealer...
/Emil
]]></description>
			<content:encoded><![CDATA[<p>I'm now on Twitter...</p>
<p><a href="http://twitter.com/emilast">http://twitter.com/emilast</a></p>
<p>Wonder if it's gonna be useful or a time-stealer...</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=384</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Distributed transactions with WCF and NHibernate</title>
		<link>http://www.meadow.se/wordpress/?p=363</link>
		<comments>http://www.meadow.se/wordpress/?p=363#comments</comments>
		<pubDate>Fri, 13 Nov 2009 21:54:17 +0000</pubDate>
		<dc:creator>Emil Åström</dc:creator>
				<category><![CDATA[.Net programming]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://www.meadow.se/wordpress/?p=363</guid>
		<description><![CDATA[I recently started working on a new project in which we wanted to use WCF services that utilized NHibernate for database work. We also wanted those services to support distributed transactions so that several calls to one or more service would be done within the same client transaction. This is possible thanks to functionality in [...]]]></description>
			<content:encoded><![CDATA[<p>I recently started working on a new project in which we wanted to use WCF services that utilized NHibernate for database work. We also wanted those services to support distributed transactions so that several calls to one or more service would be done within the same client transaction. This is possible thanks to functionality in the <strong>System.Transactions</strong> namespace and in <strong>WCF which supports transaction flowing</strong> and of course the <strong>Distributed Transaction Coordinator</strong> in the operating system (see <a href="http://msdn.microsoft.com/en-us/library/ms684146(VS.85).aspx">MSDN</a> for more info on the DTC).</p>
<p><strong>Note</strong>: The code below has been tested on NHibernate 2.1.1, Windows XP and .Net 4 Beta 2. Older versions of the .Net Framework should also work, but not necessarily older versions of NHibernate. I believe distributed transaction support was introduced in 2.1.0, but it may or may not work in similar ways to what is described here in older versions since the ADO.Net supports the System.Transactions namespace.</p>
<p>The goal is to write code like this on the WCF client side:</p>
<div class="syntax_hilite">
<div id="csharp-24">
<div class="csharp"><span style="color: #008080; font-style: italic;">// TransactionScope is from the System.Transactions namespace</span><br />
<span style="color: #0600FF;">using</span> <span style="color: #000000;">&#40;</span>TransactionScope tx = <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> TransactionScope<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp; service1.<span style="color: #0000FF;">MyMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; service2.<span style="color: #0000FF;">MyMethod</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
&nbsp; &nbsp; tx.<span style="color: #0000FF;">Complete</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>;<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p></p>
<p>If all goes well, the results of both service calls are comitted to the database. If the call to service2 fails and we get an exception so that tx.Complete() is never executed, then all database updates are rolled back are rolled back and nothing is persisted, even if service1 is hosted in another process or on another machine.</p>
<p>Note also that we're not limited to database updates, any resource that supports transactions and knows about System.Transactions will be able to roll back updates.</p>
<p>For the above to work, we have to do several things:</p>
<ul>
<li><strong>Configure the service binding to support transaction flow</strong> (on both the client and service side). Example:
<div class="syntax_hilite">
<div id="code-25">
<div class="code">&lt;system.<span style="">serviceModel</span>&gt;<br />
&nbsp; &lt;services&gt;<br />
&nbsp; &nbsp; &lt;service ...&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;endpoint ... <span style="">bindingConfiguration</span>=<span style="color:#CC0000;">"TransactionalWsHttp"</span>&gt;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;...<br />
&nbsp; &nbsp; &nbsp; &lt;/endpoint&gt;<br />
&nbsp; &nbsp; &nbsp; ...<br />
&nbsp; &nbsp; &lt;/service&gt;<br />
&nbsp; &lt;/services&gt;<br />
&nbsp; &lt;bindings&gt;<br />
&nbsp; &nbsp; &lt;wshttpbinding&gt;<br />
&nbsp; &nbsp; &nbsp; &lt;binding name=<span style="color:#CC0000;">"TransactionalWsHttp"</span> transactionFlow=<span style="color:#CC0000;">"true"</span> /&gt;<br />
&nbsp; &nbsp; &lt;/wshttpbinding&gt;<br />
&nbsp; &lt;/bindings&gt;<br />
&lt;/system.<span style="">serviceModel</span>&gt;</div>
</div>
</div>
<p>
</li>
<li>Mark the methods that should be enlisted in the distributed transaction with the <strong>TransactionFlow attribute</strong>. Do this on the service contract interface:
<div class="syntax_hilite">
<div id="csharp-26">
<div class="csharp"><span style="color: #000000;">&#91;</span>TransactionFlow<span style="color: #000000;">&#40;</span>TransactionFlowOption.<span style="color: #0000FF;">Allowed</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">void</span> SaveMyObject<span style="color: #000000;">&#40;</span>Foobar <span style="color: #FF0000;">object</span><span style="color: #000000;">&#41;</span>;</div>
</div>
</div>
<p>
That setting dictates that the client's transaction will be used, if there is one. If there isn't, a new one will be created automatically for the method call and it will be auto-committed when the method returns.</p>
<p>You can also use <strong>TransactionFlowOption.Mandatory</strong> to require the client to supply the transaction. If it doesn't then WCF will throw an exception.
</li>
<li>Mark the method implementation with the <strong>OperationBehavior attribute </strong>like this:
<div class="syntax_hilite">
<div id="csharp-27">
<div class="csharp"><span style="color: #000000;">&#91;</span>OperationBehavior<span style="color: #000000;">&#40;</span>TransactionScopeRequired=<span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#93;</span><br />
<span style="color: #0600FF;">public</span> <span style="color: #0600FF;">void</span> SaveMyObject<span style="color: #000000;">&#40;</span>Foobar <span style="color: #FF0000;">object</span><span style="color: #000000;">&#41;</span><br />
<span style="color: #000000;">&#123;</span><br />
&nbsp; &nbsp;...<br />
<span style="color: #000000;">&#125;</span></div>
</div>
</div>
<p>
This will supply the method with the transaction (the other settings are not enough, they simply indicate that WCF should be configured to be prepared supply the transaction and this setting says that the method really wants it).
</li>
</ul>
<p>This is actually all that is required! NHibernate will now detect if there is a so called ambient transaction (to do this yourself, look at the <strong>System.Transactions.Transaction.Current</strong> static property, if it's non-null there there is a transaction) and will enlist its session in it. When the transaction completes, then the saved data will be comitted to the database. If there is an exception so that transaction is never completed then all data will be rolled back.</p>
<p><strong>Important notes:</strong>
<ul>
<li><strong>Don't close the NHibernate ISession explicitly</strong> when using the above pattern as NHibernate will do that itself when the distributed transaction completes or is rolled back. If you do, then you will get an exception later when the transaction tries to do it.</li>
<li><strong>Don't create transactions explicitly</strong>, let System.Transactions do that for you.</li>
<li>If you get this error:
<div class="syntax_hilite">
<div id="code-28">
<div class="code">Network access for Distributed Transaction Manager <span style="color:#006600; font-weight:bold;">&#40;</span>MSDTC<span style="color:#006600; font-weight:bold;">&#41;</span> has been disabled. <span style="">Please</span> enable DTC for<br />
network access in the security configuration for MSDTC using the Component Services Administrative tool.</div>
</div>
</div>
<p>
then you have to do as it says and enable network access in the DTC on the server where the WCF service is hosted (and also on the database server I would assume, but I haven't actually checked). Here's an instruction on how to do that:<br />
<a href="http://hspinfo.wordpress.com/2009/03/24/network-access-for-distributed-transaction-manager-msdtc-has-been-disabled/">Network access for Distributed Transaction Manager (MSDTC) has been disabled</a>
</li>
</ul>
<p>I think this is really cool stuff. Not only does it simplify transaction management in NHibernate, it also allows us to write much more robust distributed service-oriented application with very little effort. You also get support in the operating system, for example för statistics:</p>
<p><a href="http://www.meadow.se/wordpress/wp-content/uploads/2009/11/dtc_stats.JPG"><img src="http://www.meadow.se/wordpress/wp-content/uploads/2009/11/dtc_stats-300x201.jpg" alt="DTC statistics" title="DTC statistics" width="300" height="201" class="alignnone size-medium wp-image-382" /></a></p>
<p>I haven't tried with other databases than SQL Server but as NHibernate seems to support System.Transactions it is possible that it works with other DB systems as well. If you have any experience with that, please leave a comment <img src='http://www.meadow.se/wordpress/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>I will continue to update this post if I do more findings on this subject. When I google about this there wasn't much information on this subject so hopefully this post will help others with the same needs.</p>
<p>/Emil</p>
]]></content:encoded>
			<wfw:commentRss>http://www.meadow.se/wordpress/?feed=rss2&amp;p=363</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>
