<?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>.NETified</title>
	<atom:link href="http://joshclose.net/?feed=rss2" rel="self" type="application/rss+xml" />
	<link>http://joshclose.net</link>
	<description>You have been .NETified!</description>
	<lastBuildDate>Tue, 16 Feb 2010 02:52:31 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Getting Arguments from a Running Process</title>
		<link>http://joshclose.net/?p=268</link>
		<comments>http://joshclose.net/?p=268#comments</comments>
		<pubDate>Thu, 15 Oct 2009 18:42:06 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Command]]></category>
		<category><![CDATA[Processes]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=268</guid>
		<description><![CDATA[If you get all the running processes using .NET, the Process.StartInfo.Arguments will be empty.
var processes = Process.GetProcessesByName( &#34;MyProcess&#34; );

This seems a little odd, but whatever, let’s find a workaround. Here is some code to get the arguments of a process.
var managementObjectSearcher = new ManagementObjectSearcher( &#34;select CommandLine from Win32_Process where Name='MyProcess.exe'&#34; );
var managementObjects = managementObjectSearcher.Get();
foreach( var [...]]]></description>
			<content:encoded><![CDATA[<p>If you get all the running processes using .NET, the Process.StartInfo.Arguments will be empty.</p>
<pre class="code"><span style="color: blue">var </span>processes = <span style="color: #2b91af">Process</span>.GetProcessesByName( <span style="color: #a31515">&quot;MyProcess&quot; </span>);</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This seems a little odd, but whatever, let’s find a workaround. Here is some code to get the arguments of a process.</p>
<pre class="code"><span style="color: blue">var </span>managementObjectSearcher = <span style="color: blue">new </span><span style="color: #2b91af">ManagementObjectSearcher</span>( <span style="color: #a31515">&quot;select CommandLine from Win32_Process where Name='MyProcess.exe'&quot; </span>);
<span style="color: blue">var </span>managementObjects = managementObjectSearcher.Get();
<span style="color: blue">foreach</span>( <span style="color: blue">var </span>managementObject <span style="color: blue">in </span>managementObjects )
{
    <span style="color: blue">var </span>command = (<span style="color: blue">string</span>)managementObject[<span style="color: #a31515">&quot;CommandLine&quot;</span>];
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Command is the full command line used to start the process. This includes the path and all arguments.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=268</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Setting Up WCF with Multiple Services and Multiple Databases Using NHibernate and Ninject</title>
		<link>http://joshclose.net/?p=264</link>
		<comments>http://joshclose.net/?p=264#comments</comments>
		<pubDate>Thu, 15 Oct 2009 01:02:55 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[NHibernate]]></category>
		<category><![CDATA[Ninject]]></category>
		<category><![CDATA[WCF]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=264</guid>
		<description><![CDATA[This is a tutorial on how to setup WCF using NHibernate and Ninject with multiple databases and services. I’ve seen many examples of how to setup WCF with NHibernate, or Ninject, but not with both. None of the examples I’ve seen have used multiple services or multiples databases either. I had to do this, so [...]]]></description>
			<content:encoded><![CDATA[<p>This is a tutorial on how to setup WCF using NHibernate and Ninject with multiple databases and services. I’ve seen many examples of how to setup WCF with NHibernate, or Ninject, but not with both. None of the examples I’ve seen have used multiple services or multiples databases either. I had to do this, so I thought I’d share. We’ll setup this code as if we’re creating some sort of blog application.</p>
<p>First, create an assembly for each of the following: business logic, services, service contracts, and the service host.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb.png" border="0" alt="image" /></a></p>
<p>We want to separate all these into different assemblies so we don’t have issues with circular dependencies. The other option is to have everything in a single assembly, but that usually isn’t an option, or a very good one for that matter.</p>
<p><strong>Contracts</strong></p>
<p>The contract assembly is where we keep all the contracts, or interfaces, that are used in our services. This makes it so that the services can reference and use the contracts, and so can a client to the services. If the service is visible to projects outside of yours, you can just give them the contract assembly.</p>
<p>Let’s create a service contract, and some data contracts for our blog application. I put them into folders to separate the service contracts from the data contracts.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image1.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb1.png" border="0" alt="image" /></a></p>
<p>IBlogService.cs</p>
<p><a href="http://11011.net/software/vspaste"></a><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.ServiceModel;
<span style="color: blue">using </span>Wnn.Service.Contract.DataContracts;

<span style="color: blue">namespace </span>Wnn.Service.Contract.ServiceContracts
{
    [<span style="color: #2b91af">ServiceContract</span>]
    <span style="color: blue">public interface </span><span style="color: #2b91af">IBlogService
    </span>{
        [<span style="color: #2b91af">OperationContract</span>]
        <span style="color: #2b91af">Post </span>GetPostById( <span style="color: blue">int </span>id );
        [<span style="color: #2b91af">OperationContract</span>]
        <span style="color: #2b91af">User </span>GetUserById( <span style="color: blue">int </span>id );
        [<span style="color: #2b91af">OperationContract</span>]
        <span style="color: #2b91af">User </span>GetUserByUserNameAndPassword( <span style="color: blue">string </span>userName, <span style="color: blue">string </span>password );
        [<span style="color: #2b91af">OperationContract</span>]
        <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Post</span>&gt; GetPosts();
        [<span style="color: #2b91af">OperationContract</span>]
        <span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Post</span>&gt; GetPostsFromAuthor( <span style="color: blue">string </span>userName );
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Post.cs</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Runtime.Serialization;

<span style="color: blue">namespace </span>Wnn.Service.Contract.DataContracts
{
    [<span style="color: #2b91af">DataContract</span>]
    <span style="color: blue">public class </span><span style="color: #2b91af">Post
    </span>{
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>Content { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public </span><span style="color: #2b91af">DateTime </span>Created { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>Author { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;<span style="color: blue">string</span>&gt; Tags { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>User.cs</p>
<pre class="code"><span style="color: blue">using </span>System.Runtime.Serialization;

<span style="color: blue">namespace </span>Wnn.Service.Contract.DataContracts
{
    [<span style="color: #2b91af">DataContract</span>]
    <span style="color: blue">public class </span><span style="color: #2b91af">User
    </span>{
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>UserName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>Email { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        [<span style="color: #2b91af">DataMember</span>]
        <span style="color: blue">public string </span>LastName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Business Logic</strong></p>
<p>Let’s create our business objects, repositories to access our database, and all the NHibernate guts. We don’t have a data layer here because NHibernate IS our data layer. There could still be a use of separating some stuff out into a data layer, but I will not be doing that. References will need to be added to NHibernate.dll, FluentNHibernate.dll, NHibernate.ByteCode.LinFu.dll, and NHibernate.Linq.dll.</p>
<p>Let’s create our POCO (plain old CLR object) business objects. All members need to be virtual so NHibernate can use them.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image2.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb2.png" border="0" alt="image" /></a></p>
<p>User.cs</p>
<p>In the password property we want to encrypt any password that gets set.</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Security.Cryptography;
<span style="color: blue">using </span>System.Text;

<span style="color: blue">namespace </span>Wnn.Business.Objects
{
    <span style="color: blue">public class </span><span style="color: #2b91af">User
    </span>{
        <span style="color: blue">private string </span>password;

        <span style="color: blue">public virtual int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">protected set</span>; }
        <span style="color: blue">public virtual string </span>UserName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual string </span>Email { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual string </span>Password
        {
            <span style="color: blue">get </span>{ <span style="color: blue">return </span>password; }
            <span style="color: blue">set
            </span>{
                <span style="color: blue">var </span>md5 = <span style="color: #2b91af">MD5</span>.Create();
                <span style="color: blue">var </span>passwordBytes = <span style="color: #2b91af">Encoding</span>.UTF8.GetBytes( <span style="color: blue">value </span>);
                md5.ComputeHash( passwordBytes );
                password = <span style="color: #2b91af">Convert</span>.ToBase64String( md5.Hash );
            }
        }

        <span style="color: blue">public virtual string </span>FirstName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual string </span>LastName { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    }
}</pre>
<p>Tag.cs</p>
<pre class="code"><span style="color: blue">namespace </span>Wnn.Business.Objects
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Tag
    </span>{
        <span style="color: blue">public virtual int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">protected set</span>; }
        <span style="color: blue">public virtual string </span>Name { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual </span><span style="color: #2b91af">Post </span>Post { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
    }
}</pre>
<p>Post.cs</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.Generic;

<span style="color: blue">namespace </span>Wnn.Business.Objects
{
    <span style="color: blue">public class </span><span style="color: #2b91af">Post
    </span>{
        <span style="color: blue">public virtual int </span>Id { <span style="color: blue">get</span>; <span style="color: blue">protected set</span>; }
        <span style="color: blue">public virtual string </span>Title { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual string </span>Content { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual </span><span style="color: #2b91af">DateTime </span>Created { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual </span><span style="color: #2b91af">User </span>Author { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }
        <span style="color: blue">public virtual </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Tag</span>&gt; Tags { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue">public virtual void </span>AddTag( <span style="color: #2b91af">Tag </span>tag )
        {
            tag.Post = <span style="color: blue">this</span>;
            Tags.Add( tag );
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The AddTag method is used to set both sides of the object association. Otherwise NHibernate will not save them properly if a tag is added to the tags list.</p>
<p>Let’s create our NHibernate mapping files now. I use FluentNHibernate to do this.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image3.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb3.png" border="0" alt="image" /></a></p>
<p>UserMap.cs</p>
<p>Since we’re using a backing field for the Password property, we need to have NHibernate use that instead. We don’t want NHibernate to use the property because the stored hash would get hashed again when set.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">using </span>FluentNHibernate.Mapping;
<span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Mappings
{
    <span style="color: blue">public class </span><span style="color: #2b91af">UserMap </span>: <span style="color: #2b91af">ClassMap</span>&lt;<span style="color: #2b91af">User</span>&gt;
    {
        <span style="color: blue">public </span>UserMap()
        {
            SetupMapping();
        }

        <span style="color: blue">private void </span>SetupMapping()
        {
            Table( <span style="color: #a31515">"Users" </span>);
            Id( m =&gt; m.Id );
            Map( m =&gt; m.Email );
            Map( m =&gt; m.FirstName );
            Map( m =&gt; m.LastName );
            Map( m =&gt; m.Password ).Access.CamelCaseField();
            Map( m =&gt; m.UserName );
        }
    }
}</pre>
<p>TagMap.cs</p>
<pre class="code"><span style="color: blue">using </span>FluentNHibernate.Mapping;
<span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Mappings
{
    <span style="color: blue">public class </span><span style="color: #2b91af">TagMap </span>: <span style="color: #2b91af">ClassMap</span>&lt;<span style="color: #2b91af">Tag</span>&gt;
    {
        <span style="color: blue">public </span>TagMap()
        {
            SetupMapping();
        }

        <span style="color: blue">private void </span>SetupMapping()
        {
            Table( <span style="color: #a31515">"Tags" </span>);
            Id( m =&gt; m.Id );
            Map( m =&gt; m.Name );
            References( m =&gt; m.Post, <span style="color: #a31515">"PostId" </span>);
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>PostMap.cs</p>
<pre class="code"><span style="color: blue">using </span>FluentNHibernate.Mapping;
<span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Mappings
{
    <span style="color: blue">public class </span><span style="color: #2b91af">PostMap </span>: <span style="color: #2b91af">ClassMap</span>&lt;<span style="color: #2b91af">Post</span>&gt;
    {
        <span style="color: blue">public </span>PostMap()
        {
            SetupMapping();
        }

        <span style="color: blue">private void </span>SetupMapping()
        {
            Table( <span style="color: #a31515">"Posts" </span>);
            Id( m =&gt; m.Id );
            Map( m =&gt; m.Content );
            Map( m =&gt; m.Title );
            References( m =&gt; m.Author, <span style="color: #a31515">"UserId" </span>);
            HasMany( m =&gt; m.Tags )
                .Inverse()
                .Cascade.AllDeleteOrphan()
                .KeyColumns.Add( <span style="color: #a31515">"PostId" </span>)
                .Fetch.Subselect();
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Next we’re going to setup our repositories. This is where we access our data. Before we can do that though, we need to create a SessionManager object. SessionManager is just a wrapper around the NHibernate ISession object to allow for easier unit testing. We’re not hiding the fact that we’re using NHibernate though. The SessionManager wrapper still returns NHibernate objects. This could be completely abstracted so it didn’t matter which ORM or data layer is being used.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image4.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb4.png" border="0" alt="image" /></a></p>
<p>SessionManager.cs</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Data;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>NHibernate;
<span style="color: blue">using </span>NHibernate.Linq;

<span style="color: blue">namespace </span>Wnn.Business
{
    <span style="color: blue">public class </span><span style="color: #2b91af">SessionManager
    </span>{
        <span style="color: blue">private readonly </span><span style="color: #2b91af">ISession </span>session;

        <span style="color: blue">public </span>SessionManager( <span style="color: #2b91af">ISession </span>session )
        {
            <span style="color: blue">if</span>( session == <span style="color: blue">null </span>)
            {
                <span style="color: blue">throw new </span><span style="color: #2b91af">ArgumentNullException</span>( <span style="color: #a31515">"session" </span>);
            }

            <span style="color: blue">this</span>.session = session;
        }

        <span style="color: blue">public virtual </span><span style="color: #2b91af">IQueryable</span>&lt;T&gt; Linq&lt;T&gt;()
        {
            <span style="color: blue">return </span>session.Linq&lt;T&gt;();
        }

        <span style="color: blue">public virtual </span>T Get&lt;T&gt;( <span style="color: blue">int </span>id )
        {
            <span style="color: blue">return </span>session.Get&lt;T&gt;( id );
        }

        <span style="color: blue">public virtual </span><span style="color: #2b91af">ISQLQuery </span>CreateSqlQuery( <span style="color: blue">string </span>queryString )
        {
            <span style="color: blue">return </span>session.CreateSQLQuery( queryString );
        }

        <span style="color: blue">public virtual void </span>SaveOrUpdate( <span style="color: blue">object </span>obj )
        {
            session.SaveOrUpdate( obj );
        }

        <span style="color: blue">public virtual void </span>Delete( <span style="color: blue">object </span>obj )
        {
            session.Delete( obj );
        }

        <span style="color: blue">public virtual </span><span style="color: #2b91af">ITransaction </span>BeginTransaction()
        {
            <span style="color: blue">return </span>session.BeginTransaction();
        }

        <span style="color: blue">public virtual </span><span style="color: #2b91af">IDbConnection </span>Close()
        {
            <span style="color: blue">return </span>session.Close();
        }

        <span style="color: blue">public virtual void </span>Dispose()
        {
            session.Dispose();
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we can create the repositories and a base repository class for common methods.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image5.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb5.png" border="0" alt="image" /></a></p>
<p>RepositoryBase.cs</p>
<p>We put any common methods in the base class.</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;

<span style="color: blue">namespace </span>Wnn.Business.Repositories
{
    <span style="color: blue">public abstract class </span><span style="color: #2b91af">RepositoryBase</span>&lt;T&gt;
    {
        <span style="color: blue">protected </span><span style="color: #2b91af">SessionManager </span>SessionManager { <span style="color: blue">get</span>; <span style="color: blue">set</span>; }

        <span style="color: blue">protected </span>RepositoryBase( <span style="color: #2b91af">SessionManager </span>sessionManager )
        {
            SessionManager = sessionManager;
        }

        <span style="color: blue">public </span>T Get( <span style="color: blue">int </span>id )
        {
            <span style="color: blue">return </span>SessionManager.Get&lt;T&gt;( id );
        }

        <span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;T&gt; GetAll()
        {
            <span style="color: blue">return </span>SessionManager.Linq&lt;T&gt;().ToList();
        }

        <span style="color: blue">public void </span>SaveOrUpdate( T obj )
        {
            SessionManager.SaveOrUpdate( obj );
        }

        <span style="color: blue">public void </span>Delete( T obj )
        {
            SessionManager.Delete( obj );
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>UserRepository.cs</p>
<pre class="code"><span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Repositories
{
    <span style="color: blue">public class </span><span style="color: #2b91af">UserRepository </span>: <span style="color: #2b91af">RepositoryBase</span>&lt;<span style="color: #2b91af">User</span>&gt;
    {
        <span style="color: blue">public </span>UserRepository( <span style="color: #2b91af">SessionManager </span>sessionManager ) : <span style="color: blue">base</span>( sessionManager ){}

        <span style="color: blue">public </span><span style="color: #2b91af">User </span>GetByUserNameAndPassword( <span style="color: blue">string </span>userName, <span style="color: blue">string </span>password )
        {
            <span style="color: blue">return </span>( <span style="color: blue">from </span>u <span style="color: blue">in </span>SessionManager.Linq&lt;<span style="color: #2b91af">User</span>&gt;()
                     <span style="color: blue">where </span>u.UserName == userName &amp;&amp; password == u.Password
                     <span style="color: blue">select </span>u ).SingleOrDefault();
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>TagRepository.cs</p>
<pre class="code"><span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Repositories
{
    <span style="color: blue">public class </span><span style="color: #2b91af">TagRepository </span>: <span style="color: #2b91af">RepositoryBase</span>&lt;<span style="color: #2b91af">Tag</span>&gt;
    {
        <span style="color: blue">public </span>TagRepository( <span style="color: #2b91af">SessionManager </span>sessionManager ) : <span style="color: blue">base</span>( sessionManager ){}
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>PostRepository.cs</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>Wnn.Business.Objects;

<span style="color: blue">namespace </span>Wnn.Business.Repositories
{
    <span style="color: blue">public class </span><span style="color: #2b91af">PostRepository </span>: <span style="color: #2b91af">RepositoryBase</span>&lt;<span style="color: #2b91af">Post</span>&gt;
    {
        <span style="color: blue">public </span>PostRepository( <span style="color: #2b91af">SessionManager </span>sessionManager ) : <span style="color: blue">base</span>( sessionManager ){}

        <span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Post</span>&gt; GetAllForUser( <span style="color: blue">string </span>userName )
        {
            <span style="color: blue">return </span>( <span style="color: blue">from </span>p <span style="color: blue">in </span>SessionManager.Linq&lt;<span style="color: #2b91af">Post</span>&gt;()
                     <span style="color: blue">where </span>p.Author.UserName == userName
                     <span style="color: blue">select </span>p ).ToList();
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Services</strong></p>
<p>Now that we have our repositories setup, we can implement the services that will use the repositories.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image6.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb6.png" border="0" alt="image" /></a></p>
<p>BlogService.cs</p>
<p>We need to set the InstanceContextMode to PerCall. This makes it so WCF will create a new context instance per service call. This sets up NHibernate nicely because we can then start a transaction at the beginning of every service method call, and end the transaction at the end of the call, rolling back if an error occurred. More on that later.</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.Linq;
<span style="color: blue">using </span>System.ServiceModel;
<span style="color: blue">using </span>Wnn.Business.Repositories;
<span style="color: blue">using </span>Wnn.Service.Contract.DataContracts;
<span style="color: blue">using </span>Wnn.Service.Contract.ServiceContracts;

<span style="color: blue">namespace </span>Wnn.Service
{
    [<span style="color: #2b91af">ServiceBehavior</span>( InstanceContextMode = <span style="color: #2b91af">InstanceContextMode</span>.PerCall )]
    <span style="color: blue">public class </span><span style="color: #2b91af">BlogService </span>: <span style="color: #2b91af">IBlogService
    </span>{
        <span style="color: blue">private readonly </span><span style="color: #2b91af">UserRepository </span>userRepository;
        <span style="color: blue">private readonly </span><span style="color: #2b91af">PostRepository </span>postRepository;
        <span style="color: blue">private readonly </span><span style="color: #2b91af">TagRepository </span>tagRepository;

        <span style="color: blue">public </span>BlogService( <span style="color: #2b91af">UserRepository </span>userRepository, <span style="color: #2b91af">PostRepository </span>postRepository, <span style="color: #2b91af">TagRepository </span>tagRepository )
        {
            <span style="color: blue">this</span>.userRepository = userRepository;
            <span style="color: blue">this</span>.postRepository = postRepository;
            <span style="color: blue">this</span>.tagRepository = tagRepository;
        }

        <span style="color: blue">public </span><span style="color: #2b91af">Post </span>GetPostById( <span style="color: blue">int </span>id )
        {
            <span style="color: blue">return </span>ConvertPost( postRepository.Get( id ) );
        }

        <span style="color: blue">public </span><span style="color: #2b91af">User </span>GetUserById( <span style="color: blue">int </span>id )
        {
            <span style="color: blue">return </span>ConvertUser( userRepository.Get( id ) );
        }

        <span style="color: blue">public </span><span style="color: #2b91af">User </span>GetUserByUserNameAndPassword( <span style="color: blue">string </span>userName, <span style="color: blue">string </span>password )
        {
            <span style="color: blue">return </span>ConvertUser( userRepository.GetByUserNameAndPassword( userName, password ) );
        }

        <span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Post</span>&gt; GetPosts()
        {
            <span style="color: blue">return </span>( <span style="color: blue">from </span>p <span style="color: blue">in </span>postRepository.GetAll()
                     <span style="color: blue">select </span>ConvertPost( p ) ).ToList();
        }

        <span style="color: blue">public </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">Post</span>&gt; GetPostsFromAuthor( <span style="color: blue">string </span>userName )
        {
            <span style="color: blue">return </span>( <span style="color: blue">from </span>p <span style="color: blue">in </span>postRepository.GetAllForUser( userName )
                     <span style="color: blue">select </span>ConvertPost( p ) ).ToList();
        }

        <span style="color: blue">private static </span><span style="color: #2b91af">User </span>ConvertUser( Business.Objects.<span style="color: #2b91af">User </span>userData )
        {
            <span style="color: #2b91af">User </span>user = <span style="color: blue">null</span>;
            <span style="color: blue">if</span>( userData != <span style="color: blue">null </span>)
            {
                user = <span style="color: blue">new </span><span style="color: #2b91af">User
                </span>{
                    Email = userData.Email,
                    FirstName = userData.FirstName,
                    Id = userData.Id,
                    LastName = userData.LastName,
                    UserName = userData.UserName,
                };
            }
            <span style="color: blue">return </span>user;
        }

        <span style="color: blue">private static </span><span style="color: #2b91af">Post </span>ConvertPost( Business.Objects.<span style="color: #2b91af">Post </span>postData )
        {
            <span style="color: #2b91af">Post </span>post = <span style="color: blue">null</span>;
            <span style="color: blue">if</span>( postData != <span style="color: blue">null </span>)
            {
                post = <span style="color: blue">new </span><span style="color: #2b91af">Post
                </span>{
                    Author = postData.Author.UserName,
                    Content = postData.Content,
                    Created = postData.Created,
                    Id = postData.Id,
                    Title = postData.Title,
                    Tags = ( <span style="color: blue">from </span>t <span style="color: blue">in </span>postData.Tags
                             <span style="color: blue">select </span>t.Name ).ToList()
                };
            }
            <span style="color: blue">return </span>post;
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><strong>Service Host</strong></p>
<p>The service host is what actually starts up all of our services. In this case, just one service, but this is being created so we can have as many as we want.</p>
<p><a href="http://joshclose.net/wp-content/uploads/2009/10/image7.png"><img style="border-bottom: 0pt; border-left: 0pt; display: inline; border-top: 0pt; border-right: 0pt" title="image" src="http://joshclose.net/wp-content/uploads/2009/10/image_thumb7.png" border="0" alt="image" /></a></p>
<p>ServiceHostEngine.cs</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>System.ServiceModel;

<span style="color: blue">namespace </span>Wnn.Service.Host
{
    <span style="color: blue">public class </span><span style="color: #2b91af">ServiceHostEngine
    </span>{
        <span style="color: blue">private readonly </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">ServiceHost</span>&gt; serviceHosts = <span style="color: blue">new </span><span style="color: #2b91af">List</span>&lt;<span style="color: #2b91af">ServiceHost</span>&gt;();

        <span style="color: blue">public </span>ServiceHostEngine()
        {
            <span style="color: green">// We can have multiple services listed here.
            // We could actually have a list of services in a config
            // file that is dynamically loaded here also.
            </span>serviceHosts.Add( <span style="color: blue">new </span><span style="color: #2b91af">ServiceHost</span>( <span style="color: blue">typeof</span>( <span style="color: #2b91af">BlogService </span>) ) );
        }

        <span style="color: blue">public void </span>Start()
        {
            <span style="color: blue">foreach</span>( <span style="color: blue">var </span>serviceHost <span style="color: blue">in </span>serviceHosts )
            {
                serviceHost.Open();
            }
        }

        <span style="color: blue">public void </span>Stop()
        {
            <span style="color: blue">foreach</span>( <span style="color: blue">var </span>serviceHost <span style="color: blue">in </span>serviceHosts )
            {
                serviceHost.Close();
            }
        }
    }
}</pre>
<p>We are now to a part where Ninject is used to create the NHibernate session instances. We need to create a factory for creating NHibernate SessionFactories. We need a factory for every database that is being used. So, we need a SessionFactoryFactory. This will be back in our business layer.</p>
<p>SessionFactoryFactory.cs</p>
<pre class="code"><span style="color: blue">using </span>System.Collections.Generic;
<span style="color: blue">using </span>FluentNHibernate.Cfg;
<span style="color: blue">using </span>NHibernate;

<span style="color: blue">namespace </span>Wnn.Business
{
    <span style="color: blue">public static class </span><span style="color: #2b91af">SessionFactoryFactory
    </span>{
        <span style="color: blue">private static readonly object </span>locker = <span style="color: blue">new object</span>();
        <span style="color: blue">private static readonly </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ISessionFactory</span>&gt; sessionFactories = <span style="color: blue">new </span><span style="color: #2b91af">Dictionary</span>&lt;<span style="color: blue">string</span>, <span style="color: #2b91af">ISessionFactory</span>&gt;();

        <span style="color: blue">public static </span><span style="color: #2b91af">ISessionFactory </span>GetSessionFactory( <span style="color: blue">string </span>connectionStringKey )
        {
            <span style="color: #2b91af">ISessionFactory </span>sessionFactory;
            sessionFactories.TryGetValue( connectionStringKey, <span style="color: blue">out </span>sessionFactory );

            <span style="color: green">// Double check locking is used here because
            // WCF could have many instances accessing this
            // code at the same time.
            </span><span style="color: blue">if</span>( sessionFactory == <span style="color: blue">null </span>)
            {
                <span style="color: blue">lock</span>( locker )
                {
                    sessionFactories.TryGetValue( connectionStringKey, <span style="color: blue">out </span>sessionFactory );
                    <span style="color: blue">if</span>( sessionFactory == <span style="color: blue">null </span>)
                    {
                        sessionFactory = <span style="color: #2b91af">Fluently</span>.Configure()
                            .Database(
                            FluentNHibernate.Cfg.Db.<span style="color: #2b91af">MsSqlConfiguration</span>.MsSql2005.ConnectionString(
                                c =&gt; c.FromConnectionStringWithKey( connectionStringKey ) )
                                .ProxyFactoryFactory( <span style="color: #a31515">"NHibernate.ByteCode.LinFu.ProxyFactoryFactory, NHibernate.ByteCode.LinFu" </span>) )
                            .Mappings( m =&gt; m.FluentMappings.AddFromAssembly( <span style="color: blue">typeof</span>( <span style="color: #2b91af">SessionFactoryFactory </span>).Assembly ) )
                            .ExposeConfiguration( cfg =&gt; cfg.SetProperty( <span style="color: #a31515">"generate_statistics"</span>, <span style="color: #a31515">"true" </span>) )
                            .ExposeConfiguration( cfg =&gt; cfg.SetProperty( <span style="color: #a31515">"adonet.batch_size"</span>, <span style="color: #a31515">"10" </span>) )
                            .BuildSessionFactory();

                        sessionFactories[connectionStringKey] = sessionFactory;
                    }
                }
            }

            <span style="color: blue">return </span>sessionFactory;
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now that we are able to create NHibernate sessions, let’s setup Ninject to create the ISession an SessionManager objects for us. We need to pass the connection string in to our Ninject setup so the SessionFactoryFactory can use the correct database connection. We will be creating a new Ninject kernel each WCF call, passing in the connection string.</p>
<pre class="code"><span style="color: blue">using </span>NHibernate;
<span style="color: blue">using </span>Ninject.Modules;
<span style="color: blue">using </span>Wnn.Business;

<span style="color: blue">namespace </span>Wnn.Service.Host
{
    <span style="color: blue">public class </span><span style="color: #2b91af">NinjectSetup </span>: <span style="color: #2b91af">NinjectModule
    </span>{
        <span style="color: blue">private readonly string </span>connectionStringKey;

        <span style="color: blue">public </span>NinjectSetup( <span style="color: blue">string </span>connectionStringKey )
        {
            <span style="color: blue">this</span>.connectionStringKey = connectionStringKey;
        }

        <span style="color: blue">public override void </span>Load()
        {
            Bind&lt;<span style="color: #2b91af">ISession</span>&gt;()
                .ToMethod( ctx =&gt; <span style="color: #2b91af">SessionFactoryFactory</span>.GetSessionFactory( connectionStringKey ).OpenSession() )
                .InSingletonScope();

            Bind&lt;<span style="color: #2b91af">SessionManager</span>&gt;()
                .ToSelf()
                .InSingletonScope();
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we can create our instance provider. The provider is what creates the instances that WCF uses. This gets called for every service method call because we used InstanceContextMode.PerCall for our services.</p>
<p>In the GetInstance method (which again happens per call), we create a Ninject kernel that we can use to create our objects for us. A SessionManager is created and a transaction is started. A transaction is meant to be used for a unit of work. Our unit of work here is a single service method call. A new service instance is then created by Ninject. This means that all our repositories in the constructor of the services will be injected for us.</p>
<p>The ReleaseInstance method is then called when the request is complete. In here we commit our transaction. If there are any errors, they are rolled back. The session is then closed.</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Runtime.Remoting.Messaging;
<span style="color: blue">using </span>System.ServiceModel;
<span style="color: blue">using </span>System.ServiceModel.Channels;
<span style="color: blue">using </span>System.ServiceModel.Dispatcher;
<span style="color: blue">using </span>NHibernate;
<span style="color: blue">using </span>Ninject;
<span style="color: blue">using </span>Wnn.Business;

<span style="color: blue">namespace </span>Wnn.Service.Host
{
    <span style="color: blue">public class </span><span style="color: #2b91af">NHibernateInstanceProvider </span>: <span style="color: #2b91af">IInstanceProvider
    </span>{
        <span style="color: blue">private const string </span>sessionKey = <span style="color: #a31515">"ThreadSession"</span>;
        <span style="color: blue">private const string </span>transactionKey = <span style="color: #a31515">"ThreadTransaction"</span>;
        <span style="color: blue">private readonly </span><span style="color: #2b91af">Type </span>serviceType;

        <span style="color: blue">private static </span><span style="color: #2b91af">SessionManager </span>SessionManager
        {
            <span style="color: blue">get </span>{ <span style="color: blue">return </span><span style="color: #2b91af">CallContext</span>.GetData( sessionKey ) <span style="color: blue">as </span><span style="color: #2b91af">SessionManager</span>; }
            <span style="color: blue">set </span>{ <span style="color: #2b91af">CallContext</span>.SetData( sessionKey, <span style="color: blue">value </span>); }
        }

        <span style="color: blue">private static </span><span style="color: #2b91af">ITransaction </span>Transaction
        {
            <span style="color: blue">get </span>{ <span style="color: blue">return </span><span style="color: #2b91af">CallContext</span>.GetData( transactionKey ) <span style="color: blue">as </span><span style="color: #2b91af">ITransaction</span>; }
            <span style="color: blue">set </span>{ <span style="color: #2b91af">CallContext</span>.SetData( transactionKey, <span style="color: blue">value </span>); }
        }

        <span style="color: blue">public </span>NHibernateInstanceProvider( <span style="color: #2b91af">Type </span>serviceType )
        {
            <span style="color: blue">this</span>.serviceType = serviceType;
        }

        <span style="color: blue">public object </span>GetInstance( <span style="color: #2b91af">InstanceContext </span>instanceContext )
        {
            <span style="color: blue">return </span>GetInstance( instanceContext, <span style="color: blue">null </span>);
        }

        <span style="color: blue">public object </span>GetInstance( <span style="color: #2b91af">InstanceContext </span>instanceContext, <span style="color: #2b91af">Message </span>message )
        {
            <span style="color: blue">var </span>connectionStringKey = GetConnectionStringKey( serviceType );

            <span style="color: blue">var </span>kernel = <span style="color: blue">new </span><span style="color: #2b91af">StandardKernel</span>( <span style="color: blue">new </span><span style="color: #2b91af">NinjectSetup</span>( connectionStringKey ) );

            SessionManager = kernel.Get&lt;<span style="color: #2b91af">SessionManager</span>&gt;();
            Transaction = SessionManager.BeginTransaction();

            <span style="color: blue">var </span>instance = kernel.Get( serviceType );

            <span style="color: blue">return </span>instance;
        }

        <span style="color: blue">public void </span>ReleaseInstance( <span style="color: #2b91af">InstanceContext </span>instanceContext, <span style="color: blue">object </span>instance )
        {
            <span style="color: blue">try
            </span>{
                Transaction.Commit();
            }
            <span style="color: blue">catch
            </span>{
                Transaction.Rollback();
            }
            <span style="color: blue">finally
            </span>{
                SessionManager.Close();
                SessionManager.Dispose();
            }
        }

        <span style="color: blue">private static string </span>GetConnectionStringKey( <span style="color: #2b91af">Type </span>serviceType )
        {
            <span style="color: green">// All of our database connections should
            // go here. We could also put all of these types
            // into a config file and load them dynamically.
            </span><span style="color: blue">string </span>connectionStringKey;
            <span style="color: blue">if</span>( serviceType == <span style="color: blue">typeof</span>( <span style="color: #2b91af">BlogService </span>) )
            {
                connectionStringKey = <span style="color: #a31515">"Dashboard"</span>;
            }
            <span style="color: blue">else
            </span>{
                <span style="color: blue">throw new </span><span style="color: #2b91af">Exception</span>( <span style="color: #a31515">"Service type '{0}' does not match any database connection." </span>);
            }
            <span style="color: blue">return </span>connectionStringKey;
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now you may be wondering how all of this is setup. How do we let WCF know that it should use our instance provider instead? There are a couple custom configuration classes we need to create and we need to add our provider to the WCF configuration. Whatever app is running the WCF service; console app, windows service, or web app; a custom behavior needs to be added to it. In the services section, we setup our service with our custom element.</p>
<p><a href="http://11011.net/software/vspaste"></a></p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: #a31515">system.serviceModel</span><span style="color: blue">&gt;
    &lt;</span><span style="color: #a31515">extensions</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">behaviorExtensions</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">add </span><span style="color: red">name</span><span style="color: blue">=</span>"<span style="color: blue">nhibernateServiceBehavior</span>" <span style="color: red">type</span><span style="color: blue">=</span>"<span style="color: blue">Wnn.Service.Host.NHibernateBehaviorExtensionElement, Wnn.Service.Host, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null</span>" <span style="color: blue">/&gt;
        &lt;/</span><span style="color: #a31515">behaviorExtensions</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">extensions</span><span style="color: blue">&gt;

    &lt;</span><span style="color: #a31515">behaviors</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">serviceBehaviors</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">behavior </span><span style="color: red">name</span><span style="color: blue">=</span>"<span style="color: blue">nhibernateServiceBehavior</span>"<span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">nhibernateServiceBehavior </span><span style="color: blue">/&gt;
                &lt;</span><span style="color: #a31515">serviceMetadata </span><span style="color: blue">/&gt;
            &lt;/</span><span style="color: #a31515">behavior</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">serviceBehaviors</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">endpointBehaviors</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">behavior </span><span style="color: red">name</span><span style="color: blue">=</span>"<span style="color: blue">HttpEnableBehavior</span>"<span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">webHttp</span><span style="color: blue">/&gt;
            &lt;/</span><span style="color: #a31515">behavior</span><span style="color: blue">&gt;
        &lt;/</span><span style="color: #a31515">endpointBehaviors</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">behaviors</span><span style="color: blue">&gt;

    &lt;</span><span style="color: #a31515">services</span><span style="color: blue">&gt;
        &lt;</span><span style="color: #a31515">service </span><span style="color: red">name</span><span style="color: blue">=</span>"<span style="color: blue">Wnn.Service.BlogService</span>" <span style="color: red">behaviorConfiguration</span><span style="color: blue">=</span>"<span style="color: blue">nhibernateServiceBehavior</span>"<span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">host</span><span style="color: blue">&gt;
                &lt;</span><span style="color: #a31515">baseAddresses</span><span style="color: blue">&gt;
                    &lt;</span><span style="color: #a31515">add </span><span style="color: red">baseAddress</span><span style="color: blue">=</span>"<span style="color: blue">net.tcp://localhost:8761/Services/</span>" <span style="color: blue">/&gt;
                &lt;/</span><span style="color: #a31515">baseAddresses</span><span style="color: blue">&gt;
            &lt;/</span><span style="color: #a31515">host</span><span style="color: blue">&gt;
            &lt;</span><span style="color: #a31515">endpoint </span><span style="color: red">address</span><span style="color: blue">=</span>"<span style="color: #0000ff">Blog</span>" <span style="color: red">binding</span><span style="color: blue">=</span>"<span style="color: blue">netTcpBinding</span>" <span style="color: red">contract</span><span style="color: blue">=</span>"<span style="color: blue">Wnn.Service.Contract.ServiceContracts.IBlogService</span>" <span style="color: blue">/&gt;
        &lt;/</span><span style="color: #a31515">service</span><span style="color: blue">&gt;
    &lt;/</span><span style="color: #a31515">services</span><span style="color: blue">&gt;
&lt;/</span><span style="color: #a31515">system.serviceModel</span><span style="color: blue">&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The custom element returns a new instance of our service behavior.</p>
<p>NHibernateBehaviorExtensionElement.cs</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.ServiceModel.Configuration;

<span style="color: blue">namespace </span>Wnn.Service.Host
{
    <span style="color: blue">public class </span><span style="color: #2b91af">NHibernateBehaviorExtensionElement </span>: <span style="color: #2b91af">BehaviorExtensionElement
    </span>{
        <span style="color: blue">protected override object </span>CreateBehavior()
        {
            <span style="color: blue">return new </span><span style="color: #2b91af">NHibernateServiceBehavior</span>();
        }

        <span style="color: blue">public override </span><span style="color: #2b91af">Type </span>BehaviorType
        {
            <span style="color: blue">get </span>{ <span style="color: blue">return typeof</span>( <span style="color: #2b91af">NHibernateServiceBehavior </span>); }
        }
    }
}</pre>
<p>The service behavior is what actually creates out custom instance provider, passing in the service that is being used.</p>
<p>NHibernateServiceBehavior.cs</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Collections.ObjectModel;
<span style="color: blue">using </span>System.ServiceModel;
<span style="color: blue">using </span>System.ServiceModel.Channels;
<span style="color: blue">using </span>System.ServiceModel.Description;
<span style="color: blue">using </span>System.ServiceModel.Dispatcher;

<span style="color: blue">namespace </span>Wnn.Service.Host
{
    <span style="color: blue">public class </span><span style="color: #2b91af">NHibernateServiceBehavior </span>: <span style="color: #2b91af">Attribute</span>, <span style="color: #2b91af">IServiceBehavior
    </span>{
        <span style="color: blue">public void </span>Validate( <span style="color: #2b91af">ServiceDescription </span>serviceDescription, <span style="color: #2b91af">ServiceHostBase </span>serviceHostBase )
        {
        }

        <span style="color: blue">public void </span>AddBindingParameters( <span style="color: #2b91af">ServiceDescription </span>serviceDescription, <span style="color: #2b91af">ServiceHostBase </span>serviceHostBase, <span style="color: #2b91af">Collection</span>&lt;<span style="color: #2b91af">ServiceEndpoint</span>&gt; endpoints, <span style="color: #2b91af">BindingParameterCollection </span>bindingParameters )
        {
        }

        <span style="color: blue">public void </span>ApplyDispatchBehavior( <span style="color: #2b91af">ServiceDescription </span>serviceDescription, <span style="color: #2b91af">ServiceHostBase </span>serviceHostBase )
        {
            <span style="color: blue">foreach</span>( <span style="color: blue">var </span>channelDispatcherBase <span style="color: blue">in </span>serviceHostBase.ChannelDispatchers )
            {
                <span style="color: blue">var </span>channelDispatcher = channelDispatcherBase <span style="color: blue">as </span><span style="color: #2b91af">ChannelDispatcher</span>;
                <span style="color: blue">if</span>( channelDispatcher == <span style="color: blue">null </span>)
                {
                    <span style="color: blue">continue</span>;
                }

                <span style="color: blue">foreach</span>( <span style="color: blue">var </span>ed <span style="color: blue">in </span>channelDispatcher.Endpoints )
                {
                    ed.DispatchRuntime.InstanceProvider = <span style="color: blue">new </span><span style="color: #2b91af">NHibernateInstanceProvider</span>( serviceDescription.ServiceType );
                }
            }
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>The WCF setup is now complete. We able to have multiple services running and use multiple databases. All the services objects are created by Ninject. Our WCF application is now a lot more unit testable that it was before. This may seem like a lot of work to setup, but once it’s done, everything else is a breeze. You will be able to add services and databases easily, and you don’t have to worry about creating your objects in your service.</p>
<p>The last step is to actually host this service in a console app, windows service or web app. You are limited to only using HTTP bindings if you choose to use an IIS hosted service. All you have to do then is to create an instance of the ServiceHostEngine, and start it.</p>
<p>Here is an example of a console app:</p>
<p>Program.cs</p>
<pre class="code"><span style="color: blue">namespace </span>Wnn.Service.Host.Console
{
    <span style="color: blue">class </span><span style="color: #2b91af">Program
    </span>{
        <span style="color: blue">static void </span>Main( <span style="color: blue">string</span>[] args )
        {
            <span style="color: blue">var </span>service = <span style="color: blue">new </span><span style="color: #2b91af">ServiceHostEngine</span>();
            service.Start();
            System.<span style="color: #2b91af">Console</span>.WriteLine( <span style="color: #a31515">"Press any key to stop the services" </span>);
            System.<span style="color: #2b91af">Console</span>.ReadKey();
        }
    }
}</pre>
<p>I put the code up on github. <a href="http://github.com/JoshClose/WcfNhibernateNinjectExample" target="_blank">http://github.com/JoshClose/WcfNhibernateNinjectExample</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=264</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Programmatically Taking a Full Web Page Screenshot</title>
		<link>http://joshclose.net/?p=247</link>
		<comments>http://joshclose.net/?p=247#comments</comments>
		<pubDate>Wed, 23 Sep 2009 13:50:13 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[Screenshot]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=247</guid>
		<description><![CDATA[I needed to programmatically take a screenshot of a full web page, and not just the visible area. When I set out to accomplish this task, I didn’t realize how tough it was actually going to be. 
The reason it was tough wasn’t because it was hard to do, but that there were many different [...]]]></description>
			<content:encoded><![CDATA[<p>I needed to programmatically take a screenshot of a full web page, and not just the visible area. When I set out to accomplish this task, I didn’t realize how tough it was actually going to be. </p>
<p>The reason it was tough wasn’t because it was hard to do, but that there were many different ways of doing it, and each had different results. Some ways would only capture the visible screen. Some would be missing backgrounds. Some wouldn’t capture pages that loaded with JavaScript, such as google.com.</p>
<p>I was able to find a way that has worked on every site I’ve tested so far that is a combination of many of the other methods I’ve found. This way also does not require any assembly outside of the System namespace.</p>
<p>The code is pretty small and self explanatory, so here is how I did it.</p>
<pre class="code"><span style="color: blue">using </span>System;
<span style="color: blue">using </span>System.Drawing;
<span style="color: blue">using </span>System.Runtime.InteropServices;
<span style="color: blue">using </span>System.Windows.Forms;

<span style="color: blue">public class </span><span style="color: #2b91af">ScreenShot
</span>{
    [<span style="color: #2b91af">ComImport</span>]
    [<span style="color: #2b91af">InterfaceType</span>( <span style="color: #2b91af">ComInterfaceType</span>.InterfaceIsIUnknown )]
    [<span style="color: #2b91af">Guid</span>( <span style="color: #a31515">&quot;0000010d-0000-0000-C000-000000000046&quot; </span>)]
    <span style="color: blue">private interface </span><span style="color: #2b91af">IViewObject
    </span>{
        [<span style="color: #2b91af">PreserveSig</span>]
        <span style="color: blue">int </span>Draw( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>dwDrawAspect, <span style="color: blue">int </span>lindex, <span style="color: #2b91af">IntPtr </span>pvAspect,
                  [<span style="color: #2b91af">In</span>] <span style="color: green">/*tagDVTARGETDEVICE*/ </span><span style="color: #2b91af">IntPtr </span>ptd, <span style="color: #2b91af">IntPtr </span>hdcTargetDev, <span style="color: #2b91af">IntPtr </span>hdcDraw,
                  [<span style="color: #2b91af">In</span>] <span style="color: green">/*COMRECT*/ </span><span style="color: #2b91af">Rectangle </span>lprcBounds, [<span style="color: #2b91af">In</span>] <span style="color: green">/*COMRECT*/ </span><span style="color: #2b91af">IntPtr </span>lprcWBounds, <span style="color: #2b91af">IntPtr </span>pfnContinue,
                  [<span style="color: #2b91af">In</span>] <span style="color: blue">int </span>dwContinue );

        [<span style="color: #2b91af">PreserveSig</span>]
        <span style="color: blue">int </span>GetColorSet( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>dwDrawAspect, <span style="color: blue">int </span>lindex, <span style="color: #2b91af">IntPtr </span>pvAspect,
                         [<span style="color: #2b91af">In</span>] <span style="color: green">/*tagDVTARGETDEVICE*/ </span><span style="color: #2b91af">IntPtr </span>ptd, <span style="color: #2b91af">IntPtr </span>hicTargetDev, [<span style="color: #2b91af">Out</span>] <span style="color: green">/*tagLOGPALETTE*/ </span><span style="color: #2b91af">IntPtr </span>ppColorSet );

        [<span style="color: #2b91af">PreserveSig</span>]
        <span style="color: blue">int </span>Freeze( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>dwDrawAspect, <span style="color: blue">int </span>lindex, <span style="color: #2b91af">IntPtr </span>pvAspect, [<span style="color: #2b91af">Out</span>] <span style="color: #2b91af">IntPtr </span>pdwFreeze );

        [<span style="color: #2b91af">PreserveSig</span>]
        <span style="color: blue">int </span>Unfreeze( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>dwFreeze );

        <span style="color: blue">void </span>SetAdvise( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>aspects, [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.U4 )] <span style="color: blue">int </span>advf,
                        [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.Interface )] <span style="color: green">/*IAdviseSink*/ </span><span style="color: #2b91af">IntPtr </span>pAdvSink );

        <span style="color: blue">void </span>GetAdvise( [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">Out</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.LPArray )] <span style="color: blue">int</span>[] paspects,
                        [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">Out</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.LPArray )] <span style="color: blue">int</span>[] advf,
                        [<span style="color: #2b91af">In</span>] [<span style="color: #2b91af">Out</span>] [<span style="color: #2b91af">MarshalAs</span>( <span style="color: #2b91af">UnmanagedType</span>.LPArray )] <span style="color: green">/*IAdviseSink[]*/ </span><span style="color: #2b91af">IntPtr</span>[] pAdvSink );
    }

    <span style="color: blue">public static </span><span style="color: #2b91af">Bitmap </span>Create( <span style="color: blue">string </span>url )
    {
        <span style="color: blue">using</span>( <span style="color: blue">var </span>webBrowser = <span style="color: blue">new </span><span style="color: #2b91af">WebBrowser</span>() )
        {
            webBrowser.ScrollBarsEnabled = <span style="color: blue">false</span>;
            webBrowser.ScriptErrorsSuppressed = <span style="color: blue">true</span>;
            webBrowser.Navigate( url );

            <span style="color: blue">while</span>( webBrowser.ReadyState != <span style="color: #2b91af">WebBrowserReadyState</span>.Complete )
            {
                <span style="color: #2b91af">Application</span>.DoEvents();
            }

            webBrowser.Width = webBrowser.Document.Body.ScrollRectangle.Width;
            webBrowser.Height = webBrowser.Document.Body.ScrollRectangle.Height;

            <span style="color: blue">var </span>bitmap = <span style="color: blue">new </span><span style="color: #2b91af">Bitmap</span>( webBrowser.Width, webBrowser.Height );
            <span style="color: blue">var </span>graphics = <span style="color: #2b91af">Graphics</span>.FromImage( bitmap );
            <span style="color: blue">var </span>hdc = graphics.GetHdc();

            <span style="color: blue">var </span>rect = <span style="color: blue">new </span><span style="color: #2b91af">Rectangle</span>( 0, 0, webBrowser.Width, webBrowser.Height );

            <span style="color: blue">var </span>viewObject = (<span style="color: #2b91af">IViewObject</span>)webBrowser.Document.DomDocument;
            viewObject.Draw( 1, -1, (<span style="color: #2b91af">IntPtr</span>)0, (<span style="color: #2b91af">IntPtr</span>)0, (<span style="color: #2b91af">IntPtr</span>)0, hdc, rect, (<span style="color: #2b91af">IntPtr</span>)0, (<span style="color: #2b91af">IntPtr</span>)0, 0 );

            graphics.ReleaseHdc( hdc );

            <span style="color: blue">return </span>bitmap;
        }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>From here, it’s pretty simple to start adding features like min/max width/height, a capture delay for pages that have Flash on them, etc.</p>
<p><strong>Note:</strong> For Flash to work properly, you need to set the project type to x86. If running on a 64-bit system and the project is set to “Any CPU”, Flash won’t work because it’s not compatible with 64-bit systems.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=247</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically Loading Partial Views with the Spark View Engine</title>
		<link>http://joshclose.net/?p=244</link>
		<comments>http://joshclose.net/?p=244#comments</comments>
		<pubDate>Thu, 10 Sep 2009 17:41:29 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Spark View Engine]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=244</guid>
		<description><![CDATA[Loading a partial view is easy with Spark. There are basically two ways:
&#60;use file=&#34;MyPartialView&#34; /&#62;

And if you have name your files using the _MyPartialView.spark convention:
&#60;MyPartialView /&#62;
Now what if you don’t know the name of the partial file until runtime and need to dynamically render the view? As far as I know, there is no way [...]]]></description>
			<content:encoded><![CDATA[<p>Loading a partial view is easy with Spark. There are basically two ways:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: maroon">use </span><span style="color: red">file</span>=<span style="color: blue">&quot;MyPartialView&quot; /&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>And if you have name your files using the _MyPartialView.spark convention:</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: maroon">MyPartialView </span><span style="color: blue">/&gt;</span></pre>
<p><a href="http://11011.net/software/vspaste"></a>Now what if you don’t know the name of the partial file until runtime and need to dynamically render the view? As far as I know, there is no way of doing this in Spark. How you can get around this is by doing some inline code using ASP.NET MVCs RenderPartial method.</p>
<pre class="code"><span style="color: teal">#</span>Html.RenderPartial( myPartialViewVariable );</pre>
<p><a href="http://11011.net/software/vspaste"></a>This is assuming myPartialViewVariable is a string variable with the name of the view that needs to be rendered. </p>
<p><strong>Update:</strong></p>
<p>When you use ASP.NET’s Html.RenderPartial, your data isn’t automatically passed along to the spark view. To pass your data along, use the ViewData dictionary, like you would in a controller.</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: maroon">ul</span><span style="color: blue">&gt;
    &lt;</span><span style="color: maroon">li </span><span style="color: red">each</span>=<span style="color: blue">&quot;var item in items&quot;&gt;
        </span><span style="color: teal">#</span>ViewData[<span style="color: maroon">&quot;item&quot;</span>] = item;
        <span style="color: teal">#</span>Html.RenderPartial( <span style="color: maroon">&quot;path/to/&quot; </span>+ item.Name );
    <span style="color: blue">&lt;/</span><span style="color: maroon">li</span><span style="color: blue">&gt;
&lt;/</span><span style="color: maroon">ul</span><span style="color: blue">&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>In the partial view, use the &lt;viewdata /&gt; spark attribute like you normally would in a spark file.</p>
<pre class="code"><span style="color: blue">&lt;</span><span style="color: maroon">viewdata </span><span style="color: red">item</span>=<span style="color: blue">&quot;Item&quot; /&gt;
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=244</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>FireFox is Slow When Using Visual Studios ASP.NET Development Server (Cassini)</title>
		<link>http://joshclose.net/?p=239</link>
		<comments>http://joshclose.net/?p=239#comments</comments>
		<pubDate>Thu, 20 Aug 2009 19:52:48 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Cassini]]></category>
		<category><![CDATA[FireFox]]></category>
		<category><![CDATA[Visual Studio]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=239</guid>
		<description><![CDATA[If you&#8217;re ever running a web app locally in Cassini (Visual Studios built in web server) and FireFox is really really really slow, but IE is fast like it should be, then you probably need to turn off IPV6 in FireFox.
To do this, type &#8220;about:config&#8221; in the address bar and filter on &#8220;v6&#8243;. Turn off [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re ever running a web app locally in Cassini (Visual Studios built in web server) and FireFox is really really really slow, but IE is fast like it should be, then you probably need to turn off IPV6 in FireFox.</p>
<p>To do this, type &#8220;about:config&#8221; in the address bar and filter on &#8220;v6&#8243;. Turn off IPV6 and FireFox should be fast again.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=239</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>ASP.NET Magic Maintenance Mode</title>
		<link>http://joshclose.net/?p=237</link>
		<comments>http://joshclose.net/?p=237#comments</comments>
		<pubDate>Wed, 19 Aug 2009 19:39:12 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET]]></category>

		<guid isPermaLink="false">http://joshclose.net/?p=237</guid>
		<description><![CDATA[I just recently found that you&#8217;re able to easily put your ASP.NET website int &#8220;maintenance mode&#8221;. All you need to do is add an App_Offline.htm file to you site root, and ASP.NET will serve up all requests to that file. Just rename or remove the file when you&#8217;re down with your maintenance.
Scott Gu has a [...]]]></description>
			<content:encoded><![CDATA[<p>I just recently found that you&#8217;re able to easily put your ASP.NET website int &#8220;maintenance mode&#8221;. All you need to do is add an App_Offline.htm file to you site root, and ASP.NET will serve up all requests to that file. Just rename or remove the file when you&#8217;re down with your maintenance.</p>
<p>Scott Gu has a couple good posts on this.</p>
<p><a title="http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx" href="http://" target="_blank">http://weblogs.asp.net/scottgu/archive/2005/10/06/426755.aspx</a></p>
<p><a title="http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx" href="http://" target="_blank">http://weblogs.asp.net/scottgu/archive/2006/04/09/442332.aspx</a></p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=237</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ASP.NET MVC, Ninject.Web.Mvc and 404’s</title>
		<link>http://joshclose.net/?p=229</link>
		<comments>http://joshclose.net/?p=229#comments</comments>
		<pubDate>Thu, 06 Aug 2009 21:00:34 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ASP.NET MVC]]></category>
		<category><![CDATA[Ninject]]></category>

		<guid isPermaLink="false">http://joshclose.wordpress.com/2009/08/06/asp-net-mvc-ninject-web-mvc-and-404s/</guid>
		<description><![CDATA[I went about trying to handle 404 errors on my ASP.NET MVC application by following code I found here.
protected void Application_Error( object sender, EventArgs e )
{
    var exception = Server.GetLastError();

    Response.Clear();

    var httpException = exception as HttpException;

    var routeData = new RouteData();
  [...]]]></description>
			<content:encoded><![CDATA[<p>I went about trying to handle 404 errors on my ASP.NET MVC application by following code I found <a href="http://stackoverflow.com/questions/619895/how-can-i-properly-handle-404s-in-asp-net-mvc" target="_blank">here</a>.</p>
<pre class="code"><span style="color:blue;">protected void </span>Application_Error( <span style="color:blue;">object </span>sender, <span style="color:#2b91af;">EventArgs </span>e )
{
    <span style="color:blue;">var </span>exception = Server.GetLastError();

    Response.Clear();

    <span style="color:blue;">var </span>httpException = exception <span style="color:blue;">as </span><span style="color:#2b91af;">HttpException</span>;

    <span style="color:blue;">var </span>routeData = <span style="color:blue;">new </span><span style="color:#2b91af;">RouteData</span>();
    routeData.Values.Add( <span style="color:#a31515;">&quot;controller&quot;</span>, <span style="color:#a31515;">&quot;Error&quot; </span>);

    <span style="color:blue;">if</span>( httpException == <span style="color:blue;">null </span>)
    {
        routeData.Values.Add( <span style="color:#a31515;">&quot;action&quot;</span>, <span style="color:#a31515;">&quot;Index&quot; </span>);
    }
    <span style="color:blue;">else </span><span style="color:green;">//It's an Http Exception, Let's handle it.
    </span>{
        <span style="color:blue;">switch</span>( httpException.GetHttpCode() )
        {
            <span style="color:blue;">case </span>404:
                <span style="color:green;">// Page not found.
                </span>routeData.Values.Add( <span style="color:#a31515;">&quot;action&quot;</span>, <span style="color:#a31515;">&quot;HttpError404&quot; </span>);
                <span style="color:blue;">break</span>;
            <span style="color:blue;">case </span>500:
                <span style="color:green;">// Server error.
                </span>routeData.Values.Add( <span style="color:#a31515;">&quot;action&quot;</span>, <span style="color:#a31515;">&quot;HttpError500&quot; </span>);
                <span style="color:blue;">break</span>;

            <span style="color:green;">// Here you can handle Views to other error codes.
            // I choose a General error template
            </span><span style="color:blue;">default</span>:
                routeData.Values.Add( <span style="color:#a31515;">&quot;action&quot;</span>, <span style="color:#a31515;">&quot;General&quot; </span>);
                <span style="color:blue;">break</span>;
        }
    }

    <span style="color:green;">// Pass exception details to the target error View.
    </span>routeData.Values.Add( <span style="color:#a31515;">&quot;error&quot;</span>, exception );

    <span style="color:green;">// Clear the error on server.
    </span>Server.ClearError();

    <span style="color:green;">// Call target Controller and pass the routeData.
    </span><span style="color:#2b91af;">IController </span>errorController = <span style="color:blue;">new </span><span style="color:#2b91af;">ErrorController</span>();
    errorController.Execute( <span style="color:blue;">new </span><span style="color:#2b91af;">RequestContext</span>( <span style="color:blue;">new </span><span style="color:#2b91af;">HttpContextWrapper</span>( Context ), routeData ) );
}</pre>
<p><a href="http://11011.net/software/vspaste"></a>Although, this didn’t work for me because I’m using Ninject.Web.Mvc. Instead, I get this error:</p>
<p><i>The IControllerFactory &#8216;Ninject.Web.Mvc.NinjectControllerFactory&#8217; did not return a controller for a controller named &#8216;blah&#8217;.</i></p>
<p>The NinjectControllerFactory.CreateController( RequestContext requestContext, string controllerName ) method is returning null when it can’t find a controller, when it should be throwing a 404 HttpException.</p>
<p>There apparently is a patch waiting to be merged into trunk for this, but until then, you can use this change to the controller factory. The factory now inherits from DefaultControllerFactory, CreateController and ReleaseController are now overrides, and a test for a null controller will call base.CreateController.</p>
<pre class="code"><span style="color:blue;">using </span>System;
<span style="color:blue;">using </span>System.Globalization;
<span style="color:blue;">using </span>System.Web;
<span style="color:blue;">using </span>System.Web.Mvc;
<span style="color:blue;">using </span>System.Web.Routing;

<span style="color:blue;">namespace </span>Ninject.Web.Mvc
{
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">A controller factory that creates </span><span style="color:gray;">&lt;see cref=&quot;IController&quot;/&gt;</span><span style="color:green;">s via Ninject.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    </span><span style="color:blue;">public class </span><span style="color:#2b91af;">NinjectControllerFactory </span>: <span style="color:#2b91af;">DefaultControllerFactory
    </span>{
        <span style="color:gray;">/// &lt;summary&gt;
        /// </span><span style="color:green;">Gets the kernel that will be used to create controllers.
        </span><span style="color:gray;">/// &lt;/summary&gt;
        </span><span style="color:blue;">public </span><span style="color:#2b91af;">IKernel </span>Kernel { <span style="color:blue;">get</span>; <span style="color:blue;">private set</span>; }

        <span style="color:gray;">/// &lt;summary&gt;
        /// </span><span style="color:green;">Initializes a new instance of the </span><span style="color:gray;">&lt;see cref=&quot;NinjectControllerFactory&quot;/&gt; </span><span style="color:green;">class.
        </span><span style="color:gray;">/// &lt;/summary&gt;
        /// &lt;param name=&quot;kernel&quot;&gt;</span><span style="color:green;">The kernel that should be used to create controllers.</span><span style="color:gray;">&lt;/param&gt;
        </span><span style="color:blue;">public </span>NinjectControllerFactory(<span style="color:#2b91af;">IKernel </span>kernel)
        {
            Kernel = kernel;
        }

        <span style="color:gray;">/// &lt;summary&gt;
        /// </span><span style="color:green;">Creates the controller with the specified name.
        </span><span style="color:gray;">/// &lt;/summary&gt;
        /// &lt;param name=&quot;requestContext&quot;&gt;</span><span style="color:green;">The request context.</span><span style="color:gray;">&lt;/param&gt;
        /// &lt;param name=&quot;controllerName&quot;&gt;</span><span style="color:green;">Name of the controller.</span><span style="color:gray;">&lt;/param&gt;
        /// &lt;returns&gt;</span><span style="color:green;">The created controller.</span><span style="color:gray;">&lt;/returns&gt;
        </span><span style="color:blue;">public override </span><span style="color:#2b91af;">IController </span>CreateController(<span style="color:#2b91af;">RequestContext </span>requestContext, <span style="color:blue;">string </span>controllerName)
        {
            <span style="color:blue;">var </span>controller = Kernel.TryGet&lt;<span style="color:#2b91af;">IController</span>&gt;(controllerName.ToLowerInvariant());

            <span style="color:blue;">if</span>(controller == <span style="color:blue;">null</span>)
                <span style="color:blue;">return base</span>.CreateController(requestContext, controllerName);

            <span style="color:blue;">var </span>standardController = controller <span style="color:blue;">as </span><span style="color:#2b91af;">Controller</span>;

            <span style="color:blue;">if</span>(standardController != <span style="color:blue;">null</span>)
                standardController.ActionInvoker = <span style="color:blue;">new </span><span style="color:#2b91af;">NinjectActionInvoker</span>(Kernel);

            <span style="color:blue;">return </span>controller;
        }

        <span style="color:gray;">/// &lt;summary&gt;
        /// </span><span style="color:green;">Releases the specified controller.
        </span><span style="color:gray;">/// &lt;/summary&gt;
        /// &lt;param name=&quot;controller&quot;&gt;</span><span style="color:green;">The controller to release.</span><span style="color:gray;">&lt;/param&gt;
        </span><span style="color:blue;">public override void </span>ReleaseController(<span style="color:#2b91af;">IController </span>controller) { }
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=229</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NHibernate 2.1 and Unit Testing with MSTest Using MSBuild</title>
		<link>http://joshclose.net/?p=227</link>
		<comments>http://joshclose.net/?p=227#comments</comments>
		<pubDate>Thu, 30 Jul 2009 14:45:02 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[MSBuild]]></category>
		<category><![CDATA[MSTest]]></category>
		<category><![CDATA[NHibernate]]></category>

		<guid isPermaLink="false">http://joshclose.wordpress.com/2009/07/30/nhibernate-2-1-and-unit-testing-with-mstest-using-msbuild/</guid>
		<description><![CDATA[I have a MSBuild script that Cruise Control is using to do it’s build and run the unit tests. After updating to NHibernate 2.1 and using NHibernate.ByteCode.LinFu for the ProxyFactoryFactory, the unit tests started failing in the build script, but not in Visual Studio. 
I checked the references of my test project, and NHibernate.ByteCode.LinFu.dll was [...]]]></description>
			<content:encoded><![CDATA[<p>I have a MSBuild script that Cruise Control is using to do it’s build and run the unit tests. After updating to NHibernate 2.1 and using NHibernate.ByteCode.LinFu for the ProxyFactoryFactory, the unit tests started failing in the build script, but not in Visual Studio. </p>
<p>I checked the references of my test project, and NHibernate.ByteCode.LinFu.dll was referenced, but the assembly wasn’t being copied into the test folder when ran with the script. I opened the test assembly up with ildasm.exe and checked the manifest and the LinFu assembly wasn’t in there. It was being references in the project, but apparently since there is not code actually using the assembly, the reference isn’t built into the manifest.</p>
<p>The way I fixed this is, in the unit test class I added a deployment item for the assembly that I wanted copied.</p>
<pre class="code">[<span style="color:#2b91af;">DeploymentItem</span>( <span style="color:#a31515;">@&quot;..\References\NHibernate\NHibernate.ByteCode.LinFu.dll&quot; </span>)]</pre>
<p>&#160;</p>
<p>This fixed the failing unit test. Life is good again.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=227</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How Safe is the Using Block?</title>
		<link>http://joshclose.net/?p=226</link>
		<comments>http://joshclose.net/?p=226#comments</comments>
		<pubDate>Tue, 28 Jul 2009 13:42:48 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://joshclose.wordpress.com/2009/07/28/how-safe-is-the-using-block/</guid>
		<description><![CDATA[I’m sure there are a lot of people out there who think of the using block as their security blanket. You can just wrap anything that has a close or dispose method on it in a using block, and everything is handled the way it should be, and automagically. Well, this isn’t always the case.
While [...]]]></description>
			<content:encoded><![CDATA[<p>I’m sure there are a lot of people out there who think of the using block as their security blanket. You can just wrap anything that has a close or dispose method on it in a using block, and everything is handled the way it should be, and automagically. Well, this isn’t always the case.</p>
<p>While doing some WCF work using the System.Component.ClientBase&lt;TChannel&gt;, I was running into a few issues. I was creating client proxy classes that inherited from ClientBase&lt;TChannel&gt;, and when using these, I would just wrap them in a using block.</p>
<pre class="code"><span style="color:blue;">using</span>( <span style="color:blue;">var </span>proxy = <span style="color:blue;">new </span><span style="color:#2b91af;">MyProxy</span>() )
{
    <span style="color:green;">// Do some work.
</span>}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>This seems like what you’re supposed to do, but in this case, it’s not. I ran across an article on MSDN <a href="http://msdn.microsoft.com/en-us/library/aa355056.aspx" target="_blank">“Avoiding Problems with the Using Statement”</a> that explains why you can’t use the using block. Basically, the Close() method on the proxy can throw exceptions, such as a timeout, and if this occurs, you need to call Abort() instead.</p>
<p>Here is the correct way to handle a proxy:</p>
<pre class="code"><span style="color:blue;">var </span>proxy = <span style="color:blue;">new </span><span style="color:#2b91af;">MyProxy</span>();
<span style="color:blue;">try
</span>{
    <span style="color:green;">// Do some work.
    </span>proxy.Close();
}
<span style="color:blue;">catch</span>( <span style="color:#2b91af;">CommunicationException </span>ex )
{
    proxy.Abort();
}
<span style="color:blue;">catch</span>( <span style="color:#2b91af;">TimeoutException </span>ex )
{
    proxy.Abort();
}
<span style="color:blue;">catch</span>( <span style="color:#2b91af;">Exception </span>ex )
{
    proxy.Abort();
    <span style="color:blue;">throw</span>;
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>EDIT:</p>
<p>After doing this a couple times, it gets pretty repetitive. I ended up creating a base proxy class.</p>
<pre class="code">    <span style="color:blue;">public abstract class </span><span style="color:#2b91af;">ProxyBase</span>&lt;TChannel&gt; : <span style="color:#2b91af;">ClientBase</span>&lt;TChannel&gt;, <span style="color:#2b91af;">IDisposable </span><span style="color:blue;">where </span>TChannel : <span style="color:blue;">class
</span></pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Then I implemented IDisposable.</p>
<pre class="code"><span style="color:blue;">protected void </span>CheckDisposed()
{
    <span style="color:blue;">if</span>( disposed )
    {
        <span style="color:blue;">throw new </span><span style="color:#2b91af;">ObjectDisposedException</span>( GetType().Name );
    }
}

<span style="color:blue;">public void </span>Dispose()
{
    Dispose( <span style="color:blue;">true </span>);
    <span style="color:#2b91af;">GC</span>.SuppressFinalize( <span style="color:blue;">this </span>);
}

<span style="color:blue;">protected virtual void </span>Dispose( <span style="color:blue;">bool </span>disposing )
{
    <span style="color:blue;">if</span>( !disposed )
    {
        <span style="color:blue;">if</span>( disposing )
        {
            <span style="color:blue;">try
            </span>{
                <span style="color:blue;">base</span>.Close();
            }
            <span style="color:blue;">catch</span>( <span style="color:#2b91af;">CommunicationException </span>ex )
            {
                <span style="color:blue;">base</span>.Abort();
            }
            <span style="color:blue;">catch</span>( <span style="color:#2b91af;">TimeoutException </span>ex )
            {
                <span style="color:blue;">base</span>.Abort();
            }
            <span style="color:blue;">catch</span>( <span style="color:#2b91af;">Exception </span>ex )
            {
                <span style="color:blue;">base</span>.Abort();
                <span style="color:blue;">throw</span>;
            }
        }

        disposed = <span style="color:blue;">true</span>;
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now we can wrap our proxy in a using block again, and we get our security blanket back.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=226</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing Color Tint with C#</title>
		<link>http://joshclose.net/?p=221</link>
		<comments>http://joshclose.net/?p=221#comments</comments>
		<pubDate>Wed, 22 Jul 2009 13:17:21 +0000</pubDate>
		<dc:creator>Josh Close</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Color]]></category>

		<guid isPermaLink="false">http://joshclose.wordpress.com/2009/07/22/changing-color-tint-with-c/</guid>
		<description><![CDATA[I’m currently doing a web application that has some branding. I’m doing a bunch of charts on metrics and there are specific color combinations that are used with charts. Each combination, or theme, has 3 main colors and 1 tint of that color, so 6 total. If you need more colors, you can do a [...]]]></description>
			<content:encoded><![CDATA[<p>I’m currently doing a web application that has some branding. I’m doing a bunch of charts on metrics and there are specific color combinations that are used with charts. Each combination, or theme, has 3 main colors and 1 tint of that color, so 6 total. If you need more colors, you can do a tint percentage of the original color. Doing this in .NET wasn’t trivial and there isn’t a whole lot of information on the net about this.</p>
<p>First off, what does tinting mean? Basically, if you have red and want to tint it 10% lighter, you would add 10% of white to the red.</p>
<p>How this is done in .NET is by taking the System.Drawing.Color and converting it’s RGB values to HSL values. You then can bump the lighting up or down a certain percent. After it’s converted back into RGB color values it will be a tint of the original color.</p>
<p><strong>Converting RGB to HSL:</strong></p>
<p>This is simple in .NET. The color object has 3 methods for this: GetHue(), GetSaturation(), and GetBrightness(). But wait a second here, are brightness and lighting the same thing? No, they are not. Wikipedia has a good description on the difference <a title="http://en.wikipedia.org/wiki/HSL_and_HSV" href="http://en.wikipedia.org/wiki/HSL_and_HSV">http://en.wikipedia.org/wiki/HSL_and_HSV</a>. So why are we not converting into HSB instead of HSL? Apparently .NET is actually giving back lighting and not brightness. Chris Jackson has a great post on this <a title="http://blogs.msdn.com/cjacks/archive/2006/04/12/575476.aspx" href="http://blogs.msdn.com/cjacks/archive/2006/04/12/575476.aspx">http://blogs.msdn.com/cjacks/archive/2006/04/12/575476.aspx</a>. His conversion from HSL to RGB is also the one I’m using in this post. So we just assume that GetBrightness() is actually giving us the lighting that we need.</p>
<p><strong>Converting HSL to RGB:</strong></p>
<p>Here is the tricky part. Thanks to Chris Jackson for posting this code.</p>
<pre class="code"><span style="color:blue;">public static class </span><span style="color:#2b91af;">ColorHelper
</span>{
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Converts the HSL values to a Color.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;alpha&quot;&gt;</span><span style="color:green;">The alpha.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;hue&quot;&gt;</span><span style="color:green;">The hue.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;saturation&quot;&gt;</span><span style="color:green;">The saturation.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;lighting&quot;&gt;</span><span style="color:green;">The lighting.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;&lt;/returns&gt;
    </span><span style="color:blue;">public static </span><span style="color:#2b91af;">Color </span>FromHsl( <span style="color:blue;">int </span>alpha, <span style="color:blue;">float </span>hue, <span style="color:blue;">float </span>saturation, <span style="color:blue;">float </span>lighting )
    {
        <span style="color:blue;">if</span>( 0 &gt; alpha || 255 &lt; alpha )
        {
            <span style="color:blue;">throw new </span><span style="color:#2b91af;">ArgumentOutOfRangeException</span>( <span style="color:#a31515;">&quot;alpha&quot; </span>);
        }
        <span style="color:blue;">if</span>( 0f &gt; hue || 360f &lt; hue )
        {
            <span style="color:blue;">throw new </span><span style="color:#2b91af;">ArgumentOutOfRangeException</span>( <span style="color:#a31515;">&quot;hue&quot; </span>);
        }
        <span style="color:blue;">if</span>( 0f &gt; saturation || 1f &lt; saturation )
        {
            <span style="color:blue;">throw new </span><span style="color:#2b91af;">ArgumentOutOfRangeException</span>( <span style="color:#a31515;">&quot;saturation&quot; </span>);
        }
        <span style="color:blue;">if</span>( 0f &gt; lighting || 1f &lt; lighting )
        {
            <span style="color:blue;">throw new </span><span style="color:#2b91af;">ArgumentOutOfRangeException</span>( <span style="color:#a31515;">&quot;lighting&quot; </span>);
        }

        <span style="color:blue;">if</span>( 0 == saturation )
        {
            <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, <span style="color:#2b91af;">Convert</span>.ToInt32( lighting * 255 ), <span style="color:#2b91af;">Convert</span>.ToInt32( lighting * 255 ), <span style="color:#2b91af;">Convert</span>.ToInt32( lighting * 255 ) );
        }

        <span style="color:blue;">float </span>fMax, fMid, fMin;
        <span style="color:blue;">int </span>iSextant, iMax, iMid, iMin;

        <span style="color:blue;">if</span>( 0.5 &lt; lighting )
        {
            fMax = lighting - ( lighting * saturation ) + saturation;
            fMin = lighting + ( lighting * saturation ) - saturation;
        }
        <span style="color:blue;">else
        </span>{
            fMax = lighting + ( lighting * saturation );
            fMin = lighting - ( lighting * saturation );
        }

        iSextant = (<span style="color:blue;">int</span>)<span style="color:#2b91af;">Math</span>.Floor( hue / 60f );
        <span style="color:blue;">if</span>( 300f &lt;= hue )
        {
            hue -= 360f;
        }
        hue /= 60f;
        hue -= 2f * (<span style="color:blue;">float</span>)<span style="color:#2b91af;">Math</span>.Floor( ( ( iSextant + 1f ) % 6f ) / 2f );
        <span style="color:blue;">if</span>( 0 == iSextant % 2 )
        {
            fMid = hue * ( fMax - fMin ) + fMin;
        }
        <span style="color:blue;">else
        </span>{
            fMid = fMin - hue * ( fMax - fMin );
        }

        iMax = <span style="color:#2b91af;">Convert</span>.ToInt32( fMax * 255 );
        iMid = <span style="color:#2b91af;">Convert</span>.ToInt32( fMid * 255 );
        iMin = <span style="color:#2b91af;">Convert</span>.ToInt32( fMin * 255 );

        <span style="color:blue;">switch</span>( iSextant )
        {
            <span style="color:blue;">case </span>1:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMid, iMax, iMin );
            <span style="color:blue;">case </span>2:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMin, iMax, iMid );
            <span style="color:blue;">case </span>3:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMin, iMid, iMax );
            <span style="color:blue;">case </span>4:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMid, iMin, iMax );
            <span style="color:blue;">case </span>5:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMax, iMin, iMid );
            <span style="color:blue;">default</span>:
                <span style="color:blue;">return </span><span style="color:#2b91af;">Color</span>.FromArgb( alpha, iMax, iMid, iMin );
        }
    }
}</pre>
<p>Now that we can do the conversion, let’s create some extension methods on Color to do lightening and darkening.</p>
<pre class="code"><span style="color:blue;">public static class </span><span style="color:#2b91af;">ColorExtensions
</span>{
    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Tints the color by the given percent.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;color&quot;&gt;</span><span style="color:green;">The color being tinted.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;percent&quot;&gt;</span><span style="color:green;">The percent to tint. Ex: 0.1 will make the color 10% lighter.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">The new tinted color.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public static </span><span style="color:#2b91af;">Color </span>Lighten( <span style="color:blue;">this </span><span style="color:#2b91af;">Color </span>color, <span style="color:blue;">float </span>percent )
    {
        <span style="color:blue;">var </span>lighting = color.GetBrightness();
        lighting = lighting + lighting * percent;
        <span style="color:blue;">if</span>( lighting &gt; 1.0 )
        {
            lighting = 1;
        }
        <span style="color:blue;">else if</span>( lighting &lt;= 0 )
        {
            lighting = 0.1f;
        }
        <span style="color:blue;">var </span>tintedColor = <span style="color:#2b91af;">ColorHelper</span>.FromHsl( color.A, color.GetHue(), color.GetSaturation(), lighting );

        <span style="color:blue;">return </span>tintedColor;
    }

    <span style="color:gray;">/// &lt;summary&gt;
    /// </span><span style="color:green;">Tints the color by the given percent.
    </span><span style="color:gray;">/// &lt;/summary&gt;
    /// &lt;param name=&quot;color&quot;&gt;</span><span style="color:green;">The color being tinted.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;param name=&quot;percent&quot;&gt;</span><span style="color:green;">The percent to tint. Ex: 0.1 will make the color 10% darker.</span><span style="color:gray;">&lt;/param&gt;
    /// &lt;returns&gt;</span><span style="color:green;">The new tinted color.</span><span style="color:gray;">&lt;/returns&gt;
    </span><span style="color:blue;">public static </span><span style="color:#2b91af;">Color </span>Darken( <span style="color:blue;">this </span><span style="color:#2b91af;">Color </span>color, <span style="color:blue;">float </span>percent )
    {
        <span style="color:blue;">var </span>lighting = color.GetBrightness();
        lighting = lighting - lighting * percent;
        <span style="color:blue;">if</span>( lighting &gt; 1.0 )
        {
            lighting = 1;
        }
        <span style="color:blue;">else if</span>( lighting &lt;= 0 )
        {
            lighting = 0;
        }
        <span style="color:blue;">var </span>tintedColor = <span style="color:#2b91af;">ColorHelper</span>.FromHsl( color.A, color.GetHue(), color.GetSaturation(), lighting );

        <span style="color:blue;">return </span>tintedColor;
    }
}</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p>Now this makes it easy to create a tint of any color. To use this in a web page, we can use the System.Drawing.ColorTranslator.ToHtml( Color c ) method to give us our html color.</p>
]]></content:encoded>
			<wfw:commentRss>http://joshclose.net/?feed=rss2&amp;p=221</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
