<?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>Ryan Cromwell &#187; WCF</title>
	<atom:link href="http://blog.cromwellhaus.com/index.php/category/wcf/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.cromwellhaus.com</link>
	<description>Improving my craft...</description>
	<lastBuildDate>Tue, 07 Feb 2012 15:06:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Injecting a WCF Channel as Dependency via Windsor</title>
		<link>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/</link>
		<comments>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/#comments</comments>
		<pubDate>Tue, 18 Aug 2009 15:45:41 +0000</pubDate>
		<dc:creator>cromwellryan</dc:creator>
				<category><![CDATA[WCF]]></category>
		<category><![CDATA[Windsor]]></category>

		<guid isPermaLink="false">/blogs/ryanc/archive/2009/08/18/injecting-a-wcf-channel-as-dependency-via-windsor.aspx</guid>
		<description><![CDATA[When working within a closed system which uses WCF and an IoC container you will often find the following pattern: &#160; What happens is the SomeAppService is often, but not always, a very thin wrapper over the WCF Service.&#160; If you own both ends of this scenario its often nice to remove the WCF layer [...]]]></description>
			<content:encoded><![CDATA[<p>When working within a closed system which uses WCF and an IoC container you will often find the following pattern:</p>
<p><a href="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/dependency20of.3EAppCode20SomeAppServ.3EWCF20Service20Proxy20SomeAppService20implis20injected20into1_5F00_73E9A3C9.png"><img style="border-bottom: 0px; border-left: 0px; display: inline; border-top: 0px; border-right: 0px" title="dependency%20of-.-%3E[AppCode],%20[SomeAppService%20Impl]delegates%20to-.-%3E[WCF%20Service%20Proxy],%20[SomeAppService%20impl]is%20injected%20into-[1]" border="0" alt="dependency%20of-.-%3E[AppCode],%20[SomeAppService%20Impl]delegates%20to-.-%3E[WCF%20Service%20Proxy],%20[SomeAppService%20impl]is%20injected%20into-[1]" src="http://cromwellhaus.com/cfs-file.ashx/__key/CommunityServer.Blogs.Components.WeblogFiles/ryanc/dependency20of.3EAppCode20SomeA.3EWCF20Service20Proxy20SomeAppService20implis20injected20into1_5F00_thumb_5F00_6A413593.png" width="376" height="296" /></a> </p>
<p>&#160;</p>
<p>What happens is the SomeAppService is often, but not always, a very thin wrapper over the WCF Service.&#160; If you own both ends of this scenario its often nice to remove the WCF layer between.&#160; Maybe you want a different deployment model in some situations such that the WCF service itself can be an in-proc reference.&#160; Other times the front end is in a DMZ and the WCF services needs to be inside the corporate firewall.&#160; Maybe its just easier to run on a dev machine.&#160; Whatever the reason, you don’t always want to be tunneling through WCF.&#160; Plus, its just cool to see it work.</p>
<p>To do this, you can use Windsor’s <a href="http://using.castleproject.org/display/IoC/Fluent+Registration+API" target="_blank">FactorySupportFacility</a> to return a <a href="http://msdn.microsoft.com/en-us/library/aa344730.aspx" target="_blank">ClientBase&lt;T&gt;.Channel</a> as the dependency instance.&#160; </p>
<p>First, you’ll need a little class to expose the ClientBase&lt;T&gt;.Channel since it’s protected:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ServiceClient&lt;T&gt; : ClientBase&lt;T&gt; where T : <span style="color: #0000ff">class</span>
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">{
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    <span style="color: #0000ff">public</span> <span style="color: #0000ff">new</span> T Channel
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    {
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">        <span style="color: #0000ff">get</span> { <span style="color: #0000ff">return</span> <span style="color: #0000ff">base</span>.Channel;}
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    }
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">}</pre>
</pre>
<p>Second, add the FactorySupportFacility to your WindsorContainer:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 600px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container.AddFacility&lt;FactorySupportFacility&gt;();</pre>
</pre>
<p>Lastly, register your implementation as either the WCF channel:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 580px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    .Register(Component.For&lt;ISomeAppService&gt;()
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">                       .UsingFactoryMethod(() =&gt; <span style="color: #0000ff">new</span> ServiceClient&lt;IWCFService&gt;().Channel))
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px"></pre>
</pre>
<p>…or the actual implementation:</p>
<pre style="border-bottom: #cecece 1px solid; border-left: #cecece 1px solid; padding-bottom: 5px; background-color: #fbfbfb; min-height: 40px; padding-left: 5px; width: 580px; padding-right: 5px; overflow: auto; border-top: #cecece 1px solid; border-right: #cecece 1px solid; padding-top: 5px">
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">container
</pre>
<pre style="background-color: #ffffff; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">    .Register(Component.For&lt;ISomeAppService&gt;()
</pre>
<pre style="background-color: #ffffe6; margin: 0em; width: 100%; font-family: consolas,&#39;Courier New&#39;,courier,monospace; font-size: 12px">                  .ImplementedBy&lt;WCFServiceImpl&gt;())</pre>
<p>&#160;</p>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://blog.cromwellhaus.com/2009/08/injecting-a-wcf-channel-as-dependency-via-windsor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

