<?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/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>Valeriu Caraulean &#187; NServiceBus</title>
	<atom:link href="http://blog.caraulean.com/category/nservicebus/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.caraulean.com</link>
	<description>Keep it simple. It will work...</description>
	<lastBuildDate>Fri, 10 Feb 2012 04:38:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='blog.caraulean.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>Valeriu Caraulean &#187; NServiceBus</title>
		<link>http://blog.caraulean.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://blog.caraulean.com/osd.xml" title="Valeriu Caraulean" />
	<atom:link rel='hub' href='http://blog.caraulean.com/?pushpress=hub'/>
		<item>
		<title>Configuring NServiceBus using Binsor</title>
		<link>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/</link>
		<comments>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/#comments</comments>
		<pubDate>Sun, 13 Jul 2008 20:58:29 +0000</pubDate>
		<dc:creator>Valeriu Caraulean</dc:creator>
				<category><![CDATA[Castle]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[NServiceBus]]></category>

		<guid isPermaLink="false">http://varely.wordpress.com/2008/07/13/configuring-nservicebus-using-binsor/</guid>
		<description><![CDATA[I&#8217;m evaluating the NServiceBus communication framework for using it in some parts of application that we&#8217;re working on. I like the messaging infrastructure that it offers and how it all is implemented &#8211; behind a simple interface is hidden a modular, extensible, reliable and extremely powerful messaging engine. Sure, like any other framework, it has [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=23&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m evaluating the <a href="http://nservicebus.com/">NServiceBus</a> communication framework for using it in some parts of application that we&#8217;re working on. I like the messaging infrastructure that it offers and how it all is implemented &#8211; behind a simple interface is hidden a modular, extensible, reliable and extremely powerful messaging engine. Sure, like any other framework, it has his own pitfalls and places that requires a special knowledge about how things works to use it efficiently. </p>
<p>One of those tricky places is his configuration system. To start use NServiceBus you have to do few steps to properly configure it. You have to use 2-3 &#8220;app.config&#8221; configuration sections with approximately 10-15 various parameters and few configuration classes provided by NServiceBus. </p>
<p>Another place that make me stuck is his integration with IoC (Inversion of Control) container. It has an abstraction over all container stuff, so you can pretty easily create an adapter to use it with container of your choice. I don&#8217;t like how this abstraction is used and implemented, a bit unnatural for my habits on how to put container to work.</p>
<p>Anyway, we have to deal with it, and use the NServiceBus with Castle Windsor container. As I said to do that you have to create an adapter for IBuilder interface. The NServiceBus Contrib project contains a patch that do this, but it is done in a completely wrong way &#8211; by mimicking the adapter for Spring framework. Bad, bad, bad&#8230;</p>
<p>So let&#8217;s take our big gun &#8211; Binsor from Rhino.Tools (I wrote about <a href="http://blog.caraulean.com/2008/05/14/configuring-nhibernateintegration-facility-with-binsor/">here</a>) that should do all dirty work by configuring NServiceBus and integrating it in our existing Windsor container. The link to VS solution with all code shown here is provided at the end of the page.</p>
<p>Here we go &#8211; how to configure a subscriber for Pub/Sub interactions:</p>
<p>1. Binsor configuration (.boo file):</p>
<pre class="code">import System.Reflection
import NServiceBus
import NServiceBus.Unicast
import NServiceBus.Serialization
import NServiceBus.Serializers.Binary
import NServiceBus.Unicast.Transport
import NServiceBus.Unicast.Transport.Msmq
import NServiceBus.Unicast.Subscriptions
import NServiceBus.Unicast.Subscriptions.Msmq

import ObjectBuilder
import nServiceBus.CastleIntegration

component IBus, NServiceBus.Unicast.UnicastBus:
    MessageOwners = {"Messages":"messagebus"}
    MessageHandlerAssemblies = [Assembly.Load("Subscriber1")]

component ITransport, MsmqTransport:
    InputQueue = "worker"
    ErrorQueue = "error"
    NumberOfWorkerThreads = 1
    MaxRetries = 5
    IsTransactional = false
    PurgeOnStartup = false

component IMessageSerializer, MessageSerializer
component IBuilder, Builder
</pre>
<p><a href="http://11011.net/software/vspaste"></a></p>
<p><a href="http://11011.net/software/vspaste"></a>&nbsp;</p>
<p>2. Program initialization:</p>
<pre class="code"><span style="color:blue;">var </span>container = <span style="color:blue;">new </span><span style="color:#2b91af;">WindsorContainer</span>();

<span style="color:#2b91af;">BooReader</span>.Read(container, <span style="color:#a31515;">"nServiceBus.boo"</span>);

container.Register(<span style="color:#2b91af;">SagasAndMessageHandlers</span>.From(<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">EventMessageHandler</span>).Assembly));

<span style="color:blue;">var </span>bus = container.Resolve&lt;<span style="color:#2b91af;">IBus</span>&gt;();

bus.Start();
</pre>
<p><a href="http://11011.net/software/vspaste"></a>&nbsp;</p>
<p>That&#8217;s all. Now you can start send/receive messages.</p>
<p>For my taste this is much more readable and maintainable than traditional NServiceBus configuration. Less code, no XML, much cleaner, container friendly.</p>
<p>To make this work you have need for adapter class itself. His responsibility is to forward IBuilder.Build&lt;T&gt; calls to Castle&#8217;s Resolve&lt;T&gt; methods. Also, a small helper class that will register Sagas and MessageHandlers in container. Here it is:</p>
<pre class="code"><span style="color:blue;">public static class </span><span style="color:#2b91af;">SagasAndMessageHandlers
</span>{
    <span style="color:blue;">public static </span><span style="color:#2b91af;">IRegistration</span>[] From(<span style="color:blue;">params </span><span style="color:#2b91af;">Assembly</span>[] assemblies)
    {
        <span style="color:blue;">var </span>registrations = <span style="color:blue;">new </span><span style="color:#2b91af;">List</span>&lt;<span style="color:#2b91af;">ComponentRegistration</span>&gt;();
        <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>assembly <span style="color:blue;">in </span>assemblies)
        {
            <span style="color:blue;">var </span>types = assembly.GetTypes();
            <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>type <span style="color:blue;">in </span>types)
            {
                <span style="color:blue;">var </span>implementedInterfaces = type.GetInterfaces();
                <span style="color:blue;">foreach </span>(<span style="color:blue;">var </span>interf <span style="color:blue;">in </span>implementedInterfaces)
                {
                    <span style="color:blue;">if </span>(!interf.IsGenericType)
                        <span style="color:blue;">continue</span>;
                    <span style="color:blue;">var </span>genericArguments = interf.GetGenericArguments();
                    <span style="color:blue;">if </span>(genericArguments.Length != 1)
                        <span style="color:blue;">continue</span>;

                    <span style="color:blue;">if </span>(!<span style="color:blue;">typeof</span>(<span style="color:#2b91af;">IMessage</span>).IsAssignableFrom(genericArguments[0]))
                        <span style="color:blue;">continue</span>;

                    <span style="color:blue;">var </span>sagaHandlerType = <span style="color:blue;">typeof</span>(<span style="color:#2b91af;">ISaga</span>&lt;&gt;).MakeGenericType(genericArguments[0]);
                    <span style="color:blue;">if </span>(sagaHandlerType.IsAssignableFrom(interf))
                    {
                        registrations.Add(CreateRegistration(type, sagaHandlerType));
                        registrations.Add(CreateRegistration(type, type));
                    }

                    <span style="color:blue;">var </span>messageHandlerType = <span style="color:blue;">typeof </span>(<span style="color:#2b91af;">IMessageHandler</span>&lt;&gt;).MakeGenericType(genericArguments[0]);
                    <span style="color:blue;">if </span>(messageHandlerType.IsAssignableFrom(interf))
                        registrations.Add(CreateRegistration(type, type));
                }
            }
        }
        <span style="color:blue;">return </span>registrations.ToArray();
    }

    <span style="color:blue;">private static </span><span style="color:#2b91af;">ComponentRegistration </span>CreateRegistration(<span style="color:#2b91af;">Type </span>type, <span style="color:#2b91af;">Type </span>handlerType)
    {
        <span style="color:blue;">string </span>componentName;
        <span style="color:blue;">if </span>(type == handlerType)
            componentName = type.FullName;
        <span style="color:blue;">else
            </span>componentName = type.FullName + handlerType.FullName;

        <span style="color:blue;">return </span>(<span style="color:#2b91af;">ComponentRegistration</span>)<span style="color:#2b91af;">Component
                                          </span>.For(handlerType)
                                          .ImplementedBy(type)
                                          .LifeStyle.Is(<span style="color:#2b91af;">LifestyleType</span>.Transient)
                                          .Named(componentName);
    }
}
</pre>
<p>&nbsp;</p>
<p>One thing to notice here, NServiceBus required that a saga to be registered as a saga handler ( ISaga&lt;Message&gt;) and as a Saga class itself. A better way will be to use Castle&#8217;s newly added forwarding ability, when a component implementation can be accessed through multiple interfaces all forwarded to component&#8217;s instance. Bud I had no time to play with this feature yet, so this duplication will still here some time given the fact that no bugs showed up.</p>
<p>You can download a Visual Studio solution with all code shown here from <a href="http://cid-dcef5f73996bad79.skydrive.live.com/self.aspx/Blog/nServiceBus.CastleIntegration.zip">this place</a>. It contains all infrastructure code and two working sample projects ported from original NServiceBus samples &#8211; Pub/Sub &amp; Saga.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/varely.wordpress.com/23/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/varely.wordpress.com/23/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/varely.wordpress.com/23/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/varely.wordpress.com/23/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/varely.wordpress.com/23/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=blog.caraulean.com&amp;blog=1485392&amp;post=23&amp;subd=varely&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://blog.caraulean.com/2008/07/13/configuring-nservicebus-using-binsor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5d96a7814c830eb5ee8e12b677212cae?s=96&#38;d=http%3A%2F%2F1.gravatar.com%2Favatar%2Fad516503a11cd5ca435acc9bb6523536%3Fs%3D96" medium="image">
			<media:title type="html">varely</media:title>
		</media:content>
	</item>
	</channel>
</rss>
