<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://www.lostechies.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>PhatBoyG (Chris Patterson)</title><link>http://www.lostechies.com/blogs/chris_patterson/default.aspx</link><description>Coding from the Hood since &amp;#39;87</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 (Build: 30929.2835)</generator><item><title>Leveraging Windows 7 Boot to VHD to Test Visual Studio 2010</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2010/01/01/leveraging-windows-7-boot-to-vhd-to-test-visual-studio-2010.aspx</link><pubDate>Sat, 02 Jan 2010 00:11:01 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:45166</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=45166</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2010/01/01/leveraging-windows-7-boot-to-vhd-to-test-visual-studio-2010.aspx#comments</comments><description>&lt;p&gt;
The goal of this article is simple: a clean installation of Windows 7 that can be used to test new versions of Visual Studio 2010 until we reach RTM. Since a new beta drops every couple of months (and maybe even an interim release or two in between for those lucky enough to get access to it), I don't want to have to repave the machine for each new release. Since Windows 7 has the ability to boot into a VHD, I thought this would be the perfect opportunity to try this functionality myself.
&lt;/p&gt;
&lt;h3&gt;Getting Started&lt;/h3&gt;
&lt;p&gt;
The first thing I did was slide the Windows 7 DVD into the drive and rebooted the machine. Once the setup window appeared, I pressed Shift+F1 to open a command prompt and created a new VHD by typing the commands shown below.
&lt;/p&gt;
&lt;tt&gt;&lt;br /&gt;
diskpart&lt;br /&gt;
create vdisk file="d:\vhds\w7base.vhd" type=expandable maximum=80000&lt;br /&gt;
select vdisk file="d:\vhds\w7base.vhd"&lt;br /&gt;
attach vdisk&lt;br /&gt;
&lt;/tt&gt;
&lt;/p&gt;&lt;p&gt;
Once this was finished, I switched back to the setup window and clicked Install Now. I chose the advanced install option, after which I selected my newly created partition as the installation target and formatted it so the installation could continue. While waiting for the setup process to complete (which included at least two reboots, both of which properly loaded into the VHD without any issues, so that's cool), I started working on this blog post.
&lt;/p&gt;&lt;p&gt;
After the install completed, I wanted to make sure I didn't get confused by the boot manager now having two "Windows 7" options at started. So I quickly opened a command prompt (as Administrator), and typed:
&lt;/p&gt;
&lt;tt&gt;&lt;br /&gt;
bcdedit /set description "Windows 7 Baseline"&lt;br /&gt;
&lt;/tt&gt;
&lt;/p&gt;&lt;p&gt;
This will change the name of the boot manager entry for the current machine to "Windows 7 Baseline," distinguishing it from the normal Windows 7 install. Once I've completed all the setup tasks, I'll set the regular Windows 7 option to be the default when I'm not working with the beta software installations.
&lt;/p&gt;&lt;p&gt;
Next I ran Windows Update, along with installing the &lt;a href="http://www.microsoft.com/Security_Essentials"&gt;Microsoft Security Essentials&lt;/a&gt; to have a basic level of virus/malware protection.
&lt;/p&gt;&lt;p&gt;
I almost always need a database server to do any sort of distributed application testing, so I wanted to have a SQL Server 2008 available. However, I didn't want to have it setup on each virtual disk. So I installed it to the base installation, along with a few other tools that I use often (and are not really updated regularly). I also decided that since Visual Studio 2010 and Visual Studio 2008 run nicely side-by-side that I would go ahead and install Visual Studio 2008 on the base image since I'll most likely be working primarily with 2008 through the next year.
&lt;/p&gt;&lt;p&gt;
When you boot from a VHD, your regular Windows system drive is available as drive D. This is really useful since you can keep a single checkout of all your projects and work on them from different systems! On my regular Windows 7 install, I created a "Shared" folder off the root (OMG not a Library?). Within that folder I have a Home folder which I use for SVN checkouts, and I also configured SQL Server to use a directory under Shared for all the data storage. This will be handy for ensuring that data in the database is updated regardless of which system is using SQL - very cool.
&lt;/p&gt;
&lt;h3&gt;Making a Difference&lt;/h3&gt;
&lt;p&gt;
With the above steps behind us, we now have a machine with two Windows 7 installations on it. The first, a regular disk-based install, is our primary installation that we use for whatever. The second, a VHD-based install, is our clean Windows 7 with our minimum set of software installed on it. It's this clean version that we want to keep "pristine" to avoid having to redo all the steps above with each new release of Visual Studio 2010.
&lt;/p&gt;&lt;p&gt;
&lt;em&gt;A quick note, the size of the VHD after all the software is installed is about 20 gigabytes, which is pretty substantial considering that we now don't need to copy this space for each branch of the baseline we create -- a nice savings!&lt;/em&gt;
&lt;/p&gt;&lt;p&gt;
To create the differential image, I restarted the machine and selected my regular disk-based Windows 7 install. Once it was loaded, I opened up a command prompt and created a new VHD based off the existing one I just finished creating.
&lt;/p&gt;
&lt;tt&gt;&lt;br /&gt;
diskpart&lt;br /&gt;
create vdisk file="D:\vhds\w7-2008.vhd" parent="D:\vhds\w7base.vhd"&lt;br /&gt;
create vdisk file="D:\vhds\w7-2010.vhd" parent="D:\vhds\w7base.vhd"&lt;br /&gt;
&lt;/tt&gt;
&lt;/p&gt;
&lt;p&gt;Once the partition is created, create a boot entry for it using:
&lt;/p&gt;
&lt;tt&gt;&lt;br /&gt;
bcdedit /v &lt;em&gt;(this displays the GUID we'll use next for our copy)&lt;/em&gt;&lt;br /&gt;
bcdedit /copy {3F2504E&lt;strong&gt;0&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} /d "Windows 7 - 2008"&lt;br /&gt;
bcdedit /v &lt;em&gt;(to get the GUID of our copy, which is also displayed after the copy command as well)&lt;/em&gt;&lt;br /&gt;
bcdedit /set {3F2504E&lt;strong&gt;1&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} device vhd="[C:]\vhds\w7-2008.vhd"&lt;br /&gt;
bcdedit /set {3F2504E&lt;strong&gt;1&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} osdevice vhd="[C:]\vhds\w7-2008.vhd"&lt;br /&gt;
bcdedit /v &lt;em&gt;(to verify our work)&lt;/em&gt;&lt;br /&gt;
bcdedit /set {3F2504E&lt;strong&gt;2&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} device vhd="[C:]\vhds\w7-2010.vhd"&lt;br /&gt;
bcdedit /set {3F2504E&lt;strong&gt;2&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} osdevice vhd="[C:]\vhds\w7-2010.vhd"&lt;br /&gt;
bcdedit /v &lt;em&gt;(again, to verify our work)&lt;/em&gt;&lt;br /&gt;
bcdedit /displayorder {current} {3F2504E&lt;strong&gt;1&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} {3F2504E&lt;strong&gt;2&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301}&lt;br /&gt;
&lt;/tt&gt;
In this case, {3F2504E&lt;strong&gt;0&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} is my original VHD-based install, {3F2504E&lt;strong&gt;1&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} is my copy for Visual Studio 2008, and {3F2504E&lt;strong&gt;2&lt;/strong&gt;-4F89-11D3-9A0C-0305E82C3301} is my copy for Visual Studio 2010.
&lt;/p&gt;
&lt;p&gt;
I now have three boot options when the system starts, my disk-based Windows 7 install, and my two newly created "differential" installs -- one for Visual Studio 2008 and one for Visual Studio 2010. Since both of these are based off the VHD we created above, we want to make sure we don't accidentally boot into the w7base VHD and change it -- that would be like crossing the streams in Ghostbusters (read: bad). The /displayorder command takes care of this by having a side effect of removing the Windows 7 Baseline option from the list of boot configurations.
&lt;/p&gt;&lt;p&gt;
One thing to keep in mind, and another benefit of having the physical disk drive available as drive D, is that I do all of my work on the D drive (in the Shared folder). Since this drive is persistent I can be sure that I don't lose my files as I boot between different installations of Windows 7. It also makes it so I don't have to checkout projects on each VHD, which would waste valuable space.
&lt;/p&gt;
&lt;h3&gt;Cleaning Up&lt;/h3&gt;
&lt;p&gt;
So now that we have a nice baseline and a couple of working boot options (both of which don't impact each other as changes are made), we can install our latest BETA/CTP/whatever on our 2010 install. When an update is released (in the form of a subsequent CTP, beta, etc.), we can take a few simple steps to revert our 2010 to a clean install.
&lt;/p&gt;&lt;p&gt;
First, we need to boot into our disk-based installation of Windows 7. Once there, there are a couple of options available. We could create an entirely new configuration by repeating the steps above. This would be helpful if we were in the middle of something on a current version of 2010 and wanted to try it in the update to check for breaking changes. The more likely option, however, is to just replace the installation with a clean baseline to install fresh.
&lt;/p&gt;&lt;p&gt;
To install fresh, we can just delete the existing w7-2010.vhd and create it again using:
&lt;/p&gt;
&lt;tt&gt;&lt;br /&gt;
diskpart&lt;br /&gt;
create vdisk file="D:\vhds\v7-2010.vhd" parent="D:\vhds\w7base.vhd"&lt;br /&gt;
&lt;/tt&gt;
The boot configuration is still pointing to that file, so it will find it after a restart and you can install the latest update without fear of some bad configuration/files from a previous beta/CTP getting in the way.
&lt;/p&gt;
&lt;h3&gt;Conclusion&lt;/h3&gt;
&lt;p&gt;
The new boot to VHD features of Windows 7 (which also work just fine for Windows 2008 Server R2 by the way) are a really slick addition to the Windows platform. Being able to take advantage of the VHD features, differential disks in particular, really makes it a great host for the various incarnations of Visual Studio. I hope this guide has helped ease some of the CTP/BETA pain that comes with running on the bleeding edge.
&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Leveraging+Windows+7+Boot+to+VHD+to+Test+Visual+Studio+2010&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2010%2f01%2f01%2fleveraging-windows-7-boot-to-vhd-to-test-visual-studio-2010.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2010%2f01%2f01%2fleveraging-windows-7-boot-to-vhd-to-test-visual-studio-2010.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=45166" width="1" height="1"&gt;</description></item><item><title>Traveling to Technical Events</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/12/23/traveling-to-technical-events.aspx</link><pubDate>Wed, 23 Dec 2009 16:49:20 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:42016</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>2</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=42016</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/12/23/traveling-to-technical-events.aspx#comments</comments><description>&lt;p&gt;Over the past couple of years I have been traveling more, both for work and to attend and/or present at a variety of technical events. While I have yet to earn the esteemed grizzled road veteran merit badge, I have managed to learn a few things along the way. When presenting at a technical event, whether it is a user group meeting, a code camp, or a full blown conference, being prepared is critical to delivering a successful presentation (hey, you aren&amp;rsquo;t paying for this advice, so don&amp;rsquo;t expect it to be groundbreaking).&lt;/p&gt;
&lt;p&gt;In this post, I would like to share with you a few things that I have found that can make the difference between a good event and a great event, at least as far as your presentation is concerned.&lt;/p&gt;
&lt;h3&gt;Before You Leave (a.k.a. Pack Lightly, But Carry An Armored Tank Division)&lt;/h3&gt;
&lt;p&gt;If you pack a multiple outlet power strip, you will make friends. Power outlets are like gold at any technical event and people will covet them to charge up their power hungry devices. If you encounter a full tap and plan to stay for more than a few minutes, pull it out and share the juice with your five new friends. I have a very compact one that turns 2 outlets into 6, as well as a slightly larger unit that provides 3-to-1 expansion along with 2 USB ports for charging iPhone, iPod, or other USB-powered devices.&lt;/p&gt;
&lt;p&gt;If you will be driving during any part of the trip, pack a car charger for your devices. Small and compact usually wins, so I prefer something that gives me at least two USB connections from the 12V plug. That is usually enough to charge my phone and headset, and can help charge a friends phone at the same time (since they likely forgot their charger unless you forwarded them a link to this article).&lt;/p&gt;
&lt;p&gt;Don&amp;rsquo;t forget to bring plenty of cables. I always pack at least one 14-foot network cable, two USB-to-mini-USB cables, one USB-to-micro-USB cable, one firewire 800 cable, and a 800/400 adapter. The power supply for your laptop is pretty important too, so bring that long as well.&lt;/p&gt;
&lt;p&gt;Bring an additional mobile phone battery or a battery powered top-up charger to get you through the trip. Nothing drains an iPhone like watching a movie during the flight, firing up the GPS to find your way to the hotel, checking your email and flight status, etc. I bought an inexpensive unit from Monoprice (less than $20) that connects to the bottom and tops off an iPhone that is under 20%. It has another dock connector on the bottom and charges from the same cable as the iPhone.&lt;/p&gt;
&lt;p&gt;Scope out your network availability and plan accordingly. If you are traveling alone and cannot tether your phone to your laptop, make sure wireless networking is available. If it is not available, be sure to have all the software you need installed and make sure your demo doesn&amp;rsquo;t require an internet connection. If you are traveling with others and the hotel charges for internet, considering bringing along a wireless router (the Airport Express from Apple is small and easy to setup) to share the connection with your friends!&lt;/p&gt;
&lt;p&gt;A quick note about travel gear - carry on only. Do not check a bag, it will only slow you down. If last minute flight changes are needed when travel issues occur, being quick and unencumbered by checked baggage will help you out. Also, the 22&amp;rdquo; roller bags will not fit in smaller aircraft, so get yourself a smaller 17&amp;rdquo; one that fits under the seat (and in the overhead on the smaller regional jets). You can easily make a two day trip with a smaller bag, particularly if you avoid different styles of clothes.&lt;/p&gt;
&lt;h3&gt;En Route (cue Vacation theme music)&lt;/h3&gt;
&lt;p&gt;First things first &amp;mdash; if you don&amp;rsquo;t have an iPhone (or some other type of reliable smartphone), get one. I have found that having an always available internet connection, full web browser, instant email access, real-time flight tracking, and live Google maps with GPS to be the most important tools when traveling. &lt;/p&gt;
&lt;p&gt;Now that you have your iPhone, some great apps that will really help your trip are listed below.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;AutoPilot from USA TODAY is a great, free application for managing your travel. It&amp;rsquo;s not the fastest or best, but it is free and includes TripIt integration for loading your flight information.&lt;/li&gt;
&lt;li&gt;Flight Update Pro is a air travel focused app that is highly accurate in keeping you up-to-date on flight status and arrival/departure times. While expensive ($9.99), it also includes TripIt integration (saving you data entry) and an easy to read status. &lt;/li&gt;
&lt;li&gt;The FlightCaster web is fairly accurate at predicting delays, and they also have an iPhone application. Again, integration with TripIt eliminates the duplicate entry of information.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;By the way, if you aren&amp;rsquo;t using TripIt to consolidate all of your booking confirmations into a single site yet &amp;mdash; do it. TripIt can parse the details from most booking confirmation emails and automatically updates your travel calendar (which you can subscribe to using iCal). I setup a filter in Gmail to forward my confirmation emails to plans@tripit.com, which parses the message and updates your travel calendar.&lt;/p&gt;
&lt;p&gt;And hopefully this goes without saying, but when traveling &amp;mdash; particularly by air &amp;mdash; dress respectable, be courteous, and don&amp;rsquo;t lose your temper. Things happen during travel &amp;mdash; delayed flights, road construction, missed exits, engine fires &amp;mdash; most of these are out of your control. The last thing you want to do is be an ass to a person already under pressure. Being polite can mean the difference between getting the last seat on the last flight and sleeping on the airport floor. Oh, and when at all possible, use your phone since there are more agents at the call center who can help get you on the right flight then there are at the airport helping the people in line.&lt;/p&gt;
&lt;h3&gt;After Arriving (the night before)&lt;/h3&gt;
&lt;p&gt;Do not let any friends, co-workers, acquaintances, or otherwise get you drunk because presenting with a hangover is just not cool. And that&amp;rsquo;s all I&amp;rsquo;ve got to say about that.&lt;/p&gt;
&lt;h3&gt;Presenting (drum roll, please)&lt;/h3&gt;
&lt;p&gt;If you are presenting to a Microsoft-centric audience and you are using a Mac, make sure you have everything you need to peacefully coexist in a Windows world. You will get the inevitable comments about running a Mac, so the last thing you want is to fail due to hardware or software issue. To help you out, here are a few tips:&lt;/p&gt;
&lt;p&gt;Almost every projector you come across will use an analog VGA (d-sub) connector, so be sure to keep a Mini DisplayPort to VGA (or DVI to VGA for older Macs) in your backpack. It doesn&amp;rsquo;t hurt to have a Mini DisplayPort to DVI as well in case you encounter a more modern project (although I&amp;rsquo;ve seen more HDMI than DVI projectors this past year &amp;mdash; all of which also supported VGA). I also pack actual video cables as well in case you end up doing an ad-hoc session in your hotel room.&lt;/p&gt;
&lt;p&gt;Many groups like to record or broadcast their sessions so that members unable to participate in person can view the presentation. To make things easy, be sure to have LiveMeeting already setup and tested on your machine. If you are on a Mac, this means converting your presentation from Keynote to either PowerPoint (using the free PowerPoint viewer if necessary) or Quicktime and presenting the slides from Windows. If you are not doing any Windows-based demos (like if you were giving an iPhone development talk), well, the remote folks are out of luck!&lt;/p&gt;
&lt;p&gt;If your talk is less about code and more about practices or principles, use the presenter display from Keynote or Powerpoint on your laptop. This helps you keep track of time, review your slide notes, and preview the next slide (which helps you avoid talking ahead of your deck). On the other hand, with code focused sessions stick to a mirrored display in the native resolution of the projector.&lt;/p&gt;
&lt;p&gt;When presenting code it is best to use a white background with black text. While the dark themes are great for large screen monitors, they look terrible on projectors with monospaced fonts. Many editors allow you to save and load settings, so build yourself a presentation mode settings file. With a set of high contrast colors (the default Visual Studio colors are a bit too hard to read at 25 feet) and a font size of 12-14 you&amp;rsquo;ll be all set to show your code to a large room. Font choice is another thing. While Consolas is my favorite, at larger font sizes it looks terrible - particularly in bold. Consider an alternate font for your presentation settings that looks good at a large font size.&lt;/p&gt;
&lt;p&gt;When presenting with slides, a remote is a great way to get out from behind the podium. I like to present while standing in front of the screen, so I always pack my Apple remote. I tend to avoid the higher tech solutions such as the Keynote Remote app for the iPhone as they tend to just get in the way. If you need to use the remote to control PowerPoint, there are some utilities that will make that work as well.&lt;/p&gt;
&lt;h3&gt;Choose Your Own Adventure (n, n, open door, n, e, melt wizard)&lt;/h3&gt;
&lt;p&gt;Most of the time, your presentation is going to be perfect for the audience. Assuming that your session description was accurate and a level of learning was indicated you should be set. However, your audience might not be prepared for the material being covered. In these situations it is best to have an alternative path that your presentation can take, one that is more appropriate for a general audience. In some cases this might not be possible, but it can really make the difference between an engaged audience and a bunch of people cleaning out their inbox (because there is likely was too much caffeine in their blood to actually sleep).&lt;/p&gt;
&lt;h3&gt;Wrap It Up (I&amp;rsquo;ll take it)&lt;/h3&gt;
&lt;p&gt;I hope the tips I&amp;rsquo;ve shared with you here will be useful if you decide to step up to the podium. There are many other posts that talk about slide design, demo subject matter, travel tips, and more but the items above are some of the ones I&amp;rsquo;ve had to learn on my own (and thankfully not always the hard way &amp;mdash; well except for one).&lt;/p&gt;



&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Traveling+to+Technical+Events&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f12%2f23%2ftraveling-to-technical-events.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f12%2f23%2ftraveling-to-technical-events.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=42016" width="1" height="1"&gt;</description></item><item><title>Building a Service Gateway Using MassTransit, Part 3</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/31/building-a-service-gateway-using-masstransit-part-3.aspx</link><pubDate>Sun, 01 Nov 2009 02:58:25 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:29858</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>13</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=29858</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/31/building-a-service-gateway-using-masstransit-part-3.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;This post is the third in a series on building a highly available service gateway. The implementation will be built in C# using &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt;, &lt;a href="http://www.asp.net/mvc/"&gt;ASP.NET MVC&lt;/a&gt;, and &lt;a href="http://nhforge.org/"&gt;NHibernate&lt;/a&gt;.&lt;/em&gt;
&lt;/p&gt;
&lt;h3&gt;Did somebody say code?&lt;/h3&gt;
&lt;p&gt;
The past two posts began to explain how to build a service gateway using MassTransit. In this post, I'm going to share some of the initial code that makes up the gateway service. The gateway itself consists of two components. The first implements the communication to the external service with a set of messages that are only used internally by the service. The second is the saga that provides the interface to the service gateway.
&lt;/p&gt;
&lt;h3&gt;Service Contract&lt;/h3&gt;
&lt;p&gt;The interface exposed to the application consists of two messages, the first for the command and the second for the response. The message contracts are defined using interfaces, allowing the class for the message to be an internal implementation detail.
&lt;/p&gt;&lt;p&gt;
The message contract representing a request for order details includes the CustomerId and the OrderId. 
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public interface RetrieveOrderDetails
{
	string OrderId { get; }
	string CustomerId { get; }
}
&lt;/pre&gt;
&lt;p&gt;
When the order details are received, the following message is published.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public interface OrderDetailsReceived
{
	string OrderId { get; }
	string CustomerId { get; }
	DateTime Created { get; }
	OrderStatus Status { get; }
}

public enum OrderStatus
{
	Unknown = 0,
	Submitted = 1,
	Accepted = 2,
	InProcess = 3,
	Complete = 4,
}
&lt;/pre&gt;
&lt;p&gt;
The CustomerId and OrderId are the same as the values passed in the request. Created is when the order was created, and Status is an enum representing the status of the order. Notice that no internal values are included -- no primary key from the order table and no primary key from the customer table. The request and response are correlated on identifiers that make sense in the application domain. While SQL purists will point out that numeric primary keys are quicker for retrieving rows in a database, they make for a very fragile interface with other components in the system. Reliance on a primary key outside of the context of the system storing the order details is a path to friction or outright failure.
&lt;/p&gt;
&lt;h3&gt;Time To Make the Saga&lt;/h3&gt;
&lt;p&gt;
At this point in the design of our service, the need for a saga to manage the request state is not entirely obvious. While the TDD purists might want to call YAGNI at this point, let me assure you that "it will all be... revealed!" So for now, let us take a look at the first pass of our saga definition.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public class OrderDetailsRequestSaga :
	SagaStateMachine&amp;lt;OrderDetailsRequestSaga&amp;gt;,
	ISaga
{
	static OrderDetailsRequestSaga()
	{
		Define(Saga);
	}
&lt;/pre&gt;
&lt;p&gt;
Our saga state machine uses a static initializer to define the states, events, and transitions of a saga. The previous code merely defines our class as a saga and calls our saga initialization method (shown below).
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
	private static void Saga()
	{
		Correlate(RequestReceived)
			.By((saga, message) =&amp;gt; saga.CustomerId == message.CustomerId &amp;amp;&amp;amp;
			                       saga.OrderId == message.OrderId &amp;amp;&amp;amp;
			                       saga.CurrentState == WaitingForResponse);

		Correlate(ResponseReceived)
			.By((saga, message) =&amp;gt; saga.CustomerId == message.CustomerId &amp;amp;&amp;amp;
			                       saga.OrderId == message.OrderId &amp;amp;&amp;amp;
			                       saga.CurrentState == WaitingForResponse);
&lt;/pre&gt;
&lt;p&gt;
Since our request criteria include our customer id and our order id, we use those to correlate the message to the saga. We also include the state of the saga to ensure that we do not match to a request that has already completed. We will look at some other ways we can enhance the performance of the service later on by using some additional states.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
	public static State Initial { get; set; }
	public static State WaitingForResponse { get; set; }
	public static State Completed { get; set; }
&lt;/pre&gt;
&lt;p&gt;
The three states we have defined, including an initial state when a new saga instance is created, a waiting for response state one our request has been sent to the service, and a completed state once the response has been received and published.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
	public static Event&amp;lt;RetrieveOrderDetails&amp;gt; RequestReceived { get; set; }
	public static Event&amp;lt;OrderDetailsResponse&amp;gt; ResponseReceived { get; set; }
	public static Event&amp;lt;OrderDetailsRequestFailed&amp;gt; RequestFailed { get; set; }
&lt;/pre&gt;
&lt;p&gt;
The three events that we have defined, including the message contract that maps to the event. The subscription logic for the saga will automatically map message handlers for these events that will invoke the actions depending upon the current state of the saga.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
		Initially(
			When(RequestReceived)
				.Then((saga, request) =&amp;gt;
					{
						saga.OrderId = request.OrderId;
						saga.CustomerId = request.CustomerId;
					})
				.Publish((saga, request) =&amp;gt; new SendOrderDetailsRequest
					{
						RequestId = saga.CorrelationId,
						CustomerId = saga.CustomerId,
						OrderId = saga.OrderId,
					})
				.TransitionTo(WaitingForResponse));
&lt;/pre&gt;
&lt;p&gt;
The first event handler, RequestReceived, is invoked when the saga is created in response to the RetrieveOrderDetails message. The handler copies the properties of the request, and then publishes the request message to the proxy that will call the external web service. After the message is published, the state of the saga transitions to the waiting for response state. When using transactional queues, the receipt of the message, creation of the saga in the database, sending of the command message to the proxy, and saving the saga are all part of a single distributed transaction. This ensures that everything completes as a single operation to ensure no requests are lost.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
		During(WaitingForResponse,
			When(ResponseReceived)
				.Then((saga, response) =&amp;gt;
					{
						saga.OrderCreated = response.Created;
						saga.OrderStatus = response.Status;
					})
				.Publish((saga, request) =&amp;gt; new OrderDetails
					{
						CustomerId = saga.CustomerId,
						OrderId = saga.OrderId,
						Created = saga.OrderCreated.Value,
						Status = saga.OrderStatus,
					})
				.TransitionTo(Completed));
	}
&lt;/pre&gt;
&lt;p&gt;
The second event handler, ResponseReceived, is invoked when the OrderDetailsResponse message is received. The results of the request are stored in the saga and a message is published containing the details of the order back to the original requestor. Another approach would be to capture the requestor address (via the ResponseAddress header from the message) and then resolve that address using the endpoint factory to send the response directly to the requestor. I don't really encourage this approach without having a truly unique identifier for each request. 
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
	public OrderDetailsRequestSaga(Guid correlationId)
	{
		CorrelationId = correlationId;
	}

	protected OrderDetailsRequestSaga()
	{
	}

	public virtual string CustomerId { get; set; }
	public virtual string OrderId { get; set; }
	public virtual OrderStatus OrderStatus { get; set; }
	public virtual DateTime? OrderCreated { get; set; }
	
	public virtual Guid CorrelationId { get; set; }
	public virtual IServiceBus Bus { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
The rest of the saga class is shown above for completeness. The properties are part of the saga and get saved when the saga is persisted (using the NHibernate saga persister, or in the case of the sample the in-memory implementation). The constructor with the Guid is used to initialize the saga when a new one is created, the protected one is there for NHibernate to be able to persist the saga.
&lt;/p&gt;
&lt;h3&gt;The Service Proxy&lt;/h3&gt;
&lt;p&gt;
The saga uses the external service proxy to perform the actual work, which is shown in the proxy class below.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public class OrderDetailsWebServiceProxy :
	Consumes&amp;lt;SendOrderDetailsRequest&amp;gt;.All
{
	public void Consume(SendOrderDetailsRequest request)
	{
		// make the call to the service to get the order details here

		var details = new OrderDetailsResponse
			{
				OrderId = request.OrderId,
				CustomerId = request.CustomerId,
				Created = (-1).Days().FromUtcNow(),
				Status = OrderStatus.InProcess,
			};

		CurrentMessage.Respond(details, x =&amp;gt; x.ExpiresAt(5.Minutes().FromNow()));
	}
}
&lt;/pre&gt;
&lt;p&gt;
The message handler uses the criteria from the SendOrderDetailsRequest message (which was published by the saga) to call the external service and retrieve the order details. The details are then returned to the saga in the form of an OrderDetailsResponse message which is internal to the service (and therefore not part of the interface assembly that is provided to applications that want to use the order details service).
&lt;/p&gt;
&lt;h3&gt;Test That Thang&lt;/h3&gt;
&lt;p&gt;
Now that our saga has been developed, we need to be able to test it. A unit test will be created that creates a testing instance of the service bus (see the sample for the implementation details) and verifies that the saga responds properly to the request. To request order details, a very simple client would subscribe to the response message and then publish the request. 
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
const string orderId = "ABC123";
const string customerId = "12345";

LocalBus.Subscribe&amp;amp;lt; OrderDetailsReceived &amp;gt;(message =&amp;gt;
	{
		response.Set(message);
	},
	x =&amp;gt; x.OrderId == orderId &amp;amp;&amp;amp; x.CustomerId == customerId);

RetrieveOrderDetails request = new RetrieveOrderDetailsRequest(customerId, orderId);
LocalBus.Publish(request, x =&amp;gt; x.SendResponseTo(LocalBus.Endpoint));
&lt;/pre&gt;
&lt;p&gt;
The subscribe method used above specifies that when a message of type OrderDetailsReceived is received, if the contents of the message match the predicate specified (which in this case, is checking the OrderId and CustomerId contained in the message) then the statement specified should be called. Our example is from the integration test (built using NUnit) that verifies the service performs from end-to-end.
&lt;/p&gt;&lt;p&gt;
The syntax above is functional, and it will work, but it does not represent the most scalable approach. In the next post, I'll start to explain how to build a much more scalable method of handling thousands of concurrent requests on a single machine using IIS.
&lt;/p&gt;&lt;p&gt;
In the meantime, the sample code is available in the MassTransit trunk as a standalone solution. You can find it in the trunk\src\Samples\ServiceGatewaySample folder. There are unit tests that verify the calling syntax shown above and a service that hosts the services (both the saga, and the proxy service).
&lt;/p&gt;





&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Building+a+Service+Gateway+Using+MassTransit%2c+Part+3&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f31%2fbuilding-a-service-gateway-using-masstransit-part-3.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f31%2fbuilding-a-service-gateway-using-masstransit-part-3.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=29858" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category></item><item><title>Building a Service Gateway Using MassTransit, Part 2</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/29/building-a-service-gateway-using-masstransit-part-2.aspx</link><pubDate>Thu, 29 Oct 2009 22:34:21 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:29367</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=29367</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/29/building-a-service-gateway-using-masstransit-part-2.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;This post is the second in a series on building a highly available service gateway. The implementation will be built in C# using MassTransit, StructureMap, ASP.NET MVC, and NHibernate.&lt;/em&gt;
&lt;/p&gt;
&lt;h3&gt;Continued...&lt;/h3&gt;
&lt;p&gt;
In part one, I discussed two exchange patterns that are often exposed as a web service. In this installment, I'm going to cover a more complex exchange pattern that including makes a request to an external system in response to a request on our web service.
&lt;/p&gt;&lt;p&gt;
&lt;em&gt;&lt;blockquote&gt;One of the comments on the previous post raised the question of using messaging for queries. Udi, Ayende, and I agree that this so-called "Data-SOA" is a bad thing. The queries I am presenting in this article are made against an external service and not an internal database. In many cases, this service might have limited availability and limited throughput, making it important to isolate our service to increase availability.&lt;/blockquote&gt;
&lt;/em&gt;&lt;/p&gt;
&lt;h3&gt;Complex Request&lt;/h3&gt;
&lt;p&gt;In the simple request, order status is a small enough data point that caching it at the web service makes sense. The details of an order, however, are much more involved and it is not practical to keep the details cached in case they are requested (which in this case, is much less often that the order status). To support the order detail request, we will apply a pattern that separates the inbound request from the outbound request, ensures that requests are performed only once (minimizing the click, click, click refresh mentality of some users), and even retain requests if the outbound service is unavailable until the request expires or the outbound service becomes available.
&lt;/p&gt;&lt;p&gt;
The first thing we want to build is the service that will be responsible for calling the external web service. To start, we'll define two messages representing the happy path of calling the service. The first message contains the criteria of the request (such as the order id and perhaps the customer id) and the second message contains the details of the order that were returned by the web service. In addition to the messages on the happy path, we may also define additional messages used to publish exception information related to the request.
&lt;/p&gt;&lt;p&gt;
Once the message contracts are defined to call the web service, we need to build a service that will handle the request messages. To do this, we'll build a message consumer for the request message, host it inside Topshelf, and subscribe to a service bus bound to the input queue of the service.
The message handler will use the request message properties to prepare the request to the external web service and produce the response message once the request returns. In this example, the external web service only supports synchronous requests (in a later article I will try to cover calling remote services that support asynchronous requests).
&lt;/p&gt;&lt;p&gt;
At this point, we have a service that responds to a command message (the request) and produces a result depending upon the outcome of the request. What we need now is a way to coordinate the requests to the service to ensure that requests are not lost due to an unavailable service, errors are retried once a failed service becomes available, and duplicate requests are ignored.
&lt;/p&gt;&lt;p&gt;
To coordinate the service requests, a saga will be created to manage the state of each request. A new message will be created to initiate the saga containing the same data that is needed to produce the request message that is sent to our gateway service. And as with the service, messages will be created to return the results of the request to the consumer of the external service. The saga will use our state machine driven saga syntax, making the business logic understandable at a glance. The saga will also define the retry parameters that should be used to recover from service outages by specifying an exception policy. The messages that are used by the requester to interact with the saga will become the interface that is used by our system to make requests to the external service. This includes the web service we are in turn providing to our customers. The internal messages used to communicate between the saga and the gateway service are not intended for use outside of our gateway service component.
&lt;/p&gt;&lt;p&gt;
&lt;em&gt;&lt;blockquote&gt;A nice side benefit of this architecture involves the actual call made by the gateway service. Since the interface is defined by the messages that are orchestrated by the saga, the backing implementation can be changed without impacting the consumers of the saga-based front end. This can be a huge benefit when it comes time to change service providers or bring an external service in-house either through acquisition or new product developments.&lt;/blockquote&gt;
&lt;/em&gt;&lt;/p&gt;&lt;p&gt;
With the saga in place (and for this example, we will go ahead and host it in the same process that we are hosting the gateway service), we are now ready to build the web service request handler. Rather than go straight into that now, I think I'm going to stop here and save that for part three. After part three is finished, I'll start posting some of the code for the examples and include it in the samples folder to make it easy to pull down and experiment with on your own machine.
&lt;/p&gt; 
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Building+a+Service+Gateway+Using+MassTransit%2c+Part+2&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f29%2fbuilding-a-service-gateway-using-masstransit-part-2.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f29%2fbuilding-a-service-gateway-using-masstransit-part-2.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=29367" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category></item><item><title>Building A Service Gateway Using MassTransit, Part 1</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/28/building-a-service-gateway-using-masstransit-part-1.aspx</link><pubDate>Thu, 29 Oct 2009 03:18:58 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:29068</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>8</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=29068</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/28/building-a-service-gateway-using-masstransit-part-1.aspx#comments</comments><description>&lt;p&gt;&lt;em&gt;This post is the first in a series on building a highly available service gateway. The implementation will be built in C# using MassTransit, StructureMap, ASP.NET MVC, and NHibernate.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Introduction&lt;/h3&gt;
&lt;p&gt;A common way of applying a messaging solution to an existing system is to find a tightly coupled service reference in the system and insulate the system from that dependency. For example, the system may utilize a web service via a call from the user interface, blocking the user interface until the web service request completes. Another situation might involve a web service provided by the system which in turn calls another web service to complete the request. While these two examples are common in systems today, it is important consider how the system is using a dependent service when determining how best to insulate the system from that service.
&lt;/p&gt;&lt;p&gt;
To help make that decision, allow me to share some common exchange patterns implemented using a web service. While most web services are modeled after the request/response pattern, there are actually several types of exchanges that can occur within a web service method.
&lt;/p&gt;
&lt;h3&gt;Drop Box&lt;/h3&gt;
&lt;p&gt;If the web service is called by the client to provide information to the application, availability is likely the most important concern. Clients may have limited connectivity (such as a wireless client), limited resources (such as an embedded system that is unable to store data for future delivery), or a combination of these and other factors. To deal with these limitations, the web service should be designed for maximum availability to avoid failed requests due to an outage behind the web service boundary.
&lt;/p&gt;&lt;p&gt;
To support this high availability, the web service should count on the only thing that is available at the time web service method is invoked -- the local machine. If the web service attempts to write the information to a database located on a server across the network that is not available, the information in the request may be lost. In this case, it would be a better choice to write a message that contains the request information to a local queue. By doing this, the request information is retained and can be processed separately from the web service request. This allows allows the web service method to return to the caller, keeping resources available for other clients to report.
&lt;/p&gt;&lt;p&gt;
Once the data from the request is safely stored in the queue, a separate service is built that consumes the messages from the queue and sends the information to the internal system, which in this case might be a database server. If the database server is unavailable, the message is left in the queue until the database is available. This new service can also be stopped and even updated without disrupting the web service from receiving requests.
&lt;/p&gt;
&lt;h3&gt;Simple Request&lt;/h3&gt;
&lt;p&gt;If we look at another scenario, one where the request contains search criteria and the response includes the data specified matching the request. In order to break this down in different way, we will look at two different types of requests. First, we will define a request to retrieve the status of an order. Second, we will define a request to retrieve the details of an order.
&lt;/p&gt;&lt;p&gt;
When only the status of the order is requested, we are dealing with a relatively small amount of data per order -- in this case, the order id and the status of the order. If this consisted of maybe 50 bytes of data per order, we could easily store the status of 100,000 orders in only 5 megabytes of RAM. Since users might check the status of an order often, at least once an hour, this could generate thousands of requests an hour. Since the status of an order may only change once or twice a day, the need to query the database each time the order status is checked can put an unnecessary strain on the database server. In addition, the order status request service is now tightly coupled to the database, making the availability of the service dependent upon the availability of the database. This dependency chain can get even longer as more complex systems are designed, so limiting the dependencies of a service is a key parameter in increasing availability.
&lt;/p&gt;&lt;p&gt;
To reduce database load, eliminate the dependency on the database, and increase the availability of our order status web service, we can design our service to subscribe to order status updates. When the status of an order is updated, the update will publish a message containing the new status. As the status of orders are updated, our service would update an in-memory cache containing the status of every order in the system (well, every is not necessarily every -- it could just be the orders placed over the last week that have not yet been received by the customer). On startup, the web service would query the database for the status of all orders placed and use the results of that query to seed the cache. Once the cache is seeded, as new orders are added and order status updates occur, the cache would update dynamically in response to the update messages. Since the cache is local to the web service, requests need only check the status by querying the cache and immediately returning the status of the order to the caller. In the case of an status request for an order that does not exist in the cache, the service could queue a request to get the order status from the database which would then publish that orders status so that it could be returned. If the order is not found, the service could return an unknown order response to the caller with instructions to perhaps try their request again later. 
&lt;/p&gt;
&lt;h3&gt;Up Next&lt;/h3&gt;
&lt;p&gt;
The two exchanges described above are relatively easy to implement using messaging (and likewise, using MassTransit). The next exchange pattern I'm going to cover is the more complex request where the dependent service must be called to complete the request. Due to that complexity, I'm going to wait until the next installment to describe that in greater detail. After that, I'll start to share some code as we build a solution to these exchanges.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Building+A+Service+Gateway+Using+MassTransit%2c+Part+1&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f28%2fbuilding-a-service-gateway-using-masstransit-part-1.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f28%2fbuilding-a-service-gateway-using-masstransit-part-1.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=29068" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category></item><item><title>Performance Counters Added to MassTransit</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/14/performance-counters-added-to-masstransit.aspx</link><pubDate>Wed, 14 Oct 2009 15:57:16 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:26349</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=26349</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/10/14/performance-counters-added-to-masstransit.aspx#comments</comments><description>&lt;p&gt;
One feature that is often overlooked in software development is the output of information that can be observed by operations once the application is in production. Fortunately, many open source projects are leveraging &lt;a href="http://logging.apache.org/log4net/index.html"&gt;log4net&lt;/a&gt; to provide a configurable level of runtime information that can be useful in figuring out why a system is behaving a certain way (and face, if you're looking, it's more than likely behaving badly). Logging, however, is only one view into an application -- one that might not deliver the appropriate information in a useful way.
&lt;/p&gt;&lt;p&gt;
Anyone who has used a computer with any interest is familiar with system monitoring tools. Task Manager (or if you're really cool Process Explorer) is the first place Windows users look when their system starts to crawl, Mac users turn to Activity Monitor, and I'm sure Linux users have some really obscure command-line tool as well. These coarse grained tools are usually enough for users, however, an operations team needs a higher degree of visibility into application -- particularly if they are expected to determine how to tune the application for better performance.
&lt;/p&gt;&lt;p&gt;
For operations on Windows, Performance Monitor provides detailed information for running applications in real-time. On a web server, it is easy to find out how many threads your ASP.NET application is using, as well as how many requests are queued. That information can be correlated with processor utilization to help determine if the bottleneck is the CPU, the network, or possibly even the database server. When it comes to troubleshooting issues on a live system, more information is always helpful to determine the source of the problem.
&lt;/p&gt;&lt;p&gt;
To support this level of visibility in &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, performance counter support has been added. Performance counters in .NET are part of the System.Diagnostics namespace. There are various counter types that can be defined, including counts, rates, and averages. When an application wants to output performance counters, it has to create a category and specify the counters that are included in the category. For instance:
&lt;/p&gt;&lt;p&gt;
&lt;pre name="code" class="csharp"&gt;
ConsumerThreadCount = new RuntimePerformanceCounter("Consumer Threads",
	"The current number of threads processing messages.",
	PerformanceCounterType.NumberOfItems32);

ReceiveRate = new RuntimePerformanceCounter("Received/s",
	"The number of messages received per second",
	PerformanceCounterType.RateOfCountsPerSecond32);
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
These are two of the counters defined by the MassTransit category. The first is a count that is updated when the number of threads in use changes. The second is a rate which gets incremented once for every message received. The actual calculation and display of the rate is handled by the performance monitoring tools - the application does not need to calculate it.
&lt;/p&gt;&lt;p&gt;
&lt;pre name="code" class="csharp"&gt;
ConsumerDuration = new RuntimePerformanceCounter("Average Consumer Duration",
	"The average time a consumer spends processing a message.",
	PerformanceCounterType.AverageCount64);

ConsumerDurationBase = new RuntimePerformanceCounter("Average Consumer Duration Base",
	"The average time a consumer spends processing a message.",
	PerformanceCounterType.AverageBase);
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
This counter is used to report the average consumer duration of a message. For an average, two counters are used. One is the base which is incremented for each occurrence and the counter is the actual count that is added. So for each message, the base is incremented once and the duration is incremented by the amount of time spent executing the consumer.
&lt;/p&gt;&lt;p&gt;
In adding performance counter support, I wanted to do it in a way that didn't leak the details of updating performance information throughout the framework. It was at this point that I turned to the &lt;a href="http://blog.phatboyg.com/2009/07/27/event-aggregator-using-the-magnum-pipeline/"&gt;Magnum Pipeline&lt;/a&gt;. Using the pipeline to publish the metrics allowed me to isolate the actual performance counter interface to a single method in a single class for the service bus. So instead of passing interfaces around all the components that make up the bus, a single event aggregator is passed instead. When you start up the bus, the performance counter code subscribes to the events as shown:
&lt;/p&gt;&lt;p&gt;
&lt;pre name="code" class="csharp"&gt;
_eventAggregatorScope.Subscribe&lt;MessageReceived&gt;(message =&gt;
	{
		_counters.ReceiveCount.Increment();
		_counters.ReceiveRate.Increment();
		_counters.ReceiveDuration.IncrementBy((long) message.ReceiveDuration.TotalMilliseconds);
		_counters.ReceiveDurationBase.Increment();
		_counters.ConsumerDuration.IncrementBy((long) message.ConsumeDuration.TotalMilliseconds);
		_counters.ConsumerDurationBase.Increment();
	});
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
Now, when the bus receives a message, it sends the event to the event aggregator (an instance of the Magnum Pipeline).
&lt;/p&gt;&lt;p&gt;
&lt;pre name="code" class="csharp"&gt;
var message = new MessageReceived
	{
		MessageType = messageType,
		ReceiveDuration = receiveTime,
		ConsumeDuration = consumeTime,
	};

	_eventAggregator.Send(message);
&lt;/pre&gt;
&lt;/p&gt;&lt;p&gt;
Since the Magnum Pipeline is publish/subscription, additional consumers could also opt-in to the MessageReceived event and perform other actions as well. I also plan to add counters per message type, allowing a finer grained view at message counts and consumer durations.
&lt;/p&gt;&lt;p&gt;
While the main story behind this post is the new counters available in MassTransit, my hope is that this brief introduction to performance counters was useful as well. You can learn more about performance counters from various articles that have been posted (such as &lt;a href="http://www.codeproject.com/KB/aspnet/DOTNETBestPractices3.aspx"&gt;a good one &lt;/a&gt;on CodeProject). You can check out the &lt;a href="http://blog.phatboyg.com/2009/07/27/event-aggregator-using-the-magnum-pipeline/"&gt;Magnum Pipeline&lt;/a&gt; in the &lt;a href="http://magnum-project.net/"&gt;Magnum&lt;/a&gt; project which is &lt;a href="http://code.google.com/p/magnum/"&gt;hosted at GoogleCode&lt;/a&gt;.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Performance+Counters+Added+to+MassTransit&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f14%2fperformance-counters-added-to-masstransit.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f10%2f14%2fperformance-counters-added-to-masstransit.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=26349" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/c_2300_/default.aspx">c#</category></item><item><title>Interface Subscriptions Now Supported by MassTransit</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/09/16/interface-subscriptions-now-supported-by-masstransit.aspx</link><pubDate>Thu, 17 Sep 2009 01:46:05 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:24899</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=24899</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/09/16/interface-subscriptions-now-supported-by-masstransit.aspx#comments</comments><description>
&lt;p&gt;Last year when we were reviewing the backlog of items that we wanted to build for MassTransit, one item that kept rising to the top of the list is a solid story for evolving message producers over the lifecycle of an enterprise system. Being able to publish events that current and down-level subscribers could consume was a key goal to avoid having to upgrade systems all at once when a publisher is updated. Fortunately, it hasn't been a real concern in our application since we deploy the entire system as a whole with each delivery.&lt;/p&gt;

&lt;p&gt;Nonetheless, a way to update a service that publishes messages without requiring every subscribing service to be updated at the same time was need.&lt;/p&gt;

&lt;h3&gt;Eliminating Impediments&lt;/h3&gt;

&lt;p&gt;Before we could implement interface subscriptions, there were a few things in the way that needed to be addressed, things that were not easy to implement.&lt;/p&gt;

&lt;p&gt;First, we were still doing binary message serialization. While we had the ability to use the .NET XML Serializer, it tends to be slow and difficult to fit into the model we had built with MT. Back in May, &lt;a href="http://blog.phatboyg.com/2009/05/27/masstransit-now-speaks-xml-by-default-2/"&gt;XML became the default serialization format&lt;/a&gt; using an entirely new serializer built from scratch.&lt;/p&gt;

&lt;p&gt;Second, we wanted to ensure that a publisher could publish a single message and have it delivered to all of the interested subscribers regardless of whether they had subscribed to the message class or one of the interfaces implemented by the class. In MassTransit, subscriptions are added by type a defined using a plain old CLR object (POCO). &lt;a href="http://blog.phatboyg.com/2009/03/30/masstransit-06-release-candidate/"&gt;In the 0.6 release&lt;/a&gt;, we replaced the message dispatcher with a new type-based pipeline for both inbound and outbound messages. Starting with an object and working down the type structure of the message, messages are pushed through the pipeline to interested message sinks. In the case of the outbound pipeline, it makes it easy to push a class through that has interfaces, since the interfaces can be assigned from the message object. Another hurdle eliminated.&lt;/p&gt;

&lt;h3&gt;Implementing Interfaces&lt;/h3&gt;

&lt;p&gt;
Once the hurdles were eliminated, it was actually very easy to add interface subscriptions. Since most of the internal bits had been reworked leveraging the power of expressions and generics, it was simply a matter of tweaking a few parts of the serializer and we were ready to rock and roll. Ensuring that message objects retain their type through the various pathways inside the system was also important, and resulted in fixing a couple of low hanging bugs related to message retry and fault publishing.
&lt;/p&gt;

&lt;p&gt;
The one bit of code that needed to be built was a way to provide a backing class for an interface to store the property values. At first, I looked at using something like LinFu or DynamicProxy2 to create a proxy for the interface and intercept the property accessors, but this had a problem. I did not want property setters on the interface. At that point, I started looking at using the Emit classes, AutoMapper, the FastProperty expression-based accessors, and how Udi had dealt with it inside NServiceBus. What I ended up with was a very fast, cached object builder implementation that is integrated within the message deserializer. In the words of Cartman, "It's pretty cool."
&lt;/p&gt;

&lt;p&gt;
There isn't really a difference in the code between using classes and interfaces from either the producing or consuming end. While a producer will likely continue to publish a class, it just has to implement the message interface on that class, allowing the consumer to subscribe to the interface, breaking the dependency on the actual class published by the producer. The pipeline will then properly serialize out a message for that interface and send it directly to the consumer.
&lt;/p&gt;
&lt;p&gt;
I'm pretty excited about this, and hope to update some of the pre-built services to use interfaces instead of classes in the near future. In the meantime, pull down the latest trunk and check it out.
&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Interface+Subscriptions+Now+Supported+by+MassTransit&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f09%2f16%2finterface-subscriptions-now-supported-by-masstransit.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f09%2f16%2finterface-subscriptions-now-supported-by-masstransit.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=24899" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/msmq/default.aspx">msmq</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/esb/default.aspx">esb</category></item><item><title>Under the Covers with MassTransit Endpoints</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/09/12/under-the-covers-with-masstransit-endpoints.aspx</link><pubDate>Sat, 12 Sep 2009 19:57:40 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:24757</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>3</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=24757</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/09/12/under-the-covers-with-masstransit-endpoints.aspx#comments</comments><description>&lt;p&gt;
&lt;em&gt;This post details some of the internal changes to how &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, an open-source lightweight service bus, communicates with transports such as MSMQ, ActiveMQ, and TIBCO. These changes are not likely to impact anyone using MassTransit, they are all well below the abstraction layer provided by the bus. At the same time, I felt it was important to share the change, along with the reasons it was made, with those that are using MassTransit.&lt;/em&gt;
&lt;/p&gt;
&lt;p&gt;
When &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt; was first started, MSMQ was the only transport we intended to support. In due time, however, it was determined that support for transports such as ActiveMQ and TIBCO was important. The ability to run on Linux and OS X under Mono (which does not support the System.Messaging namespace) as well as interoperability with Java systems using JMS (a specification for messaging, implemented by messaging systems like ActiveMQ and TIBCO) were the primary drivers of this decision. At the same time, insulating developers from the particulars of each transport was equally important.
&lt;/p&gt;
&lt;p&gt;
To communicate with an endpoint, MassTransit uses the &lt;em&gt;IEndpoint&lt;/em&gt; interface. The service bus would receive messages from an endpoint using this method:
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
IEnumerable&lt; IMessageSelector &gt; SelectiveReceive(TimeSpan timeout);
&lt;/pre&gt;
&lt;p&gt;
This involved making a call that returned an enumeration of message selectors, allowing the caller to step through the messages until an interesting message (in the case of the bus, a message with a subscribed consumer). The concerns of receiving a message were seemingly spread at random across three or four different classes (and yes, I wrote this crap). The reason for the complexity was solid though - I need the ability to selectively receive a message from a queue and skip over ones in which I have no interest.
&lt;/p&gt;
&lt;p&gt;
The complexity of dealing with the yield return/break syntax of enumerators and managing scope is difficult. The programming semantics behind it are difficult to understand. I wanted something better. With all the time I've been spending since this was written dealing with nested closures, lambda functions, and continuations I realized there was a better way to reduce the complexity while at the same time improving extensibility.
&lt;/p&gt;
&lt;p&gt;
The new signature for the receive method on an endpoint looks like this:
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
void Receive(Func&lt; object, Action&lt; object &gt; &gt; receiver, TimeSpan timeout);
&lt;/pre&gt;
&lt;p&gt;
With this new interface, the caller need only pass a method that accepts an object and returns a method that also accepts an object. The first method provides the caller an opportunity to inspect the message object to determine if the message will be consumed by the bus. If the bus is not interested, it can simply return null. If it is interested, it returns a method (either anonymous or a regular class method) that will consume the message. The endpoint will then call the returned method with the message once it has been received successfully. If the endpoint determines that the message is no longer available (if it were picked up by another process reading from the same queue for example), the returned method is not called.
&lt;/p&gt;
&lt;p&gt;
The calling method looks something like this:
&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
_endpoint.Receive(m =&gt; message =&gt; { doSomethingWith(message); });
&lt;/pre&gt;

&lt;p&gt;
This interface is far less complex to implement, and also made it easy to make a clean separation of what is an endpoint and what is a transport. Which leads me to...
&lt;/p&gt;
&lt;h3&gt;Endpoint and Transport Split?&lt;/h3&gt;

&lt;p&gt;Sadly that reads like a Hollywood headline, but it is true. &lt;strong&gt;Endpoints&lt;/strong&gt; now deal only with address resolution of sending and receiving messages and translating between the transport format and a message object (including de/serialization). New &lt;strong&gt;transport&lt;/strong&gt; classes are now responsible for the actual communication with the various queue implementations supported by MassTransit.
&lt;/p&gt;&lt;p&gt;
For example, previously there was one class, MsmqEndpoint, that contained all the aspects of talking to MSMQ regardless of the type of queue (local non-transactional, local transactional, remote). Now beneath the endpoint itself, there are three MSMQ transports, one for each of these scenarios. Each of these transports cleanly deals with the particulars only, for example, the non-transactional transport has no transactional concerns in it at all. 
&lt;/p&gt;

&lt;h4&gt;Introducing ITransport&lt;/h4&gt;

&lt;p&gt;The new ITransport interface is narrow, dealing only with the simplest form of communication -- streams. The send and receive methods from the endpoint are matched, but instead of dealing with objects, streams are used. Every transport should provide stream support at a minimum. The receive method of the transport looks like:&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
void Receive(Func&lt; Stream, Action&lt; Stream &gt; &gt; receiver, TimeSpan timeout);
&lt;/pre&gt;

&lt;p&gt;
While all transports implement streams, there is a benefit to communicating at a level above streams for certain types of endpoints. For example, when using MSMQ there are advantages to communicating directly with the Message object such as having access to the transport level message ID, the message label, and other interesting properties. To support this, the MsmqEndpoint only accepts an IMsmqTransport interface, which inherits from ITransport and adds:
&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
void Receive(Func&lt; Message, Action&lt; Message &gt; &gt; receiver, TimeSpan timeout);
&lt;/pre&gt;

&lt;p&gt;
Other transports may benefit from a custom interface as well, but it is only implemented for MSMQ at this point. ActiveMQ, Loopback, and Multicast UDP all use the base stream interface.
&lt;/p&gt;

&lt;h3&gt;Looking Forward&lt;/h3&gt;

&lt;p&gt;
This rewrite was not purely for entertainment value (well, it was fun). Latency when sending a message from a machine to a remote queue is orders of magnitude slower than writing to a local queue. And in addition, local queues have the advantage of being local -- which is important considering the first fallacy of distributed computing -- the network is reliable (NOT!). To compensate for this, a more reliable method of sending messages to a remote queue is needed. By ensuring that messages sent/published by an application are durable regardless of network failure, developers can use this fire-and-forget approach to messaging that is key to building event driven applications.
&lt;/p&gt;
&lt;p&gt;
To handle this, MassTransit now uses a store and forward transport for remote MSMQ queues. The store and forward transport will automatically create a local queue to cache the outbound messages destined for the remote queue. When a message is sent to the remote queue, the transport writes it to the local queue and returns to the caller. An asynchronous method then delivers the message in the background. The same transports that are used by the endpoint are reused by the store and forward transport, maintaining that high level of code reuse.
&lt;/p&gt;
&lt;p&gt;
&lt;em&gt;Note that on Windows Server 2003, I have observed that MSMQ will accept messages destined for a unreachable remote queue and attempt redelivery itself, but only for transactional queues (at least, that is what I have seen). &lt;/em&gt;
&lt;/p&gt;
&lt;h3&gt;Wrapping Up&lt;/h3&gt;
&lt;p&gt;
While it is always hopeful that changes like this will go by unnoticed, there is always the chance that there are some unintended consequences (read: bugs). Hopefully any of these will be weeded out quickly. In the meantime, I hope to start work on some availability features to support load balancing of command services.
&lt;/p&gt;

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Under+the+Covers+with+MassTransit+Endpoints&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f09%2f12%2funder-the-covers-with-masstransit-endpoints.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f09%2f12%2funder-the-covers-with-masstransit-endpoints.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=24757" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/activemq/default.aspx">activemq</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/msmq/default.aspx">msmq</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/c_2300_/default.aspx">c#</category></item><item><title>Event Aggregator Using the Magnum Pipeline</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/28/event-aggregator-using-the-magnum-pipeline.aspx</link><pubDate>Tue, 28 Jul 2009 05:11:39 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:23261</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=23261</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/28/event-aggregator-using-the-magnum-pipeline.aspx#comments</comments><description>&lt;p&gt;In the past few weeks, both &lt;a href="http://www.udidahan.com/2009/06/14/domain-events-salvation/"&gt;Udi Dahan&lt;/a&gt; and &lt;a href="http://codebetter.com/blogs/jeremy.miller/archive/2009/07/23/how-i-m-using-the-event-aggregator-pattern-in-storyteller.aspx"&gt;Jeremy D. Miller&lt;/a&gt; have posted on events. Udi posted about domain events, while Jeremy posted about his use of the event aggregator pattern in StoryTeller. In each case, events are represented as messages and each message is a class in C#. And in each post, a small publish/subscribe system is described that allowed objects (be it a domain object, domain service, or a controller) to subscribe to messages. Other objects could then use that same system to publish events to the subscribed objects.&lt;/p&gt;

&lt;p&gt;Now while you could use &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt; out of the box to handle this type of event aggregation, it is a bit heavy. The in-memory message transport serializes the message, which makes it impossible to pass a continuation or an object reference as part of an event. There is also a very service-oriented thread model where each consumer runs on a different thread making synchronization an important concern for unit testing. While it would work, it is not always the shiniest hammer in the toolbox for UI-based application.&lt;/p&gt;

&lt;p&gt;To address this, one of the things I've been adding to &lt;a href="http://code.google.com/p/magnum/"&gt;Magnum&lt;/a&gt; over the past few weeks is a new version of the pipeline that handles message distribution in MassTransit. In this implementation, I wanted a way to implement the event aggregator pattern with the same flexibility that I get with MassTransit but designed for an in-process mode of execution. At the same time, I wanted to make sure that I could scale this solution via adapters to extend events to MassTransit for publishing out-of-process.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Note, I use the word event and message interchangeably in this post.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;First, I wanted it to be able to handle any object without any constraints on the type. To this end, I came up with a very narrow API that only deals with the publishing of a message.&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
public interface Pipe
{
	void Send&lt; T &gt;(T message) where T : class;
}
&lt;/pre&gt;

&lt;p&gt;The &lt;strong&gt;Send&lt;/strong&gt; method is fairly obvious, it is used to send a message to any consumers that are subscribed to the message. With this implementation, consumers that are subscribed to any type to which the message can be assigned will also get the message. Consider the following class structure:&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
public class CustomerChanged
{
	public Customer Customer { get; set; }
}

public class CustomerRatingDowngraded : 
	CustomerChanged
{
}
&lt;/pre&gt;

&lt;p&gt;A consumer that subscribed to the &lt;em&gt;CustomerChanged&lt;/em&gt; type would receive the message if a &lt;em&gt;CustomerRatingDowngraded&lt;/em&gt; message was published. It also works for interfaces, as long as the message object being published supports the interface.&lt;/p&gt;

&lt;p&gt;An obvious omission from this API is any method of subscribing consumers to the pipeline. To subscribe to the pipeline, an extension method on the &lt;em&gt;Pipe&lt;/em&gt; interface creates a new subscription scope. A subscription scope, represented by the &lt;em&gt;ISubscriptionScope&lt;/em&gt; interface, makes changes to the pipeline resulting in the creation of a new pipeline. A series of visitors are used to create a new version of the pipeline with the consumers added, along with another visitor to remove the consumers when they unsubscribe. ISubscriptionScope implements &lt;em&gt;IDisposable&lt;/em&gt; so to unsubscribe your application can just dispose of the object.&lt;/p&gt;

&lt;p&gt;It is interesting to note that much like the Expression class in .NET, pipelines are immutable. Since pipelines cannot be changed, the need to lock parts of the pipeline during message distribution is removed. By removing the need for locking to ensure safe operation in a concurrent environment, performance improves and blocking is eliminated. At the same time, consumers can subscribe and unsubscribe from the pipeline as needed without disrupting the system.&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
public void Start()
{
	// this creates an empty pipeline that accepts any object
	_eventAggregator=PipeSegment.Input(PipeSegment.End());

	_scope=_eventAggregator.NewSubscriptionScope();
	_scope.Subscribe&lt; CustomerChanged &gt;(message=&gt;Trace.WriteLine("Customer changed: "+message.CustomerName));
}
&lt;/pre&gt;

&lt;p&gt;In this example, pipe and scope would likely be member variables that would be released when the containing object is stopped or disposed. Multiple subscriptions can be added to a single scope, each one modifying the pipeline as it is added.&lt;/p&gt;

&lt;p&gt;When I discuss event-based programming, I often mention the need for visualization tools in order to ensure the system is performing as expected. In the example above, I could use the TracePipeVisitor to verify that the consumer was indeed subscribed to the pipeline (by calling new TracePipeVisitor() .Trace(_eventAggregator) and viewing the results in the output window).&lt;/p&gt;

&lt;p&gt;&lt;pre&gt;
Input&lt; Object &gt;: 
RecipientList&lt; Object &gt;: 
     Filter&lt; CustomerChanged &gt;: Allow Magnum.Specs.Pipeline.Messages.CustomerChanged
     RecipientList&lt; CustomerChanged &gt;: 
          MessageConsumer&lt; CustomerChanged &gt;: 
&lt;/pre&gt;&lt;/p&gt;

&lt;p&gt;As consumers are added, the pipeline is built up using a series of PipeSegment classes. The Input segment is the initial entry point to the pipeline and by having the responsibility is the only segment that actually changes in the pipeline. The RecipientList is a one-to-many switch that delivers incoming messages to each consumer. The Filter segment only passes a specific type through the filter, preventing unwanted messages from receiving the consumer. The MessageConsumer actually invokes the method that was subscribed to the message.&lt;/p&gt;

&lt;p&gt;In the above example, the message consumer was accepted using the MessageConsumer&lt;T&gt; delegate type, which is analogous to Action&lt;T&gt; with T being the message type. Another way to subscribe is to implement the IConsume&lt;T&gt; method as shown below.&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
public class ListViewController :
	IConsume&lt; CustomerChanged &gt;
{
	public ListViewController(ListView customerListView)
	{
		_customerListView = customerListView;
	}
	public void Consume(CustomerChanged message)
	{
		_customerListView.DoSomeUpdate(message.Customer);
	}
}
&lt;/pre&gt;

&lt;p&gt;A class can implement the IConsume&lt;T&gt; method to indicate that it is interested in messages of type T. In this case, the CustomerChanged message is of particular interest as it is used to update the user interface in response to a customer change event. The instance of the controller can be subscribed to the pipeline by calling the Subscribe method passing the object reference itself.&lt;/p&gt;

&lt;pre name="code" class="csharp"&gt;
public void BootstrapUserInterfaceControllers()
{
	_customerListViewController = new ListViewController(customerListView);

	_scope=_eventAggregator.NewSubscriptionScope();
	_scope.Subscribe(_customerListViewController);
}
&lt;/pre&gt;

&lt;p&gt;This is the first in a series of posts about the pipeline in Magnum. As I add the remaining functionality, including asynchronous message consumers, aggregate consumers, and automatic binding to the Magnum StateMachine (similar to how sagas are done using MassTransit), I'll post about how they are used. I encourage you to &lt;a href="http://code.google.com/p/magnum/"&gt;take a look at the code&lt;/a&gt; and particularly the unit tests to see the different ways the pipeline can be used.&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Event+Aggregator+Using+the+Magnum+Pipeline&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f28%2fevent-aggregator-using-the-magnum-pipeline.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f28%2fevent-aggregator-using-the-magnum-pipeline.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=23261" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category></item><item><title>Accessing ActiveMQ on Mac OSX from a Windows VM</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/12/accessing-activemq-on-mac-osx-from-a-windows-vm.aspx</link><pubDate>Sun, 12 Jul 2009 19:46:27 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:22823</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=22823</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/12/accessing-activemq-on-mac-osx-from-a-windows-vm.aspx#comments</comments><description>&lt;p&gt;With &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, we support multiple messaging transports, including MSMQ (comes with Windows), ActiveMQ (an open-source Java message broker), and &lt;a href="http://www.tibco.com/software/messaging/enterprise-message-service/default.jsp"&gt;TIBCO EMS&lt;/a&gt; (a not-so-open-source message broker). With that in mind, teams building on the Windows platform can comfortably choose MSMQ and enjoy familiar management tools. If your needs expand to multiple platforms, however, the other choices become more important. One of our goals is to enable MassTransit to communicate between services running on Windows, OS X, and Linux. By using the &lt;a href="http://mono-project.com/Main_Page"&gt;Mono Project&lt;/a&gt; to run .NET code on OS X and Linux, and ActiveMQ to handle the messaging, we&amp;#39;re pretty confident that we can reach that goal.&lt;/p&gt;

&lt;p&gt;To start working towards this endeavor, I had to first get a working test environment. ActiveMQ can run on Windows, Linux, and OS X. Since &lt;a href="http://codebetter.com/blogs/dru.sellers/"&gt;Dru Sellers&lt;/a&gt; and I both develop on &lt;a href="http://www.apple.com/macbookpro/"&gt;Macs&lt;/a&gt; using &lt;a href="http://vmware.com/products/fusion/"&gt;VMware Fusion&lt;/a&gt; to host various versions of Windows, I wanted to install and run ActiveMQ on the Mac host, making it available to any of the virtual machines. I had not really dealt with setting up services on OS X yet, but was happy to learn that it is a pretty slick process to get things installed and running. Hopefully this will help if you decide to do the same.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;I should note that I am not an ActiveMQ installation/administration expert. I am configuring ActiveMQ for use in a development environment. If you are going to use ActiveMQ in production, make sure it is configured for proper production operation with the appropriate security, storage, etc. That being said, let&amp;#39;s get started.&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;Getting Started&lt;/h3&gt;

&lt;p&gt;You need to &lt;a href="http://activemq.apache.org/index.html"&gt;download ActiveMQ&lt;/a&gt;. I got the Linux version by typing in the URL manually to get it to download using Safari. The archive will be unpacked into a tar file automatically by Safari (if not, just double-click it), which you can then open the tar file into a folder by double-clicking it again. If you are a command-line wizard, you already know how to handle the tar.gz files so enjoy.&lt;/p&gt;

&lt;p&gt;Move the unpacked folder (apache-activemq-5.2.0 in my case) to the /usr/local folder by opening Terminal and entering:&lt;/p&gt;

&lt;p&gt;sudo mv apache-activemq-5.2.0 /usr/local/&lt;/p&gt;

&lt;p&gt;While still in terminal, change to the ActiveMQ folder. We need to modify the configuration.&lt;/p&gt;

&lt;p&gt;cd /usr/local/apache-activemq-5.2.0&lt;br /&gt;
mate conf/activemq.xml&lt;/p&gt;

&lt;p&gt;If you aren&amp;#39;t using &lt;a href="http://macromates.com/"&gt;TextMate&lt;/a&gt;, well, do whatever you need to do to open that file. I removed a lot of unused things from the file, but your needs may vary. You can &lt;a href="http://blog.phatboyg.com/wp-content/activemq.xml"&gt;download my configuration file&lt;/a&gt; if you want to use what I am using. You will need to modify the IP addresses to match your environment. I originally tried to use just localhost, but had issues with it connecting from my Windows 7 VM. If this is just a fluke, I&amp;#39;ll update my file later with my new settings.&lt;/p&gt;

&lt;h3&gt;Installing the launch daemon into OS X&lt;/h3&gt;

&lt;p&gt;To run ActiveMQ as a service, you need to create a property list that describes the application. This is just an XML file, but we need to create it and put it into the /Library/LaunchDaemons folder and call it com.apache.activemq so we can identify it later. You can &lt;a href="http://blog.phatboyg.com/wp-content/com.apache.activemq"&gt;download my version of the file&lt;/a&gt; to save some typing if you prefer.&lt;/p&gt;

&lt;div style="text-align:center;"&gt;&lt;p&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/activemqterminalsetup.png" alt="ActiveMQTerminalSetup.png" border="0" width="640" height="229" /&gt;&lt;/p&gt;&lt;/div&gt;

&lt;p&gt;Some of these settings can be adjusted if you don&amp;#39;t want to keep ActiveMQ running all the time. KeepAlive will automatically restart the service if it stops for some reason (including manually stopping it) and you can set that to false if you want to control it manually.&lt;/p&gt;

&lt;p&gt;After creating the file, we need to configure OS X so it knows about the new service. To do this, type the following:&lt;/p&gt;
&lt;p&gt;sudo launchctl load /Library/LaunchDaemons/com.apache.activemq&lt;br /&gt;
sudo launchctl start com.apache.activemq&lt;/p&gt;

&lt;p&gt;Once you have done this, you can verify that it is started by running the OS X Console application (find it in QuickSilver/Spotlight if you don&amp;#39;t know where it is). Look at the message logs and you can see the startup messages from the service:&lt;/p&gt;

&lt;p&gt;&lt;div style="text-align:center;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/activemqconsolelog.png" alt="ActiveMQConsoleLog.png" border="0" width="640" height="451" /&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;So how do we know that we have a working installation? Well, there is an admin console that you can reach by navigating to http://localhost:8161/admin that will let you view the queues, topics, etc. that are running. You can also use the JMX tools to dig into the queues as well, including the ability to send messages to the queues directly from the Java console! To get the console started, you need to run jconsole from Terminal. Once it is started, you need to connect to the URL that is configured:&lt;/p&gt;

&lt;p&gt;&lt;div style="text-align:center;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/jconsoleconnect.png" alt="JConsoleConnect.png" border="0" width="480" height="380" /&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;Once you are in the JConsole, you can view all the queues. It should look like this (well, assuming you&amp;#39;ve created some queues, which I&amp;#39;ve done here with the Starbucks sample from MassTransit).&lt;/p&gt;

&lt;p&gt;&lt;div style="text-align:center;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/jconsoleview.png" alt="JConsoleView.png" border="0" width="640" height="542" /&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;You can see the default URL that was connected to in the title bar, along with the tree view of all the objects. The more interesting tab is the Operations tab, which lets you run commands against the queue. In fact, you can past some XML straight into an input box and click &amp;quot;sendTextMessage&amp;quot; and the message will be stuffed into the queue right there.&lt;/p&gt;

&lt;p&gt;&lt;div style="text-align:center;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/jconsoleoperations.png" alt="JConsoleOperations.png" border="0" width="640" height="495" /&gt;&lt;/div&gt;&lt;/p&gt;

&lt;p&gt;So now that we know ActiveMQ is running and happy, we can modify our application to use the ActiveMQ transport instead of the MSMQ transport by simply changing the URI for the endpoint. So instead of msmq://localhost/mt_subscriptions you would specify activemq://192.168.0.195:61616/mt_subscriptions (in my case, that is the IPv4 address of my host machine). As long as the transport is in the same folder and you&amp;#39;re using the &lt;a href="http://structuremap.sourceforge.net/Default.htm"&gt;StructureMap&lt;/a&gt; base registry without specifying a specific transport, it should connect up to the host and start working. The other containers will hopefully get this support soon, it was just easy to add with the Scan() feature of StructureMap&amp;#39;s registry DSL.&lt;/p&gt;

&lt;p&gt;I hope to dig deeper into the ActiveMQ transport support in MassTransit, as well as start testing it while running under Mono on OS X over the next few weeks. I already have the Windows bits working, I just want to test more exception cases such as losing the connection to ActiveMQ, as well as other runtime issues to make the code more production ready. I also want to try sending messages to/from other languages, such as &lt;a href="http://www.ruby-lang.org/en/"&gt;Ruby&lt;/a&gt; via &lt;a href="http://stomp.codehaus.org/"&gt;STOMP&lt;/a&gt;, but my Ruby skills are not the greatest.&lt;/p&gt;

&lt;p&gt;At the very least, I hope this article helps you get ActiveMQ installed and running on your Mac using OS X Leopard. If you do run into issues or have problems, be sure to visit the &lt;a href="http://groups.google.com/group/masstransit-discuss"&gt;MassTransit mailing list&lt;/a&gt; and post your questions/issues there.&lt;/p&gt;






&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Accessing+ActiveMQ+on+Mac+OSX+from+a+Windows+VM&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f12%2faccessing-activemq-on-mac-osx-from-a-windows-vm.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f12%2faccessing-activemq-on-mac-osx-from-a-windows-vm.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=22823" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/osx/default.aspx">osx</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/mac/default.aspx">mac</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/activemq/default.aspx">activemq</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/msmq/default.aspx">msmq</category></item><item><title>I'm a Microsoft C# MVP!</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/01/i-m-a-microsoft-c-mvp.aspx</link><pubDate>Thu, 02 Jul 2009 00:14:35 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:22573</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=22573</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/07/01/i-m-a-microsoft-c-mvp.aspx#comments</comments><description>&lt;p&gt;Opening my Inbox this morning resulted in a pleasant surprise from Microsoft. I have been presented with the 2009 Microsoft&amp;reg; MVP Award as a recognition for sharing my expertise in Visual C# with others. It is an honor to be recognized by Microsoft for doing something that I truly enjoy - building really cool software.&lt;/p&gt;

&lt;div style="text-align:center;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/07/mvpbanner.png" alt="MVPbanner.png" border="0" width="584" height="108" /&gt;&lt;/div&gt;

&lt;p style="text-align:center;"&gt;&lt;blockquote&gt;&lt;em&gt;Microsoft Most Valuable Professionals (MVPs) are exceptional technical community leaders from around the world who are awarded for voluntarily sharing their high quality, real world expertise in offline and online technical communities. Microsoft MVPs are a highly select group of experts that represents the technical community&amp;#39;s best and brightest, and they share a deep commitment to community and a willingness to help others.&lt;/em&gt;&lt;/blockquote&gt;
&lt;/p&gt;
&lt;p&gt;A community-based award like this would not be possible without the support of my peers, and I look forward to meeting up with some of those very peers at the next MVP Summit! In the meantime, congratulations to all the new and renewed MVPs!&lt;/p&gt;

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=I%27m+a+Microsoft+C%23+MVP!&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f01%2fi-m-a-microsoft-c-mvp.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f07%2f01%2fi-m-a-microsoft-c-mvp.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=22573" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/c_2300_/default.aspx">c#</category></item><item><title>Dallas TechFest 2009 Tomorrow</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/06/18/dallas-techfest-2009-tomorrow.aspx</link><pubDate>Fri, 19 Jun 2009 03:04:26 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:22197</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>0</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=22197</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/06/18/dallas-techfest-2009-tomorrow.aspx#comments</comments><description>&lt;p&gt;It&amp;#39;s been a couple of busy weeks! The weekend before my presentations in Arkansas, I decided to forego sleep and go to &lt;a href="http://www.wakarusa.com/"&gt;Wakarusa&lt;/a&gt;! The Saturday lineup included two of my favorite electronica acts, &lt;a href="http://www.twistedmusic.com/artists/shpongle"&gt;Shpongle&lt;/a&gt; and &lt;a href="http://www.ottsonic.com/"&gt;Ott&lt;/a&gt;, and introduced to another band STS9. Staying up all night dancing to heavy techno music and getting no sleep was one heck of a prelude to the week. After the seriously fun times I stopped in on the Fort Smith and Northwest Arkansas .NET user groups and shared my thoughts on Event Driven Architecture. Both nights went well, with some great conversations afterwards about the how and why that took me towards that style of architectural system design.&lt;/p&gt;

&lt;p&gt;Tomorrow morning at 9:00 AM (hey, if you aren&amp;#39;t first, you&amp;#39;re last -- this is Texas after all) I&amp;#39;ll be presenting at Dallas TechFest on the same subject in the .NET track. If you want to learn more about Event Driven Architecture and how we are making it easier to implement with MassTransit, come by and check it out. I promise to deliver a demo that includes all sorts of cool things like NHibernate, FluentNHibernate, StructureMap, Castle, Topshelf, Magnum, and of course, MassTransit.&lt;/p&gt;

&lt;p&gt;Also, if you&amp;#39;re in the Tulsa area and weren&amp;#39;t able to make it to Dallas TechFest, I&amp;#39;ll be presenting at the Tulsa .NET User Group on the 29th of June as well. If you do come by, be sure to stop afterwards and say hello. I&amp;#39;ll be at the event throughout the day and am more than happy to talk to anyone interested in learning more about &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;. I&amp;#39;m sure I&amp;#39;ll be around the functional programming open space playing with crazy monadic parsers written in C#.&lt;/p&gt;

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Dallas+TechFest+2009+Tomorrow&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f06%2f18%2fdallas-techfest-2009-tomorrow.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f06%2f18%2fdallas-techfest-2009-tomorrow.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=22197" width="1" height="1"&gt;</description></item><item><title>Presenting on Event Driven Architecture and MassTransit</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/27/presenting-on-event-driven-architecture-and-masstransit.aspx</link><pubDate>Thu, 28 May 2009 02:57:29 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:21277</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=21277</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/27/presenting-on-event-driven-architecture-and-masstransit.aspx#comments</comments><description>&lt;div style="text-align:center;padding-bottom:12px;"&gt;&lt;h3&gt;Event Driven Architecture&lt;/h3&gt;&lt;/div&gt;
&lt;p&gt;Business applications no longer exist as isolated systems. In order to provide integrated solutions that add business value, applications must be connected. Modern approaches for enterprise application integration (EAI) such as Service Oriented Architecture (SOA) separate applications into services that can be accessed via a standard interface such as a web service. This collection of services provides a platform on which new applications can be created that leverage the existing functionality.
&lt;/p&gt;&lt;p&gt;
However, as application complexity increases, the coupling of services becomes an impediment in adapting applications to meet the ever-changing goals of the business. Event Driven Architecture (EDA) is a method of designing and implementing systems where events are exchanged between highly decoupled components and services. EDA does not replace SOA, instead it compliments the request/response nature of SOA with a highly scalable event model for building and coordinating asynchronous transactions.
&lt;/p&gt;&lt;p&gt;
In this presentation, I will explain event driven architecture, describe the different types of events, demonstrate how events can be related and orchestrated, and provide a basic understanding of how this method can drive the architecture of enterprise systems. In addition to understanding the concepts of event driven architecture, we will explore a working sample built using an open-source .NET messaging framework called MassTransit.
&lt;/p&gt;
&lt;p&gt;You can catch this presentation at the following events:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;On Monday, June 8th, I&amp;#39;ll be presenting at the &lt;a href="http://www.fsdnug.org/"&gt;Fort Smith .NET User Group&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;On Tuesday, June 9th, I&amp;#39;ll be presenting at the &lt;a href="http://nwadnug.org/tabid/54/ModuleID/370/ItemID/42/mctl/EventDetails/Default.aspx"&gt;Northwest Arkansas .NET User Group&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;On Friday, June 19th, I&amp;#39;ll be presenting at &lt;a href="http://www.dallastechfest.com/"&gt;Dallas TechFest 2009&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;div style="text-align:center;padding-bottom:12px;"&gt;&lt;img src="http://blog.phatboyg.com/wp-content/uploads/2009/05/dallas-techfest-slide.png" alt="Dallas TechFest Slide.png" border="0" width="477" height="179" /&gt;&lt;/div&gt;
&lt;p&gt;If you are one or more of these events, be sure to come up and introduce yourself after the session. Also, if you have any feedback on the presentation, feel free to post your comments here or via e-mail.&lt;/p&gt;

&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=Presenting+on+Event+Driven+Architecture+and+MassTransit&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f27%2fpresenting-on-event-driven-architecture-and-masstransit.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f27%2fpresenting-on-event-driven-architecture-and-masstransit.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=21277" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/esb/default.aspx">esb</category></item><item><title>MassTransit Now Speaks XML By Default</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/27/masstransit-now-speaks-xml-by-default.aspx</link><pubDate>Wed, 27 May 2009 14:12:00 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:21254</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>5</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=21254</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/27/masstransit-now-speaks-xml-by-default.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Last night, I made a major update to the trunk of MassTransit that changes the default message format from binary to XML.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
Since the first drop of &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, the default format for messages was &lt;em&gt;binary&lt;/em&gt;. This had several advantages, one being the ability to take any object and just use it as a message. The BinaryFormatter in .NET is pretty hard core about making sure that the object will come out the other end without any changes. The disadvantage of using binary as a message format is it makes it very difficult to interoperate with other systems. For this reason, we are embracing XML as the new default message format.
&lt;/p&gt;
&lt;p&gt;
The road to XML was not easy. Many months ago when we opened up the endpoints to a configurable serialization format, we ran into some issues with our first attempt at XML. We used the .NET XmlSerializer at first, and found it to be too limited to meet our needs. There was no support for serializing dictionaries (which I would call a smell in your message contract anyway), and you had to register the type of every object being serialized in advance in order for it work properly. With all these problems, we continued to keep binary as the default with XML being experimental.
&lt;/p&gt;
&lt;p&gt;
I started writing a new XML serializer from the ground up nearly a year ago. I went through a lot of approaches and just didn&amp;#39;t find something that I liked using .NET 2.0 as the framework. I looked at a more Google Protocol Buffers technique for a while, but found it a little cumbersome to deal with compared to just using XML. Then, a month or two ago, I started to seriously look at how we could do a solid XML serialization attempt again using the .NET 3.5 framework features. From that research, the current default XML serializer was founded.
&lt;/p&gt;
&lt;p&gt;
Like many ORMs such as NHibernate, the use of the XML message serializer requires that the objects being serialized have a no-argument constructor. It can be protected to hide it from developers, but it must exist. Also, only properties are serialized as they represent the public contract that the message object is sharing. And the properties must be read/write so that the serializer can get and set the value of the property. Here is an example of one of our messages:
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;public class NewUserRegistered : 
	CorrelatedBy&amp;lt; Guid &amp;gt;
{
	public Guid CorrelationId { get; set; }
	public string Username { get; set; }
	public string Email { get; set; }
	public DateTime RegisteredAt { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
Since messages are DTOs (data transfer objects), I try to keep them clean and simple. They are pure data containers and are not meant to have any behavior associated with them. While I treat my messages as immutable, I don&amp;#39;t find I need to express that in the message class itself since it just tends to make the object more confusing. And quite honestly, there are better ways to express immutable message contracts, such as using interfaces.
&lt;/p&gt;
&lt;p&gt;
The resulting XML for the above message looks like:
&lt;/p&gt;
&lt;pre name="code" class="xml"&gt;&amp;lt;?xml encoding=&amp;quot;utf-8&amp;quot; version=&amp;quot;1.0&amp;quot;?&amp;gt;
&amp;lt;x:XmlMessageEnvelope 
	xmlns:x=&amp;quot;MassTransit.Serialization.XmlMessageEnvelope, MassTransit&amp;quot;
	xmlns:m=&amp;quot;MassTransit.Tests.Serialization.NewUserRegistered, MassTransit.Tests&amp;quot; 
	xmlns:s=&amp;quot;System.String, mscorlib&amp;quot; 
	xmlns:d=&amp;quot;System.DateTime, mscorlib&amp;quot; 
	xmlns:g=&amp;quot;System.Guid, mscorlib&amp;quot; 
	xmlns:i=&amp;quot;System.Int32, mscorlib&amp;quot;&amp;gt;
  &amp;lt;m:Message&amp;gt;
    &amp;lt;s:Username&amp;gt;billg&amp;lt;/s:Username&amp;gt;
    &amp;lt;s:Email&amp;gt;billg@microsoft.com&amp;lt;/s:Email&amp;gt;
    &amp;lt;d:RegisteredAt &amp;gt;2009-05-27T13:50:21.106875Z&amp;lt;/d:RegisteredAt&amp;gt;
    &amp;lt;g:CorrelationId &amp;gt;acb709df-7e32-45e2-a48c-9c160091aa5d&amp;lt;/g:CorrelationId&amp;gt;
  &amp;lt;/m:Message&amp;gt;
  &amp;lt;s:SourceAddress &amp;gt;loopback://localhost/source&amp;lt;/s:SourceAddress&amp;gt;
  &amp;lt;s:DestinationAddress &amp;gt;loopback://localhost/destination&amp;lt;/s:DestinationAddress&amp;gt;
  &amp;lt;s:MessageType &amp;gt;MassTransit.Tests.Serialization.NewUserRegistered, MassTransit.Tests&amp;lt;/s:MessageType&amp;gt;
&amp;lt;/x:XmlMessageEnvelope&amp;gt;
&lt;/pre&gt;
&lt;p&gt;
Namespaces are created for each type of object that is serialized. This makes it fast and easy for the deserialization process to figure out what type to use when reading the value from the XML. It&amp;#39;s easy to look and see the actual data that belongs to the message, as well as the headers used by MassTransit.
&lt;/p&gt;
&lt;p&gt;
I wanted to post this as quickly as possible after committing the changes to the trunk so I will wrap up. I&amp;#39;m pretty excited about having a nice, readable message format. Since finding &lt;a href="http://www.geekproject.com/tools.aspx#10"&gt;MSMQ Studio&lt;/a&gt;, I&amp;#39;ve really wanted to improve the diagnosis story for looking at messages inside the queues. I think this is a solid move towards that goal and a great first steps towards interoperability with other systems.
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=MassTransit+Now+Speaks+XML+By+Default&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f27%2fmasstransit-now-speaks-xml-by-default.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f27%2fmasstransit-now-speaks-xml-by-default.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=21254" width="1" height="1"&gt;</description><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/.net/default.aspx">.net</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/masstransit/default.aspx">masstransit</category><category domain="http://www.lostechies.com/blogs/chris_patterson/archive/tags/msmq/default.aspx">msmq</category></item><item><title>MassTransit Saga Enhancements for Event Processing</title><link>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/22/masstransit-saga-enhancements-for-event-processing.aspx</link><pubDate>Sat, 23 May 2009 02:32:18 GMT</pubDate><guid isPermaLink="false">ded273ab-9e87-4979-8222-e4e2e46f1b46:21187</guid><dc:creator>Chris Patterson</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://www.lostechies.com/blogs/chris_patterson/rsscomments.aspx?PostID=21187</wfw:commentRss><comments>http://www.lostechies.com/blogs/chris_patterson/archive/2009/05/22/masstransit-saga-enhancements-for-event-processing.aspx#comments</comments><description>&lt;p&gt;In the past year, I&amp;#39;ve been learning about &lt;a href="http://en.wikipedia.org/wiki/Event_Driven_Architecture"&gt;Event Driven Architecture&lt;/a&gt; and how using it in the enterprise can make adding functionality over time easier through a loosely-coupled event-based architecture. With Event Driven Architecture, business components can subscribe and react to events, using correlation and orchestration to combine those events to represent business processes. The loose-coupled nature makes it easier to add functionality without modifying existing services -- reducing the risk of deploying new services over time.
&lt;/p&gt;&lt;p&gt;
When I first added saga support to &lt;a href="http://code.google.com/p/masstransit/"&gt;MassTransit&lt;/a&gt;, the focus was purely on allowing for long-lived transactions and the orchestration of a series of events that related to a specific process. Earlier this year, the first release of a new declarative programming style for sagas using a fluent interface was put into the trunk, making it possible to design sagas using a more natural structure and language. This feature alone is one of the more compelling things about sagas in MassTransit.
&lt;/p&gt;&lt;p&gt;
Over the past month or so, I&amp;#39;ve been working on extending the capabilities of sagas in MassTransit beyond the orchestration of directly correlated events (represented as messages). With the latest updates to the trunk, messages that are not directly correlated to the saga can now be observed by the saga using either the interface-based or declarative state machine based saga syntax. This makes for some very compelling functionality as sagas can now &lt;em&gt;observe&lt;/em&gt; other events and react to them!
&lt;/p&gt;&lt;p&gt;
Let&amp;#39;s take a quick look at the syntax using the state machine syntax using a simple message indicating a user has signed out.
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public class UserSignedOut
{
	public string Username { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
This message only contains the username of the user that signed out of the system. If we had a saga that tracked the state of an operation on a web site based on the current user, we could observe this message and release any resources that were being tracked by the saga. For example:
&lt;/p&gt;
&lt;pre name="code" class="csharp"&gt;
public class ShoppingCartSaga : 
	SagaStateMachine
{
	static ShoppingCartSaga()
	{
		Define(()=&amp;gt;
		{
			Correlate(UserSignedOut).By((saga,message) =&amp;gt; saga.Username == message.Username);
	
			During(Active,
				When(UserSignedOut)
					.Then(saga =&amp;gt; saga.ReturnCartItemsToInventory())
					.Complete());
		};
	}

	public static Event UserSignedOut { get; set; }
}
&lt;/pre&gt;
&lt;p&gt;
The Correlate() method is used to define the correlation expression used to match a message to one or more sagas in the saga repository. With this addition, the &lt;a href="http://code.google.com/p/masstransit/source/browse/trunk/MassTransit/Saga/ISagaRepository.cs"&gt;ISagaRepository&lt;/a&gt; has been changed to make it more integrated into the message pipeline, along with the new pipeline message sinks to support this enhanced functionality. The pipeline viewer has also been updated to show the contents of the message pipeline, including all the messages handled by the saga. The Correlate() method can also be used to change how a &lt;a href="http://code.google.com/p/masstransit/source/browse/trunk/MassTransit/CorrelatedBy.cs"&gt;CorrelatedBy&lt;/a&gt; message is also correlated to the saga, making it possible to override the logic on how messages are used to create/use existing saga instances. 
&lt;/p&gt;&lt;p&gt;
The first use case for this new functionality was to improve the subscription service. By making Subscription and SubscriptionClient sagas observe messages from each other, the sagas are able to now complete when they see a node coming back online after a crash or going offline, etc. Since the saga repositories themselves are used to keep track of all the clients and subscriptions in the system, putting that responsibility within the declaration of the saga state machine itself makes for a really clean implementation.
&lt;/p&gt;&lt;p&gt;
These bits are currently in the trunk and will be part of the next release drop of MassTransit. I encourage you to check them out and provide some feedback on any issues. Some of this may be extended soon to also allow for the custom definition of the policy associated with how messages should be applied to sagas, such as always creating a new instance or using an existing instance of a saga. Some conventions are currently in place based on the Initial state that select a default saga policy (check out &lt;a href="http://code.google.com/p/masstransit/source/browse/trunk/MassTransit/Saga/ISagaPolicy.cs"&gt;ISagaPolicy&lt;/a&gt; and the classes that implement it for more details there).
&lt;/p&gt;&lt;p&gt;
So check it out and keep the great feedback and comments coming on the message group. The new syntax additions should be added to the &lt;a href="http://masstransit.pbworks.com/"&gt;wiki&lt;/a&gt; soon.
&lt;/p&gt;
&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;a href="http://www.dotnetkicks.com/kick/?title=MassTransit+Saga+Enhancements+for+Event+Processing&amp;url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f22%2fmasstransit-saga-enhancements-for-event-processing.aspx"&gt;&lt;img src="http://www.dotnetkicks.com/Services/Images/KickItImageGenerator.ashx?url=http%3a%2f%2fwww.lostechies.com%2fblogs%2fchris_patterson%2farchive%2f2009%2f05%2f22%2fmasstransit-saga-enhancements-for-event-processing.aspx" border="0" alt="Kick It on DotNetKicks.com" /&gt;&lt;/a&gt;&lt;img src="http://www.lostechies.com/aggbug.aspx?PostID=21187" width="1" height="1"&gt;</description></item></channel></rss>