<?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>Mark Harris &#187; NServiceBus</title>
	<atom:link href="http://www.markharris.net.au/blog/category/nservicebus/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markharris.net.au/blog</link>
	<description>musings on Software Development</description>
	<lastBuildDate>Wed, 19 May 2010 12:58:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Doing Advanced Distributed Systems Design &#8211; A Gift from Santa</title>
		<link>http://www.markharris.net.au/blog/2010/02/04/doing-advanced-distributed-systems-design-a-gift-from-santa/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=doing-advanced-distributed-systems-design-a-gift-from-santa</link>
		<comments>http://www.markharris.net.au/blog/2010/02/04/doing-advanced-distributed-systems-design-a-gift-from-santa/#comments</comments>
		<pubDate>Thu, 04 Feb 2010 13:46:27 +0000</pubDate>
		<dc:creator>Mark Harris</dc:creator>
				<category><![CDATA[Career]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[soa]]></category>
		<category><![CDATA[Training]]></category>

		<guid isPermaLink="false">http://www.markharris.net.au/blog/?p=210</guid>
		<description><![CDATA[It is the second time I have sat through&#160;Udi&#8217;s Advanced Distributed Systems Design. Unlike repeating a course at University, I was like a 6 year old waiting for Santa Claus to arrive with his presents at Christmas. Not only has the course content been refined in some sections, CQRS (In case you missed it you [...]]]></description>
			<content:encoded><![CDATA[<p>It is the second time I have sat through<a href="http://www.udidahan.com/training/">&nbsp;Udi&#8217;s Advanced Distributed Systems Design</a>. Unlike repeating a course at University, I was like a 6 year old waiting for Santa Claus to arrive with his presents at Christmas. Not only has the course content been refined in some sections, CQRS (In case you missed it you can <a href="http://www.markharris.net.au/blog/2010/01/24/video-of-command-query-responsibility-segregation-by-udi-dahan/">watch Udi&#8217;s presentation on CQRS here</a>) for instance. Udi&#8217;s delivery of the material is such that he always has you yearning for more. The first time i did this course it was as though someone had opened my minds eye. Since then I have made so many changes to my thinking and the way I approach development. </p>
<p>My reasons for attending the course again were simple
<ol>
<li>Gauge my progression</li>
<li>Hear other peoples thoughts and opinions</li>
<li>Spend more time at the feet of the master</li>
<li>And of course Learn some more</li>
</ol>
<p>The beauty of this course is that despite the complexity of the architecture and the concepts the delivery makes the comprehension a breeze.&nbsp; <a href="http://www.paulbatum.com/2010/01/advanced-distributed-systems-design.html">Paul Batum</a> and <a href="http://rhysc.blogspot.com/2010/01/udi-dahan-advanced-distributed-systems.html">Rhys Campbell</a> who attended the course have written up their impressions, as has <a href="http://www.simonsegal.net/blog/2010/02/04/adsd-nservicebus-and-nuclear-armament-the-full-story/">Simon</a> and we all agree that it is worth its weight in gold. So if you have thought about ever doing this course and can make it to one do so, but if your not able to go to one of upcoming courses I would strongly suggest <a href="http://www.udidahan.com/contact/">getting in touch with Udi</a> and working other possible solutions with him. Which is why I guess Simon is owed big thanks for setting up this opportunity. </p>
<p>As for NServiceBus and its comparisons to <a href="http://twitter.com/markmario/status/6254304647">Nuclear Armament</a>, I can only say that once you have used it, there is a realization that there is great power that can be wielded without any effort. Till you can try it for yourself see what <a href="http://twitter.com/#search?q=NServiceBus">twitter has to say about NServiceBus</a>. For me the next dose of sharing some NServiceBus love will be at <a href="http://www.markharris.net.au/blog/doing-net-days/">Day 2 of Doing .net Days on the 27th February 2010</a> if you have nothing on that Saturday why not send an <a href="mailto:simon.segal@simonsegal.net">email to register a seat</a> before they run out</p>
<p><a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/friendfeed?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="FriendFeed" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/friendfeed.png" width="16" height="16" alt="FriendFeed"/></a> <a href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2010%2F02%2F04%2Fdoing-advanced-distributed-systems-design-a-gift-from-santa%2F&amp;linkname=Doing%20Advanced%20Distributed%20Systems%20Design%20%26%238211%3B%20A%20Gift%20from%20Santa">Share/Save</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.markharris.net.au/blog/2010/02/04/doing-advanced-distributed-systems-design-a-gift-from-santa/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lessons Learned from building a Linq To Sql Saga Persister</title>
		<link>http://www.markharris.net.au/blog/2009/12/09/lessons-learned-from-building-a-linq-to-sql-saga-persister/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=lessons-learned-from-building-a-linq-to-sql-saga-persister</link>
		<comments>http://www.markharris.net.au/blog/2009/12/09/lessons-learned-from-building-a-linq-to-sql-saga-persister/#comments</comments>
		<pubDate>Tue, 08 Dec 2009 16:28:15 +0000</pubDate>
		<dc:creator>Mark Harris</dc:creator>
				<category><![CDATA[Architecture]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Linq To Sql]]></category>
		<category><![CDATA[saga persister]]></category>

		<guid isPermaLink="false">http://www.markharris.net.au/blog/?p=195</guid>
		<description><![CDATA[Simon has released the first version of the Linq To Sql Saga Persister code and the EF 1 Saga Persister in the past couple of days. In fact we started working on the saga persister mid September, so what took doing something like a Saga Persister, a fairly trivial exercise almost 3 months. To be [...]]]></description>
			<content:encoded><![CDATA[<p>Simon has released the first version of the <a href="http://www.simonsegal.net/blog/2009/12/08/nservicebus-linq-to-sql-saga-persister-part-20/">Linq To Sql Saga Persister</a> code and the <a href="http://www.simonsegal.net/blog/2009/12/09/entity-framework-saga-persister-for-nservicebus/">EF 1 Saga Persister</a> in the past couple of days. In fact we started <a href="http://twitter.com/markmario/status/3992828301">working on the saga persister mid September</a>, so what took doing something like a Saga Persister, a fairly trivial exercise almost 3 months. To be fair Simon initially put some of the <a href="http://www.simonsegal.net/blog/2009/09/22/a-linq-to-sql-saga-persister-for-nservicebus/">work on the persister</a> quite quickly. However once we started to use the code in our test with the Manufacturing sample under load it would begin to creak. Seeing as that was a problem we had to switch to using the NHibernate Saga persister for our client to go into production.</p>
<p>The first thing that went through our minds was that we had written the persister wrong, however a persistent Simon continued to work through the issues. Along the way we discovered a few things.</p>
<ol>
<li>NHibernate is faster and therefore able to handle the load better.</li>
<li>Proper Saga Design is important.</li>
</ol>
<h3>NHibernate is faster</h3>
<p>Most of you already know this but the key thing here is that because its faster it fails less therefore the issues that we saw in the development of the persister was difficult to isolate.</p>
<p>In the Manufacturing Sample that comes with NServiceBus start up all the required services and when the partner is ready run it in simulate mode, it does not fail. doing the same thing when using the L2S persister had the same effect. </p>
<p>Once the number of worker threads on the Order Service Host was increased to 4 could use we started to see a whole bunch of errors. From messages not being completed to violation of primary keys and deadlocks. Using the NHibernate persister required us to increase the workers to 6 or 7 you would start to notice similar errors.This leads me to the next item.</p>
<h3>Proper Saga Design is Important</h3>
<p>If your not sure about what a Saga is please <a href="http://sourceforge.net/apps/mediawiki/nservicebus/index.php?title=Sagas">read the NServiceBus wiki</a>. When considering how you build a system&#160; though that goes into how you would like to handle long running transactions is important. Knowing how many transactions per second. What are the SLA’s? My rule of thumb the more i think about it is that a Saga message that starts the saga should be unique any subsequent messages that are part of that saga should be dealt with a more explicit message handler. So you have a CreateOrderMessage that starts a saga and subsequent messages are handled by UpdateOrderMessages for changes to the order, with CancelOrderMessages and AuthorizeOrderMessages being explicit to the saga. </p>
<p>In my opinion doing so produces both a simpler and more explicit design of your long lived transaction. This however is now going down the path of a different post one that warrants a post of it own.</p>
<p>The <a href="http://graysmatter.codivation.com/post/How-I-Am-Becoming-A-Better-Developer-Part-1-Of-Infinity.aspx">Benefits of Reading code</a> became clear the deeper we went through the NServiceBus code base, the more you understand how each piece interacts with the other to provide a great framework. Despite all the hurdles L2S provided it was worth the effort. What must be said for NServiceBus is that it is highly extensible already evidenced by its existing OOB support for 4 IOC frameworks, now 3 ORM’s and in the future with version 2.1 it will support a whole raft of Queued Transports.</p>
<p><a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/friendfeed?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="FriendFeed" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/friendfeed.png" width="16" height="16" alt="FriendFeed"/></a> <a href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F09%2Flessons-learned-from-building-a-linq-to-sql-saga-persister%2F&amp;linkname=Lessons%20Learned%20from%20building%20a%20Linq%20To%20Sql%20Saga%20Persister">Share/Save</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.markharris.net.au/blog/2009/12/09/lessons-learned-from-building-a-linq-to-sql-saga-persister/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Service Broker &#8211; Some thoughts post hoc</title>
		<link>http://www.markharris.net.au/blog/2009/12/01/service-broker-some-thoughts-post-hoc/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=service-broker-some-thoughts-post-hoc</link>
		<comments>http://www.markharris.net.au/blog/2009/12/01/service-broker-some-thoughts-post-hoc/#comments</comments>
		<pubDate>Mon, 30 Nov 2009 15:22:24 +0000</pubDate>
		<dc:creator>Mark Harris</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[Architecture]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[SQL Server]]></category>
		<category><![CDATA[servicebroker]]></category>

		<guid isPermaLink="false">http://www.markharris.net.au/blog/?p=181</guid>
		<description><![CDATA[This post has been sitting in my drafts for a while. After my day today i felt that it was best that i put my thoughts about service broker down. In a project that i worked on about 2 years ago Service Broker was used as part of the system to perform data validation.&#160; If [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.markharris.net.au/blog/wp-content/uploads/2009/12/servicebroker.gif"><img style="border-bottom: 0px; border-left: 0px; display: inline; margin-left: 0px; border-top: 0px; margin-right: 0px; border-right: 0px" title="service broker" border="0" alt="sql service broker" align="left" src="http://www.markharris.net.au/blog/wp-content/uploads/2009/12/servicebroker_thumb.gif" width="178" height="167" /></a> </p>
<p>This post has been sitting in my drafts for a while. After my day today i felt that it was best that i put my thoughts about service broker down.    <br />In a project that i worked on about 2 years ago Service Broker was used as part of the system to perform data validation.&#160; If success was measured by the amount of time that a system has been in production without major issues then i can say that it was successful. </p>
<p>But if a system was to be judged by how much of a pain it is to maintain then l must say that its was not a <strike>glowing</strike> success. Below is a list of things that i found as good or bad about it. </p>
<p><strong>&#160;</strong></p>
<p><strong>Bad things</strong></p>
<ol>
<li>Limited information on the internet </li>
<li>Testing is extremely hard (compromises everywhere) </li>
<li>Troubleshooting is a pain </li>
<li>It could stop working and you may never know, (if using a fire and forget mechanism) </li>
<li>Poison messages disable queues (would have been nicer if it moved the message to a error queue) </li>
</ol>
<p><strong>Good things</strong></p>
<ol>
<li>Its Asynchronous </li>
<li>Its Asynchronous </li>
<li>Its Asynchronous </li>
</ol>
<p>Yes this list is one sided but true. The question that is important though is</p>
<p><strong>Would i use Service Broker again?</strong></p>
<p>The answer to that question is strangely YES. Only in very specific circumstances where system is ok with not having some data loss because of queue being disabled</p>
<p><strong>What would I use instead?</strong></p>
<p><strong><a href="http://www.nservicebus.com/" rel="tag">NServiceBus</a> </strong>of course. It provides an answer to all the negatives of service broker and is still asynchronous. </p>
<p><a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/friendfeed?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="FriendFeed" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/friendfeed.png" width="16" height="16" alt="FriendFeed"/></a> <a href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F12%2F01%2Fservice-broker-some-thoughts-post-hoc%2F&amp;linkname=Service%20Broker%20%26%238211%3B%20Some%20thoughts%20post%20hoc">Share/Save</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.markharris.net.au/blog/2009/12/01/service-broker-some-thoughts-post-hoc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Testing Message Handlers with NServiceBus</title>
		<link>http://www.markharris.net.au/blog/2009/09/27/testing-message-handlers-with-nservicebus/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=testing-message-handlers-with-nservicebus</link>
		<comments>http://www.markharris.net.au/blog/2009/09/27/testing-message-handlers-with-nservicebus/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 10:12:00 +0000</pubDate>
		<dc:creator>Mark Harris</dc:creator>
				<category><![CDATA[.net]]></category>
		<category><![CDATA[NServiceBus]]></category>
		<category><![CDATA[Unit Testing]]></category>
		<category><![CDATA[Rhino Mocks]]></category>

		<guid isPermaLink="false">http://www.markharris.net.au/blog/?p=110</guid>
		<description><![CDATA[One of the great things that NServiceBus ships with is a dll called “NServiceBus.Testing” which provides us with the ability to easily test your sagas. This enables us to move forward with greater confidence in knowing that the code that we have written will function as expected when we move the code into integration testing [...]]]></description>
			<content:encoded><![CDATA[<p>One of the great things that <a href="http://www.nservicebus.com/" rel="tag">NServiceBus</a> ships with is a dll called “NServiceBus.Testing” which provides us with the ability to easily test your <a href="http://www.udidahan.com/2009/04/20/saga-persistence-and-event-driven-architectures/" target="_blank">sagas</a>. This enables us to move forward with greater confidence in knowing that the code that we have written will function as expected when we move the code into integration testing and then production.&#160; Most of what i talk about here is in direct reference the <a href="http://www.udidahan.com/2009/02/07/nservicebus-19/" target="_blank">1.9 release of NServiceBus</a>. If you have not seen how Saga’s are unit tested in NServiceBus have a read of <a href="http://tech.groups.yahoo.com/group/nservicebus/message/208" target="_blank">Udi’s post on the forum</a>. This post however is about how to test a Plain old Message Handler.</p>
<p>Let us being with looking at a sample handler.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; height: 234px; max-height: 250px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> FileProcessMessageHandler:IMessageHandler&lt;ProcessFileMessage&gt;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span>     <span style="color: #0000ff">public</span> IBus Bus { get; set; }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     <span style="color: #0000ff">public</span> IFileProcessor FileProcessor {get;set;}</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> Handle(ProcessFileMessage message)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>         <span style="color: #0000ff">if</span> (FileProcessor.Run(message.FileIdentfier,message.FileSystemLocation))</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>             var msg = Bus.CreateInstance&lt;FileProcessedMessage&gt;(m =&gt; {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>                                  m.FileIdentifier = message.FileIdentfier; });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>             Bus.Publish(msg);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>         <span style="color: #0000ff">else</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>         {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>             Bus.Publish&lt;UnknownFileTypeMessage&gt;(m =&gt; { </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>                                 m.FileIdentifer = message.FileIdentfier; });</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>         }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19">  19:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>This code in and of itself is highly testable. We have two interfaces that are injected into the class, excellent candidates for a mock. However when i first started down this path my first inclination was to use a manual mock, before moving on to trying to use Moq, however after spending time the fluent saga testing API in NServiceBus I realized that it was what was needed in this situation a MockBus.</p>
<p>MockBus is essentially the same as what is in the NServiceBus.Testing library. However because that version is geared towards testing sagas, it required changing the way the Saga class was created, and to allow for creating a version that would work with message handler. The new structure looks like the class diagram below.</p>
<p>&#160;</p>
<p><a href="http://www.markharris.net.au/blog/wp-content/uploads/2009/09/NServiceBusTestingClassDiagram.png" target="_blank"><img style="border-bottom: 0px; border-left: 0px; display: block; float: none; margin-left: auto; border-top: 0px; margin-right: auto; border-right: 0px" title="NServiceBus Testing Class Diagram" border="0" alt="NServiceBus Testing Class Diagram" src="http://www.markharris.net.au/blog/wp-content/uploads/2009/09/NServiceBusTestingClassDiagram_thumb.png" width="463" height="398" /></a></p>
<p>&#160;</p>
<p>What Changed</p>
<ol>
<li>The Saga class has become the MockBus class and no longer has the static method called Test() which created the Saga instance.</li>
<li>The MockBusForSaga has the method CreateInstance&lt;T&gt; which replaces the Test() method in the old Saga class.</li>
<li>Introduced a MockBusForMessageHandler which has a CreateInstance&lt;T,TK&gt;() method that creates the message handler.</li>
</ol>
<p>Having this structure now enables us to write our tests for our message handlers very simply in a fashion that does not require to understand Rhino Mocks, though having a understanding of the errors you get from the mock bus is important as all the messages that are bubbled out are Rhino Mock Messages.</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> [Test]</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> when_file_processor_succeds_message_handler_will_publish_a_file_processed_message()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span>     FileProcessMessageHandler msgHandler;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     var mock_bus = MockBusForMessageHandler.CreateInstance</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>                             &lt;FileProcessMessageHandler,ProcessFileMessage&gt;(<span style="color: #0000ff">out</span> msgHandler);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     var fileID = Guid.NewGuid();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     msgHandler.FileProcessor = SetupAndReturnFileProcessor(<span style="color: #0000ff">true</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     mock_bus.ExpectPublish&lt;FileProcessedMessage&gt;(d =&gt; d.FileIdentifier == fileID)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         .When(() =&gt; msgHandler.Handle(CreateProcessFileRequest(fileID)));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>Lets have a look&#160; when make a call to CreateInstance&lt;FileProcessMessageHandler,ProcessFileMessage&gt;(out msgHandler)</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1">   1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">static</span> MockBusForMessageHandler CreateInstance&lt;T, TK&gt;(<span style="color: #0000ff">out</span> T handler)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2">   2:</span> <span style="color: #0000ff">where</span> TK : IMessage</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3">   3:</span> <span style="color: #0000ff">where</span> T : IMessageHandler&lt;TK&gt;, <span style="color: #0000ff">new</span>()</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4">   4:</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5">   5:</span>     handler = (T) Activator.CreateInstance(<span style="color: #0000ff">typeof</span> (T));</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6">   6:</span>     var mocks = <span style="color: #0000ff">new</span> MockRepository();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7">   7:</span>     var bus = mocks.DynamicMock&lt;IBus&gt;();</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8">   8:</span>     <span style="color: #0000ff">try</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9">   9:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10">  10:</span>         <span style="color: #0000ff">typeof</span> (T).GetProperty(“Bus”, BindingFlags.Instance | BindingFlags.Public)</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11">  11:</span>             .SetValue(handler, bus, <span style="color: #0000ff">null</span>);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12">  12:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13">  13:</span>     <span style="color: #0000ff">catch</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14">  14:</span>     {<span style="color: #008000">/**swallow if they dont have a Bus defined in the class**/</span>}</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15">  15:</span>     List&lt;Type&gt; typesToMap;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16">  16:</span>     MessageMapper mapper = GetMapperAndInitialize(<span style="color: #0000ff">out</span> typesToMap);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17">  17:</span>     <span style="color: #0000ff">return</span> <span style="color: #0000ff">new</span> MockBusForMessageHandler(mocks, bus, mapper, typesToMap);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &quot;Courier New&quot;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18">  18:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>The beauty in this code is all hidden away in the MockBus (aka NServiceBus.Testing.Saga) thank you <a href="http://www.udidahan.com">Udi</a> for this,</p>
<p>The bus is instantiated when the object is created however no expectations are recorded until the When() method is called in the test just before the delegate is run. Once the delegate is run you will get the callbacks to the ExpectPublish where you can assert that not only did you get the correct type of message but the contents as well.</p>
<p>The code for this is available on the <a href="http://www.markharris.net.au/blog/downloads/" rel="tag" target="_blank">downloads page</a> or go to it <a href="http://www.markharris.net.au/blog/downloads/?did=1" target="_blank">NServiceBus.Extensions.Testing download here</a></p>
<p><a href="http://www.addtoany.com/add_to/delicious?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="Delicious" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/delicious.png" width="16" height="16" alt="Delicious"/></a> <a href="http://www.addtoany.com/add_to/google_reader?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="Google Reader" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/reader.png" width="16" height="16" alt="Google Reader"/></a> <a href="http://www.addtoany.com/add_to/twitter?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="Twitter" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/twitter.png" width="16" height="16" alt="Twitter"/></a> <a href="http://www.addtoany.com/add_to/facebook?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="Facebook" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/facebook.png" width="16" height="16" alt="Facebook"/></a> <a href="http://www.addtoany.com/add_to/friendfeed?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="FriendFeed" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/friendfeed.png" width="16" height="16" alt="FriendFeed"/></a> <a href="http://www.addtoany.com/add_to/linkedin?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus" title="LinkedIn" rel="nofollow" target="_blank"><img src="http://www.markharris.net.au/blog/wp-content/plugins/add-to-any/icons/linkedin.png" width="16" height="16" alt="LinkedIn"/></a> <a class="a2a_dd addtoany_share_save" href="http://www.addtoany.com/share_save?linkurl=http%3A%2F%2Fwww.markharris.net.au%2Fblog%2F2009%2F09%2F27%2Ftesting-message-handlers-with-nservicebus%2F&amp;linkname=Testing%20Message%20Handlers%20with%20NServiceBus">Share/Save</a> </p>]]></content:encoded>
			<wfw:commentRss>http://www.markharris.net.au/blog/2009/09/27/testing-message-handlers-with-nservicebus/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
