<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:georss="http://www.georss.org/georss" xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr="http://purl.org/syndication/thread/1.0" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0"><channel><atom:id>tag:blogger.com,1999:blog-15136575</atom:id><lastBuildDate>Sat, 28 Jan 2012 00:02:01 +0000</lastBuildDate><category>WCF Facility</category><category>Architecture</category><category>Log4net</category><category>Forms Authentication</category><category>DDD7</category><category>RavenDb</category><category>msbuild</category><category>MSTest</category><category>MetaWeblog API</category><category>Ajax</category><category>Unit Tests</category><category>software factories</category><category>Rap</category><category>Live Writer</category><category>Google maps</category><category>AltNetUK</category><category>MVCContrib</category><category>MEF</category><category>Web Design</category><category>DDD8</category><category>Task Parallel Library</category><category>Visual Tools</category><category>WTF</category><category>LINQ to SQL</category><category>EasyNetQ</category><category>ALT.NET UK</category><category>Extension methods</category><category>jQuery</category><category>CSS</category><category>IoC</category><category>REST</category><category>Windsor</category><category>Multi-tenancy</category><category>Photosynth</category><category>Web Services</category><category>IServiceLocator</category><category>ALT.NET</category><category>sussex geek dinner</category><category>F#</category><category>Google</category><category>Web Hosting</category><category>visual studio</category><category>Suteki Shop</category><category>RSD</category><category>WCF</category><category>Castle Windsor</category><category>MVC Framework</category><category>Linq</category><category>PDC</category><category>Validation</category><category>Load Testing</category><category>NHibernate</category><category>asp.net</category><category>IE</category><category>Monorail</category><category>IRepository</category><category>ADO.NET Data Services</category><category>REMIX</category><category>blogging</category><category>custom iterators</category><category>C# 4.0</category><title>Code rant</title><description>Life as a mort.</description><link>http://mikehadlow.blogspot.com/</link><managingEditor>noreply@blogger.com (Mike Hadlow)</managingEditor><generator>Blogger</generator><openSearch:totalResults>399</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/CodeRant" /><feedburner:info uri="coderant" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-4318190308115639168</guid><pubDate>Tue, 27 Sep 2011 09:07:00 +0000</pubDate><atom:updated>2011-09-27T10:07:48.885+01:00</atom:updated><title>Some Thoughts On Service Oriented Architecture (Part 2)</title><description>&lt;p&gt;I’ve been writing a high-level ‘architectural vision’ document for my current clients. I thought it might be nice to republish bits of it here. This is part 2. &lt;a href="http://mikehadlow.blogspot.com/2011/09/some-thoughts-on-service-oriented.html"&gt;The first part is here&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;My Client has a core product that is heavily customised for each customer. In this post we look at the different kinds of components that make up this architecture. How some are common services that any make up the core product, and how other components might be bespoke pieces for a particular customer. We also examine the difference between workflow, services and endpoints.&lt;/p&gt;  &lt;p&gt;It is very important that we make a clear distinction between components that we write as part of the product and bespoke components that we write for a particular customer. We should not put customer specific code into product components and we should not replicate common product code in customer specific pieces.&lt;/p&gt;  &lt;p&gt;Because we are favouring small single-purpose components over large multi-purpose monolithic applications, it should be easy for us to differentiate between product and customer pieces.&lt;/p&gt;  &lt;p&gt;There are three main kinds of components that make up a working system. Services, workflow and endpoints. The diagram below shows how they communicate via &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt;, our open-source infrastructure layer. The green parts are product pieces. The blue parts are bespoke customer specific pieces.&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh3.ggpht.com/-Vqj19RM02QQ/ToGSYjorb2I/AAAAAAAABTs/gvwsfH7zy84/s1600-h/image%25255B4%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://lh4.ggpht.com/--f0vXqCEKYk/ToGSZBmOaLI/AAAAAAAABTw/-1FakQF6CP8/image_thumb%25255B2%25255D.png?imgmax=800" width="589" height="343" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Services&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Services are components that implement a piece of the core product. An example of a service is a component called Renderer that takes templates and data and does a kind of mail-merge. Because Renderer is a service it should never contain any client specific code. Of course customer requirements might mean that enhancements need to be made to Renderer, but these enhancements should always be done with the understanding that Renderer is part of the product. We should be able to deploy the enhanced Renderer to all our customers without the enhancement affecting them.&lt;/p&gt;  &lt;p&gt;Services (in fact all components) should maintain their own state using a service specific database. This database should &lt;em&gt;not&lt;/em&gt; be shared with other services. The service should communicate via EasyNetQ with other services and not use a shared database as a back-channel. In the case of an updated Renderer, templates would be stored in Renderer’s local database. Any new or updated templates would arrive as messages via EasyNetQ. Each data item to be rendered would also arrive as a message, and once the data has been rendered, the document should also be published via EasyNetQ. &lt;/p&gt;  &lt;p&gt;The core point here is that each service should have a clear API, defined by the message types that it subscribes to and publishes. We should be able to fully exercise a component via messages independently of other services. Because the service’s database is only used by the service, we should be able to flexibly modify its schema in response to changing requirements, without having to worry about the impact that will have on other parts of the system.&lt;/p&gt;  &lt;p&gt;It’s important that services do not implement workflow. As we’ll see in the next section, a core feature of this architecture is that workflow and services are separate. Render, for example, should not make decisions about what happens to a document after it is rendered, or implement batching logic. These are separate concerns.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Workflow&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Workflow components are customer specific components that describe what happens in response to a specific business trigger. They also implement customer specific business rules. For an airline, an example would be workflow that is triggered by a flight event, say a delay. When the component receives the delay message, it might first retrieve the manifest for the flight by sending a request to a manifest service, then render a message telling each passenger about the delay by sending a render request to renderer, then finally send that message by email by publishing an email request. It would typically implement business rules describing when a delay is considered important etc.&lt;/p&gt;  &lt;p&gt;By separating workflow from services, we can flexibly implement customer requirements by creating custom workflows without having to customise our services. We can deliver bespoke customer solutions on a common product platform.&lt;/p&gt;  &lt;p&gt;We call these workflow pieces, ‘Sagas’, this is a commonly used term in the industry for a long-running business process. Because sagas all need a common infrastructure for hosting, EasyNetQ includes a ‘SagaHost’. SagaHost is a Windows service that hosts sagas, just like it says on the box. This means that the sagas themselves are written as simple assemblies that can be xcopy deployed.&lt;/p&gt;  &lt;p&gt;Sagas will usually require a database to store their state. Once again, this should be saga specific and not a database shared by other services. However a single customer workflow might well consist of several distinct sagas, it makes sense for these to be thought of as a unit. These may well share a single database.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Endpoints&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Endpoints are components that communicate with the outside world. They are a bridge between our internal AMQP messaging infrastructure and our external HTTP API. The only way into and out of our product should be via this API. We want to be able to integrate with diverse customer systems, but these integration pieces should be implemented as bridges between the customer system and our official API, rather than as bespoke pieces that publish or subscribe directly to the message bus.&lt;/p&gt;  &lt;p&gt;Endpoints come in two flavours, externally triggered and internally triggered. An externally triggered endpoint is where communication is initiated by the customer. An example of this would be flight event. These components are best implemented as web services that simply wait to be called and then publish an appropriate message using EasyNetQ.&lt;/p&gt;  &lt;p&gt;An internally triggered endpoint is where communication is triggered by an internal event. An example of this would be the completion of a workflow with the final step being an update of a customer system. The API would be implemented as a Windows Service that subscribes to the update message using EasyNetQ and implements an HTTP client that makes a web service request to a configured endpoint.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The Importance of Testability&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A core requirement for any component is that it should be testable. It should be possible to test Services and Workflow (Sagas) simply by sending them messages and checking that they respond with the correct messages. Because ‘back-channel’ communication, especially via shared databases, is not allowed we can treat these components as black-boxes that always respond with the same output to the same input.&lt;/p&gt;  &lt;p&gt;Endpoints are slightly more complicated to test. It should be possible to send an externally triggered endpoint a request and watch for the message that’s published. An internally triggered endpoint should make a web request when it receives the correct message.&lt;/p&gt;  &lt;p&gt;Developers should provide automated tests to QA for any modification to a component.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-4318190308115639168?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/PO5qNJ7UuQA/some-thoughts-on-service-oriented_27.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/--f0vXqCEKYk/ToGSZBmOaLI/AAAAAAAABTw/-1FakQF6CP8/s72-c/image_thumb%25255B2%25255D.png?imgmax=800" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/09/some-thoughts-on-service-oriented_27.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-4715368560262774045</guid><pubDate>Mon, 26 Sep 2011 11:54:00 +0000</pubDate><atom:updated>2011-09-26T12:54:32.526+01:00</atom:updated><title>Some Thoughts On Service Oriented Architecture</title><description>&lt;p&gt;I’ve been writing a high-level ‘architectural vision’ document for my current clients. I thought it might be nice to republish bits of it here. This is the section that makes a justification a service oriented architecture based on messaging.&lt;/p&gt;  &lt;p&gt;I’ve taken out anything client-specific.&lt;/p&gt;  &lt;p&gt;SOA is one of those snake-oil consultancy terms that seem to mean different things depending on who you talk to. My own views on SOA have been formed from three main sources. Firstly there’s the bible on SOA, Hohpe &amp;amp; Woolf’s &lt;a href="http://www.eaipatterns.com/"&gt;Enterprise Integration Patterns&lt;/a&gt;. This really is essential reading for anyone attempting to get systems to work together. Next there’s the work of &lt;a href="http://www.udidahan.com/"&gt;Udi Dahan&lt;/a&gt;. He the author of the excellent &lt;a href="http://www.nservicebus.com/"&gt;NServiceBus&lt;/a&gt; messaging framework for .NET. I’ve attended his course, and &lt;a href="http://www.udidahan.com/?blog=true"&gt;his blog&lt;/a&gt; is a fantastic mine of knowledge on all things SOA. Lastly there is the fantastic series of blog posts by &lt;a href="http://bill-poole.blogspot.com/"&gt;Bill Poole on SOA&lt;/a&gt;. Just check &lt;a href="http://bill-poole.blogspot.com/2008/05/jbows-is-bad.html"&gt;JBOWS is Bad for a taster&lt;/a&gt;. &lt;/p&gt;  &lt;p&gt;So what is SOA? Software has a complexity problem. There appears to be a geometric relationship between the complexity of a monolithic system and its stability and maintainability. So a system that is twice as complex as another one will be maybe four times more expensive to maintain and a quarter as stable. There is also the human and organisational side of software. Individual teams tend to build or buy their own solutions. The organisation then needs to find ways for these disparate systems to share information and workflow. Small single purpose systems are always a better choice than large monolithic ones. If we build our systems as components, we can build and maintain them independently. SOA is a set of design patterns that guide us in building and integrating these mini-application pieces.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;What is a component?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;A component in SOA terms is very different from a component in Object-Oriented terms. A component in OO terms is a logical component that is not independently compiled and deployed. A component or ‘service’ in SOA is a physical component that is an independently built and deployed stand-alone application. It is usually designed as a Windows service or possibly as a web service or an executable. It may or may not have a UI, but it will have some mechanism to communicate with other services.&lt;/p&gt;  &lt;p&gt;Each component/service should have the following characteristics:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Encapsulated. The service should not share its internal state with the outside world. The most common way people break this rule is by having services share a single database. The service should only change state in response to input from its public interface. &lt;/li&gt;    &lt;li&gt;Contract. The service should have a clear contract with the outside world. Typically this will be a web-service API and/or the message types that it publishes and consumes. &lt;/li&gt;    &lt;li&gt;Single Purpose. A component should have one job within the system as a whole. Rendering PDFs for example. &lt;/li&gt;    &lt;li&gt;Context Free. A component should not be dependent on other components, it should only depend on contracts. For example, if my business process component relies on getting a flight manifest from somewhere, it should simply be able to publish a flight manifest request, it shouldn’t expect a specific flight manifest service to be present. &lt;/li&gt;    &lt;li&gt;Independently deployable. I should be able to deploy the component/service independently. &lt;/li&gt;    &lt;li&gt;Independently testable. It should be possible to test the service in isolation without having other services in the system running. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;&lt;strong&gt;How do components communicate?&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Now that we’ve defined the characteristics of a component in our SOA, the next challenge is to select the technology and patterns that they use to communicate with each other. We can list the things that we want from our communication technology:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Components should only be able to communicate via a well defined contract - logically-decoupled. They should not be able to dip into each other’s internal state. &lt;/li&gt;    &lt;li&gt;Components should not have to be configured to communicate with specific endpoints – decoupled configuration. It should be possible to deploy a new service without having to reconfigure the services that it talks to. &lt;/li&gt;    &lt;li&gt;The communication technology should support ‘temporal-decoupling’. This means that all the services do not have to be running at the same time for the system to work. &lt;/li&gt;    &lt;li&gt;The communication should be low latency. There should be a minimal delay between a component sending a message and the consumer receiving it. &lt;/li&gt;    &lt;li&gt;The communication should be based on open standards. &lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Let’s consider how some common communication technologies meet these criteria…&lt;/p&gt;  &lt;table border="0" cellspacing="0" cellpadding="2" width="400"&gt;&lt;tbody&gt;     &lt;tr&gt;       &lt;td valign="top" width="66"&gt;&amp;#160;&lt;/td&gt;        &lt;td valign="top" width="66"&gt;logically decoupled&lt;/td&gt;        &lt;td valign="top" width="66"&gt;decoupled configuration&lt;/td&gt;        &lt;td valign="top" width="66"&gt;temporal decoupling&lt;/td&gt;        &lt;td valign="top" width="66"&gt;low latency&lt;/td&gt;        &lt;td valign="top" width="66"&gt;open standards&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;File Transfer&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;Shared Database&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;RPC (remoting COM+)&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;SOAP Web Services&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;Message Queue (MSMQ)&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;no&lt;/td&gt;     &lt;/tr&gt;      &lt;tr&gt;       &lt;td valign="top" width="66"&gt;Pub/Sub Messaging (AMQP)&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;        &lt;td valign="top" width="66"&gt;yes&lt;/td&gt;     &lt;/tr&gt;   &lt;/tbody&gt;&lt;/table&gt;  &lt;p&gt;&amp;#160;&lt;/p&gt;  &lt;p&gt;From the table we can see that there’s only one technology that ticks all our boxes, and that is pub/sub messaging based on an open standard like &lt;a href="http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol"&gt;AMQP&lt;/a&gt;. AMQP is a bit like HTTP for messaging, an open wire-level protocol for brokers and their clients. The leading AMQP implementation is &lt;a href="http://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt;, and this is technology we’ve chosen as our core messaging platform.&lt;/p&gt;  &lt;p&gt;There is a caveat however. For communicating with external clients and their systems, building endpoints with open-standards outweighs all other considerations. Now AMQP is indeed an open standard, but it’s a relatively new one that’s only supported by a handful of products. To be confident that we can interoperate with a wide variety of 3rd party systems over the internet we need a ubiquitous technology, so for all external communication we will use HTTP based web-services.&lt;/p&gt;  &lt;p&gt;So, AMQP internally, HTTP externally.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-4715368560262774045?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/N1201FM0QU0/some-thoughts-on-service-oriented.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>3</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/09/some-thoughts-on-service-oriented.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2256847369307522507</guid><pubDate>Wed, 14 Sep 2011 09:50:00 +0000</pubDate><atom:updated>2011-09-14T10:50:51.615+01:00</atom:updated><title>Thoughts on Windows 8 (part 2)</title><description>&lt;p&gt;Back in June &lt;a href="http://mikehadlow.blogspot.com/2011/06/some-thoughts-on-windows-8.html"&gt;I wrote some thoughts on Windows 8&lt;/a&gt; after the initial announcement. &lt;a href="http://msdn.microsoft.com/en-us/windows/home/"&gt;Now that we’ve got more details&lt;/a&gt; from the Build conference, I thought I’d do a little update.&lt;/p&gt;  &lt;p&gt;Microsoft have climbed down and made a significant concession to WPF/Silverlight/.NET devs. Gone is the previous message that Metro applications will only be written in HTML/Javascript, developers can now choose which technology they want to use on the new platform. There still seems to be a bias towards HTML/Javascript judging my the number of sessions on each however, and it seems like MS would prefer developers to go down the HTML/Javascript route. How much this double headed personality effects the development experience is yet to be seen.&lt;/p&gt;  &lt;p&gt;Somebody high up in MS must have banged some heads together to get the Windows and Developer Divisions talking to each other. They'd become two opposing camps after the fallout from the failed Vista WinFX experiment. Now Windows is forced to support XAML/.NET and Dev-Division arm-wrestled into supporting the HTML/Javascript model in Visual Studio and Blend. Once again the depth of this rapprochement will be the deciding factor when it comes to getting a consistent message across to us developers.&lt;/p&gt;  &lt;p&gt;The message is still clear though; Javascript has won the latest round of the language wars, whatever you think about it as a language, it's becoming as ubiquitous as C. But .NET developers are going to have to be dragged kicking and screaming to this party.&lt;/p&gt;  &lt;p&gt;The big question is still 'will it work?' Will Windows 8 be enough to get MS back into the game? There are two main problems I can see:&lt;/p&gt;  &lt;p&gt;1. Microsoft has a strategic problem. They make money by selling operating systems. Their two main competitors, Google and Apple, don't. Will they be able to make Windows 8 financially attractive to tablet developers when they can get Android licence free and they are competing on price with the dominant iPad? Having said that, Android has been struggling on tablets, so there's still an opportunity for Microsoft to get traction in on that form factor. &lt;/p&gt;  &lt;p&gt;2. Windows 8 is a hybrid. There's the traditional Windows mouse-and-keyboard UI that they have to keep, and there's the new Metro UI that they want everyone to develop for. What's the experience going to be like for a tablet user when they end up in Windows classic, or for the desktop user when they switch to Metro? The development experience for both sets of UI is going to be very different too, almost like developing for two different platforms. It will be interesting if Microsoft sells a business version of Windows 8 without Metro, or indeed a tablet version without the classic UI.&lt;/p&gt;  &lt;p&gt;Despite having raised all these questions, I do think Microsoft have a workable strategy for Windows 8, and it’s going&amp;#160; to be an exciting time over the next few years to see how it pans out. There is still a window (sorry) of opportunity in the tablet form factor for Microsoft to challenge Apple id they can get this right. Let’s hope they can.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2256847369307522507?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/iS5ft9gZ8bc/thoughts-on-windows-8-part-2.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>6</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/09/thoughts-on-windows-8-part-2.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-988287836624279277</guid><pubDate>Tue, 13 Sep 2011 08:55:00 +0000</pubDate><atom:updated>2011-09-13T09:55:29.424+01:00</atom:updated><title>Why Write a .NET API For RabbitMQ?</title><description>&lt;p&gt;Anyone who reads this blog knows that my current focus is writing a .NET API for &lt;a href="http://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt; which I’ve named &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt;. This is being paid for by my excellent clients &lt;a href="http://www.15below.com/"&gt;15Below&lt;/a&gt; who build high volume messaging solutions for the airline industry. EasyNetQ is a core part of their strategy moving forwards, so it’s going to get plenty of real-world use.&lt;/p&gt;  &lt;p&gt;One question I’ve been asked quite a lot, is ‘why?’. Why am I building a .NET API when one is already available; the &lt;a href="http://www.rabbitmq.com/dotnet.html"&gt;C# AMQP client from RabbitHQ&lt;/a&gt;? Think of AMQP as the HTTP of messaging. It’s a relatively low-level protocol. You typically wouldn’t build a web application directly against a low-level HTTP API such as System.Net.WebRequest, instead you would use a higher level toolkit such as WCF or ASP.NET MVC. Think of EasyNetQ as the ASP.NET MVC of AMQP.&lt;/p&gt;  &lt;p&gt;AMQP is designed to be cross platform and language agnostic. It is also designed to flexibly support a wide range of messaging patterns based on the Exchange/Binding/Queue model. It’s great having this flexibility, but with flexibility comes complexity. It means that you will need to write a significant amount of code in order to implement a RabbitMQ client. Typically this code would include:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Implementing messaging patterns such as Publish/Subscribe or Request/Response. Although, to be fair, the .NET client does provide some support here. &lt;/li&gt;    &lt;li&gt;Implement a routing strategy. How will you design your exchange-queue bindings, and how will you route messages between producers and consumers? &lt;/li&gt;    &lt;li&gt;Implement message serialization/deserialization. How will you convert the binary representation of messages in AMQP to something your programming language understands?&lt;/li&gt;    &lt;li&gt;Implement a consumer thread for subscriptions. You will need to have a dedicated consumer loop waiting for messages you have subscribed to. How will you deal with multiple subscribers, or transient subscribers, like those waiting for responses from a request? &lt;/li&gt;    &lt;li&gt;Implement a versioning strategy for your messages. What happens when your message schema needs to change in response to business requirements? &lt;/li&gt;    &lt;li&gt;Implement subscriber reconnection. If the connection is disrupted or the RabbitMQ server bounces, how do you detect it and make sure all your subscriptions are rebuilt? &lt;/li&gt;    &lt;li&gt;Understand and implement quality of service settings. What settings do you need to make to ensure that you have a reliable client. &lt;/li&gt;    &lt;li&gt;Implement an error handling strategy. What should your client do if it receives a malformed message, or if an unexpected exception is thrown? &lt;/li&gt;    &lt;li&gt;Implement monitoring tools. How will you monitor your client applications so that you are alerted if there are any problems?&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;With EasyNetQ, you get all these out-of-the-box. You loose some of the flexibility in exchange for a model based on .NET types for routing, but it saves an awful lot of code.   &lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-988287836624279277?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/POKlrghPOZQ/why-write-net-api-for-rabbitmq.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>11</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/09/why-write-net-api-for-rabbitmq.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2235379738378408464</guid><pubDate>Mon, 12 Sep 2011 14:16:00 +0000</pubDate><atom:updated>2011-09-12T15:16:43.454+01:00</atom:updated><title>Restarting RabbitMQ With Running EasyNetQ Clients</title><description>&lt;p&gt;Here’s a screenshot of one of my tests of &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt; (my easy-to-use .NET API for &lt;a href="http://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt;). I’m running two publishing applications (top) and two subscribing applications (bottom), all publishing and subscribing to the same queue.&amp;#160; We’re getting a throughput of around 5000 messages / second on my local machine. Once they’re all humming along nicely, I bounce the RabbitMQ service. As you can see,&amp;#160; some messages get logged and once the RabbitMQ service comes back, things recover nicely and all the applications continue publishing and subscribing as before. I think that’s pretty sweet :)&lt;/p&gt;  &lt;p&gt;&lt;a href="http://lh5.ggpht.com/-VISEGNb9IaM/Tm4USJXuVvI/AAAAAAAABTk/VDiomgpYZAo/s1600-h/RabbitRestart%25255B6%25255D.png"&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="RabbitRestart" border="0" alt="RabbitRestart" src="http://lh3.ggpht.com/-NNbOxaVUtms/Tm4USom2a8I/AAAAAAAABTo/WnZLsAXuMIU/RabbitRestart_thumb%25255B4%25255D.png?imgmax=800" width="869" height="772" /&gt;&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The publisher and subscriber console apps are both in the EasyNetQ repository on GitHub:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://github.com/mikehadlow/EasyNetQ/blob/master/EasyNetQ.Tests.Performance.Producer/Program.cs"&gt;EasyNetQ.Tests.Performance.Producer&lt;/a&gt;    &lt;br /&gt;&lt;a href="https://github.com/mikehadlow/EasyNetQ/blob/master/EasyNetQ.Tests.Performance.Consumer/Program.cs"&gt;EasyNetQ.Tests.Performance.Consumer&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2235379738378408464?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/q6YgMnju_2E/restarting-rabbitmq-with-running.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh3.ggpht.com/-NNbOxaVUtms/Tm4USom2a8I/AAAAAAAABTo/WnZLsAXuMIU/s72-c/RabbitRestart_thumb%25255B4%25255D.png?imgmax=800" height="72" width="72" /><thr:total>0</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/09/restarting-rabbitmq-with-running.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-6762676177863244053</guid><pubDate>Fri, 26 Aug 2011 10:38:00 +0000</pubDate><atom:updated>2011-08-26T11:38:26.291+01:00</atom:updated><title>How to stop System.Uri un-escaping forward slash characters</title><description>&lt;p&gt;Sometimes you want to construct a URI that has an escaped forward slash. For example, the RabbitMQ Management API requires that you encode the default rabbit VirtualHost ‘/’ as ‘%2f’. Here is the URL to get the details of a queue:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;http://192.168.1.4:55672/api/queues/%2f/EasyNetQ_Default_Error_Queue&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;But if I try use WebRequest or WebClient the ‘%2f’ is un-escaped to a ‘/’, so the URL becomes:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;http://192.168.1.4:55672/api/queues///EasyNetQ_Default_Error_Queue&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;And I get a 404 not found back :(&lt;/p&gt;

&lt;p&gt;Both WebRequest and WebClient use System.Uri internally. It’s easy to demonstrate this behaviour with the following code:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var uri = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(url);&lt;br /&gt;Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;uri = {0}&amp;quot;&lt;/span&gt;, uri.PathAndQuery);&lt;br /&gt;&lt;span style="color: #008000"&gt;// outputs /api/queues///EasyNetQ_Default_Error_Queue&lt;/span&gt;&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;A bit of digging in the System.Uri code thanks to the excellent ReSharper 6.0, &lt;a href="http://stackoverflow.com/questions/2320533/system-net-uri-with-urlencoded-characters/"&gt;and help from this Stack Overflow question&lt;/a&gt;, shows that it’s possible to reset some flags and stop this behaviour. Here’s my LeaveDotsAndSlashesEscaped method (it’s .NET 4.0 specific):&lt;/p&gt;

&lt;div&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; LeaveDotsAndSlashesEscaped()&lt;br /&gt;{&lt;br /&gt;    var getSyntaxMethod = &lt;br /&gt;        &lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt; (UriParser).GetMethod(&lt;span style="color: #006080"&gt;&amp;quot;GetSyntax&amp;quot;&lt;/span&gt;, BindingFlags.Static | BindingFlags.NonPublic);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (getSyntaxMethod == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MissingMethodException(&lt;span style="color: #006080"&gt;&amp;quot;UriParser&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;GetSyntax&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    var uriParser = getSyntaxMethod.Invoke(&lt;span style="color: #0000ff"&gt;null&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] { &lt;span style="color: #006080"&gt;&amp;quot;http&amp;quot;&lt;/span&gt; });&lt;br /&gt;&lt;br /&gt;    var setUpdatableFlagsMethod = &lt;br /&gt;        uriParser.GetType().GetMethod(&lt;span style="color: #006080"&gt;&amp;quot;SetUpdatableFlags&amp;quot;&lt;/span&gt;, BindingFlags.Instance | BindingFlags.NonPublic);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (setUpdatableFlagsMethod == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MissingMethodException(&lt;span style="color: #006080"&gt;&amp;quot;UriParser&amp;quot;&lt;/span&gt;, &lt;span style="color: #006080"&gt;&amp;quot;SetUpdatableFlags&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    setUpdatableFlagsMethod.Invoke(uriParser, &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;[] {0});&lt;br /&gt;}&lt;/pre&gt;
&lt;/div&gt;

&lt;div&gt;
  &lt;br /&gt;The usual caveats of poking into system assemblies with reflection apply. Don’t expect this to work with any other version of .NET than 4.0.&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;Now if we re-run our test…&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;LeaveDotsAndSlashesEscaped();&lt;br /&gt;&lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; url = &lt;span style="color: #006080"&gt;&amp;quot;http://192.168.1.4:55672/api/queues/%2f/EasyNetQ_Default_Error_Queue&amp;quot;&lt;/span&gt;;&lt;br /&gt;var uri = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Uri(url);&lt;br /&gt;Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;uri = {0}&amp;quot;&lt;/span&gt;, uri.PathAndQuery);&lt;br /&gt;// outputs /api/queues/%2f/EasyNetQ_Default_Error_Queue&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;div&gt;Voila! &lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div&gt;If you have a Web.config or App.config file you can also try this configuration setting, which should do the same thing (&lt;a href="http://stackoverflow.com/questions/591694/url-encoded-slash-in-url"&gt;from this SO question&lt;/a&gt;) but I haven’t tried it personally:&lt;/div&gt;

&lt;div&gt;&amp;#160;&lt;/div&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;uri&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;schemeSettings&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #800000"&gt;add&lt;/span&gt; &lt;span style="color: #ff0000"&gt;name&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;http&amp;quot;&lt;/span&gt; &lt;span style="color: #ff0000"&gt;genericUriParserOptions&lt;/span&gt;&lt;span style="color: #0000ff"&gt;=&amp;quot;DontUnescapePathDotsAndSlashes&amp;quot;&lt;/span&gt; &lt;span style="color: #0000ff"&gt;/&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;schemeSettings&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;lt;/&lt;/span&gt;&lt;span style="color: #800000"&gt;uri&lt;/span&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-6762676177863244053?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/-AnLVn5CSlA/how-to-stop-systemuri-un-escaping.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>1</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/08/how-to-stop-systemuri-un-escaping.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-5307403651207664746</guid><pubDate>Mon, 18 Jul 2011 10:50:00 +0000</pubDate><atom:updated>2011-07-18T11:50:26.700+01:00</atom:updated><title>An Action Cache</title><description>&lt;p&gt;Do you ever find yourself in a loop calling a method that expects an Action or a Func as an argument? Here’s an example from an &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt; test method where I’m doing just that:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[Test, Explicit(&lt;span style="color: #006080"&gt;&amp;quot;Needs a Rabbit instance on localhost to work&amp;quot;&lt;/span&gt;)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Should_be_able_to_do_simple_request_response_lots()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; i = 0; i &amp;lt; 1000; i++)&lt;br /&gt;    {&lt;br /&gt;        var request = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TestRequestMessage { Text = &lt;span style="color: #006080"&gt;&amp;quot;Hello from the client! &amp;quot;&lt;/span&gt; + i.ToString() };&lt;br /&gt;        bus.Request&amp;lt;TestRequestMessage, TestResponseMessage&amp;gt;(request, response =&amp;gt;&lt;br /&gt;            Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Got response: '{0}'&amp;quot;&lt;/span&gt;, response.Text));&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    Thread.Sleep(1000);&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;My initial naive implementation of IBus.Request set up a new response subscription each time Request was called. Obviously this is inefficient. It would be much nicer if I could identify when Request is called more than once with the same callback and re-use the subscription.&lt;/p&gt;

&lt;p&gt;The question I had was: how can I uniquely identify each callback? It turns out that action.Method.GetHashcode() reliably identifies a unique action. I can demonstrate this with the following code:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;p&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; UniquelyIdentifyDelegate&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; IDictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, Action&amp;gt; actionCache = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Dictionary&amp;lt;&lt;span style="color: #0000ff"&gt;int&lt;/span&gt;, Action&amp;gt;();&lt;br /&gt;&lt;/p&gt;&lt;p&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; DemonstrateActionCache()&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;for&lt;/span&gt; (var i=0; i &amp;lt; 3; i++)&lt;br /&gt;        {&lt;br /&gt;            RunAction(() =&amp;gt; Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Hello from A {0}&amp;quot;&lt;/span&gt;, i));&lt;br /&gt;            RunAction(() =&amp;gt; Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Hello from B {0}&amp;quot;&lt;/span&gt;, i));&lt;br /&gt;&lt;br /&gt;            Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;&amp;quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; RunAction(Action action)&lt;br /&gt;    {&lt;br /&gt;        Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Mehod = {0}, Cache Size = {1}&amp;quot;&lt;/span&gt;, action.Method.GetHashCode(), actionCache.Count);&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (!actionCache.ContainsKey(action.Method.GetHashCode()))&lt;br /&gt;        {&lt;br /&gt;            actionCache.Add(action.Method.GetHashCode(), action);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        var actionFromCache = actionCache[action.Method.GetHashCode()];&lt;br /&gt;&lt;br /&gt;        actionFromCache();&lt;br /&gt;    }&lt;br /&gt;}&lt;/p&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Here, I’m creating an action cache keyed on the action method’s hashcode. Then I’m calling RunAction a few times with two distinct action delegates. Note that they also close over a variable, i, from the outer scope. &lt;/p&gt;

&lt;p&gt;Running DemonstrateActionCache() outputs the expected result:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Mehod = 59022676, Cache Size = 0&lt;br /&gt;Hello from A 0&lt;br /&gt;Mehod = 62968415, Cache Size = 1&lt;br /&gt;Hello from B 0&lt;br /&gt;&lt;br /&gt;Mehod = 59022676, Cache Size = 2&lt;br /&gt;Hello from A 1&lt;br /&gt;Mehod = 62968415, Cache Size = 2&lt;br /&gt;Hello from B 1&lt;br /&gt;&lt;br /&gt;Mehod = 59022676, Cache Size = 2&lt;br /&gt;Hello from A 2&lt;br /&gt;Mehod = 62968415, Cache Size = 2&lt;br /&gt;Hello from B 2&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Rather nice I think :)&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-5307403651207664746?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/tUrCXyzpprs/action-cache.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>8</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/action-cache.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2253105778331141009</guid><pubDate>Mon, 18 Jul 2011 09:22:00 +0000</pubDate><atom:updated>2011-07-18T10:22:20.136+01:00</atom:updated><title>Task Parallel Library: How To Write a Simple Delay Task</title><description>&lt;p&gt;I just had a need for a delay task. A simple method that I can call to create a task that will turn a Func&amp;lt;T&amp;gt; into a Task&amp;lt;T&amp;gt; that will execute after a given delay.&lt;/p&gt;  &lt;p&gt;The starting point for any Task creation based on an external asynchronous operation, like a Timer callback, is the &lt;a href="http://msdn.microsoft.com/en-us/library/dd449174.aspx"&gt;TaskCompletionSource&lt;/a&gt; class.&amp;#160; It provides methods to transition the task it creates to different states. You call SetResult when the operation is completes, SetException if the operation fails, and SetCancelled if you want to cancel the task.&lt;/p&gt;  &lt;p&gt;Here’s my RunDelayed method:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Task&amp;lt;T&amp;gt; RunDelayed&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; millisecondsDelay, Func&amp;lt;T&amp;gt; func)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (func == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span style="color: #006080"&gt;&amp;quot;func&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (millisecondsDelay &amp;lt; 0)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentOutOfRangeException(&lt;span style="color: #006080"&gt;&amp;quot;millisecondsDelay&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    var taskCompletionSource = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TaskCompletionSource&amp;lt;T&amp;gt;();&lt;br /&gt;&lt;br /&gt;    var timer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Timer(self =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        ((Timer)self).Dispose();&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;        {&lt;br /&gt;            var result = func();&lt;br /&gt;            taskCompletionSource.SetResult(result);&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (Exception exception)&lt;br /&gt;        {&lt;br /&gt;            taskCompletionSource.SetException(exception);&lt;br /&gt;        }&lt;br /&gt;    });&lt;br /&gt;    timer.Change(millisecondsDelay, millisecondsDelay);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; taskCompletionSource.Task;&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;I simply create a new TaskCompletionSource and a Timer where the callback calls SetResult with the result of the given Func&amp;lt;T&amp;gt;. If the Func&amp;lt;T&amp;gt; throws, we simply catch the exception and call SetException. Finally we start the timer and return the Task.&lt;/p&gt;

&lt;p&gt;You would use it like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var task = RunDelayed(1000, () =&amp;gt; &lt;span style="color: #006080"&gt;&amp;quot;Hello World!&amp;quot;&lt;/span&gt;);&lt;br /&gt;task.ContinueWith(t =&amp;gt;&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #008000"&gt;// 'Hello World' is output a second later on a threadpool thread.&lt;/span&gt;&lt;br /&gt;    Console.WriteLine(t.Result);&lt;br /&gt;});&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;You can use the same technique to turn any asynchronous operation into a Task. &lt;/p&gt;

&lt;p&gt;Note however if your operation exposes an &lt;a href="http://mikehadlow.blogspot.com/2011/07/first-rule-of-threading-you-dont-need.html"&gt;APM API&lt;/a&gt;, it’s much easier to use the &lt;a href="http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory.fromasync.aspx"&gt;Task.Factory.FromAsync&lt;/a&gt; method.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2253105778331141009?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/DfgeGRCmono/task-parallel-library-how-to-write.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>4</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/task-parallel-library-how-to-write.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2760092693627977672</guid><pubDate>Thu, 14 Jul 2011 10:28:00 +0000</pubDate><atom:updated>2011-07-15T16:16:53.613+01:00</atom:updated><category domain="http://www.blogger.com/atom/ns#">EasyNetQ</category><title>EasyNetQ: How Should a Messaging Client Handle Errors?</title><description>&lt;p&gt;&lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt; is my simple .NET API for RabbitMQ.&lt;/p&gt;  &lt;p&gt;I’ve started thinking about the best patterns for implementing error handling in EasyNetQ. One of the aims of EasyNetQ is to remove as many infrastructure concerns from the application developer as possible. This means that the API should correctly handle any exceptions that bubble up from the application layer. &lt;/p&gt;  &lt;p&gt;One of the core requirements is that we shouldn’t lose messages when the application throws. The question then becomes: where should the message, that the application was consuming when it threw, go? There seem to be three choices:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Put the failed message back on the queue it was consumed from. &lt;/li&gt;    &lt;li&gt;Put the failed message on an error queue. &lt;/li&gt;    &lt;li&gt;A combination of 1 and 2. &lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;&lt;strong&gt;Option 1&lt;/strong&gt; has the benefit that it’s the out-of-the-box behaviour of AMQP. In the case of EasyNetQ, I would simply catch any exceptions, log them, and just send a noAck command back to RabbitMQ. Rabbit would put the message at the back of the queue and then resend it when it got to the front.&lt;/p&gt;  &lt;p&gt;Another advantage of this technique is that it gives competing consumers the opportunity to process the message. If you have more than one consumer on a queue, Rabbit will send the messages to them in turn, so this is out-of-the-box.&lt;/p&gt;  &lt;p&gt;The drawback of this method is that there’s the possibility of the queue filling up with failed messages. The consumer would just be cycling around throwing exceptions and any messages that it might be able to to consume would be slowed down by having to wait their turn amongst a long queue of failed messages.&lt;/p&gt;  &lt;p&gt;Another problem is that it’s difficult to manually inspect the messages and selectively delete or retry them.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Option 2&lt;/strong&gt; is harder to implement. When an error occurs I would wrap the failed message in a special error message wrapper. This can include details about the type and location of the exception and other information such as stack traces. I would then publish the error message to an error exchange. Each consumer queue should have a matching error exchange. This gives the opportunity to bind generic error queues to all error exchanges, but also to have special case error consumers for particular queues.&lt;/p&gt;  &lt;p&gt;I would need to write an error queue consumer to store the messages in a database. I would then need to provide the user with some way to inspect the messages alongside the error that caused them to arrive in the error queue so that they could make a ignore/retry decision.&lt;/p&gt;  &lt;p&gt;I could also implement some kind of wait-and-retry function on the error queue, but that would also add additional complexity.&lt;/p&gt;  &lt;p&gt;It has the advantage that the original queue remains clear of failing messages. Failed messages and the error condition that caused the failure can be inspected together, and failed messages can be manually ignored or retried. &lt;/p&gt;  &lt;p&gt;With the failed messages sitting in a database, it would also be simple to create a mechanism where those messages could be replayed on a developer machine to aid in debugging.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;A combination of 1 and 2&lt;/strong&gt;. I’m moving towards thinking that a combination of 1 &amp;amp; 2 might be the best strategy. When a message fails initially, we simply noAck it and it goes back to the queue. AMQP provides a Redelivered flag, so when the messages is consumed a second time we can be aware that it’s a retry. Unfortunately there doesn’t seem to be a retry count in AMQP, so the best we can do is allow for a single retry. This has the benefit that it gives a competing consumer a chance to process the message.&lt;/p&gt;  &lt;p&gt;No retry count is a problem. One option some people use is to roll their own ‘nack’ mechanism. In this case, when an error occurs in the consumer, rather than sending a ‘nack’ to Rabbit and relying on the built-in behaviour, the client ‘acks’ the message to remove it from the queue, and then re-publishes it via the default exchange back to the originating queue. Doing this gives the client access to the message and allows a ‘retry count’ header to be set.&lt;/p&gt;  &lt;p&gt;After the single retry we fall back to &lt;strong&gt;Option 2&lt;/strong&gt;. The message is passed to the error queue on the second failure.&lt;/p&gt;  &lt;p&gt;I would be very interested in hearing how other people have implemented error handling with AMQP/RabbitMQ.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Updated based on feedback on the 15th July&lt;/em&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2760092693627977672?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/SDgou1RdmMs/easynetq-how-should-messaging-client.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>4</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/easynetq-how-should-messaging-client.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2895130452893170397</guid><pubDate>Wed, 13 Jul 2011 14:05:00 +0000</pubDate><atom:updated>2011-07-13T15:08:21.514+01:00</atom:updated><title>MEF DirectoryCatalog Fails to Load Assemblies</title><description>&lt;p&gt;I had an interesting problem with the &lt;a href="http://mef.codeplex.com/"&gt;Managed Extensibility Framework&lt;/a&gt; yesterday. I’m using the DirectoryCatalog to load assemblies from a given directory. Pretty standard stuff. When I tested my host on my developer machine, it got the &lt;a href="http://www.codinghorror.com/blog/2007/03/the-works-on-my-machine-certification-program.html"&gt;works on my machine&lt;/a&gt; badge, but when I ran the host on one of our servers, it ignored all the assemblies.&lt;/p&gt;  &lt;p&gt;Nothing loaded …&lt;/p&gt;  &lt;p&gt;Hmm …&lt;/p&gt;  &lt;p&gt;It turns out, after much digging and help from my Twitter crew,&amp;#160; that the assembly loader that MEF’s DirectoryCatalog uses ignores any files that have a &lt;a href="http://msdn.microsoft.com/en-us/library/ms537183(v=vs.85).aspx"&gt;URL Zone&lt;/a&gt; set. I described these zones in detail in my previous post here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://mikehadlow.blogspot.com/2011/07/detecting-and-changing-files-internet.html"&gt;http://mikehadlow.blogspot.com/2011/07/detecting-and-changing-files-internet.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Because we copy our plugins from a file share, Windows was marking them as belonging to the Intranet Zone. Thus the odd only-when-deployed behaviour.&lt;/p&gt;  &lt;p&gt;How you deal with this depends on whether you think that files marked in this way represent a security threat or not. If you do, the best policy is to detect any assemblies in your DirectoryCatalogue directory that have a Zone set and log them. You can do that with the &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.policy.zone.aspx"&gt;System.Security.Policy.Zone&lt;/a&gt; class:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var zone = Zone.CreateFromUrl(&lt;span style="color: #006080"&gt;&amp;quot;file:///C:/temp/ZoneTest.doc&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (zone.SecurityZone != SecurityZone.MyComputer)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;File is blocked&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;br /&gt;Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;zone.SecurityZone = {0}&amp;quot;&lt;/span&gt;, zone.SecurityZone);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;If you don’t consider files copied from elsewhere a security concern, but rather a feature of your operating procedure, then you can clear the Zone flags from all the assemblies in the directory with the help of Richard Deeming’s &lt;a href="https://github.com/hubkey/Trinet.Core.IO.Ntfs"&gt;Trinet.Core.IO.Ntfs library&lt;/a&gt;. I wrote a little class using this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; UrlZoneService&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ClearUrlZonesInDirectory(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; directoryPath)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var filePath &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; Directory.EnumerateFiles(directoryPath))&lt;br /&gt;        {&lt;br /&gt;            var fileInfo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileInfo(filePath);&lt;br /&gt;            fileInfo.DeleteAlternateDataStream(&lt;span style="color: #006080"&gt;&amp;quot;Zone.Identifier&amp;quot;&lt;/span&gt;);&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;I just run this before initiating my DirectoryCatalogue and now network copied assemblies load as expected.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2895130452893170397?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/ln99sihVTOg/mef-directorycatalog-fails-to-load.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>0</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/mef-directorycatalog-fails-to-load.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2105746677988525513</guid><pubDate>Wed, 13 Jul 2011 09:10:00 +0000</pubDate><atom:updated>2011-07-13T10:19:23.480+01:00</atom:updated><title>Detecting and Changing a File’s Internet Zone in .NET: Alternate Data Streams</title><description>&lt;p&gt;I spent most of yesterday investigating some weird behaviour in MEF, which I’ll discuss in another post. I was saved by Twitter in the guise of @Grumpydev, @jordanterrell and @SQLChap who came to the rescue and led me down a very interesting rabbit hole, to a world of URL Zones and Alternate Data Streams. Thanks chaps!&lt;/p&gt;  &lt;p&gt;If you download a file from the internet on Windows 2003 or later, right click, and select properties, you’ll see something like this:&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="BlockedFile" border="0" alt="BlockedFile" src="http://lh6.ggpht.com/-rCm1nnrkaRw/Th1hA8kVaII/AAAAAAAABTg/vb_-UFZ8dG8/BlockedFile%25255B5%25255D.png?imgmax=800" width="377" height="513" /&gt;&lt;/p&gt;  &lt;p&gt;The file is ‘blocked’ which means that you will get various dialogues if you try to say, run an executable with this flag set.&lt;/p&gt;  &lt;p&gt;Any file on NTFS can have a ‘Zone’ as the flag is called. The values are described in &lt;a href="http://msdn.microsoft.com/en-us/library/ms537175.aspx"&gt;this enumeration&lt;/a&gt;:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;typedef &lt;span style="color: #0000ff"&gt;enum&lt;/span&gt; tagURLZONE {&lt;br /&gt;  URLZONE_INVALID          = -1,&lt;br /&gt;  URLZONE_PREDEFINED_MIN   = 0,&lt;br /&gt;  URLZONE_LOCAL_MACHINE    = 0,&lt;br /&gt;  URLZONE_INTRANET,&lt;br /&gt;  URLZONE_TRUSTED,&lt;br /&gt;  URLZONE_INTERNET,&lt;br /&gt;  URLZONE_UNTRUSTED,&lt;br /&gt;  URLZONE_PREDEFINED_MAX   = 999,&lt;br /&gt;  URLZONE_USER_MIN         = 1000,&lt;br /&gt;  URLZONE_USER_MAX         = 10000 &lt;br /&gt;} URLZONE;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The Zone is not standard security information stored in the file’s ACL. Instead it uses a little known feature of NTFS, ‘&lt;a href="http://www.symantec.com/connect/articles/windows-ntfs-alternate-data-streams"&gt;Alternate Data Streams&lt;/a&gt;’ (ADS). &lt;/p&gt;

&lt;p&gt;Sysinternals provide a command line utility &lt;a href="http://technet.microsoft.com/en-us/sysinternals/bb897440"&gt;streams.exe&lt;/a&gt; that you can use to inspect and remove ADSs, including the Zone flag, on a file or a whole directory tree of files.&lt;/p&gt;

&lt;p&gt;You can access a file’s Zone in .NET by using the &lt;a href="http://msdn.microsoft.com/en-us/library/system.security.policy.zone.aspx"&gt;System.Security.Policy.Zone&lt;/a&gt; class. Like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var zone = Zone.CreateFromUrl(&lt;span style="color: #006080"&gt;&amp;quot;file:///C:/temp/ZoneTest.doc&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (zone.SecurityZone != SecurityZone.MyComputer)&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;File is blocked&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;br /&gt;Console.Out.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;zone.SecurityZone = {0}&amp;quot;&lt;/span&gt;, zone.SecurityZone);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;If you want to create, view and delate ADSs in .NET you will need to resort to pInvoke, there is no support for them in the BCL. Luckily for us, Richard Deeming, has done the work for us and created a set of classes that wrap the NTFS API. You can read about it &lt;a href="http://www.codeproject.com/KB/cs/ntfsstreams.aspx"&gt;here&lt;/a&gt; and get the code from GitHub &lt;a href="https://github.com/hubkey/Trinet.Core.IO.Ntfs"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Using Richard’s library, you can list the ADSs for a file and their values like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var fileInfo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileInfo(path);&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var alternateDataStream &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; fileInfo.ListAlternateDataStreams())&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;{0} - {1}&amp;quot;&lt;/span&gt;, alternateDataStream.Name, alternateDataStream.Size);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;// Read the &amp;quot;Zone.Identifier&amp;quot; stream, if it exists:&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (fileInfo.AlternateDataStreamExists(&lt;span style="color: #006080"&gt;&amp;quot;Zone.Identifier&amp;quot;&lt;/span&gt;))&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Found zone identifier stream:&amp;quot;&lt;/span&gt;);&lt;br /&gt;&lt;br /&gt;    var s = fileInfo.GetAlternateDataStream(&lt;span style="color: #006080"&gt;&amp;quot;Zone.Identifier&amp;quot;&lt;/span&gt;,FileMode.Open);&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt; (TextReader reader = s.OpenText())&lt;br /&gt;    {&lt;br /&gt;        Console.WriteLine(reader.ReadToEnd());&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;span style="color: #0000ff"&gt;else&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;No zone identifier stream found.&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;When I run this against a file downloaded from the internet I get this output:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Zone.Identifier - 26&lt;br /&gt;Found zone identifier stream:&lt;br /&gt;[ZoneTransfer]&lt;br /&gt;ZoneId=3&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;You can see that the ZoneId = 3, so this file’s Zone is URLZONE_INTERNET.&lt;/p&gt;

&lt;p&gt;You can delete an ADS like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var fileInfo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileInfo(path);&lt;br /&gt;fileInfo.DeleteAlternateDataStream(&lt;span style="color: #006080"&gt;&amp;quot;Zone.Identifier&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;And lastly you can set the ZoneId like this. Here I’m changing a file to have a internet zone:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var fileInfo = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; FileInfo(path);&lt;br /&gt;&lt;br /&gt;var ads = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; AlternateDataStreamInfo(path, &lt;span style="color: #006080"&gt;&amp;quot;Zone.Identifier&amp;quot;&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;, &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var stream = ads.OpenWrite())&lt;br /&gt;&lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var writer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; StreamWriter(stream))&lt;br /&gt;{&lt;br /&gt;    writer.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;[ZoneTransfer]&amp;quot;&lt;/span&gt;);&lt;br /&gt;    writer.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;ZoneId=3&amp;quot;&lt;/span&gt;);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;ADSs are very interesting, and open up a whole load of possibilities. Imagine storing application specific metadata in an ADS for example. I’d be very interested to hear if anyone has used them in this way.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2105746677988525513?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/t98CfcPZvuY/detecting-and-changing-files-internet.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-rCm1nnrkaRw/Th1hA8kVaII/AAAAAAAABTg/vb_-UFZ8dG8/s72-c/BlockedFile%25255B5%25255D.png?imgmax=800" height="72" width="72" /><thr:total>5</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/detecting-and-changing-files-internet.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-5555004231201844042</guid><pubDate>Mon, 11 Jul 2011 11:03:00 +0000</pubDate><atom:updated>2011-07-11T12:03:26.102+01:00</atom:updated><title>RabbitMQ Subscriptions with the DotNet Client</title><description>&lt;p&gt;&lt;a href="http://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt; comes with a nice .NET client called, appropriately enough, ‘&lt;a href="http://www.rabbitmq.com/dotnet.html"&gt;RabbitMQ DotNet Client&lt;/a&gt;’. It does a good job of implementing the AMQP protocol in .NET and comes with excellent documentation, which is good because there are some interesting subtleties in its usage. This is because AMQP is designed with flexibility in mind and supports a mind boggling array of possible messaging patterns. But as with any API, with flexibility comes complexity.&lt;/p&gt;  &lt;p&gt;The aim of &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ&lt;/a&gt;, my simple messaging API for RabbitMQ on .NET, is to hide much of this complexity and provide a very simple to use interface. But in order to make it simple I have had to take away much of the flexibility of AMQP and instead provide a strongly opinionated view of one way of using RabbitMQ with .NET.&lt;/p&gt;  &lt;p&gt;Today I’m going to discuss how Subscriptions work with the RabbitMQ DotNet Client&amp;#160; (RDC) and some of the choices that I’ve made in EasyNetQ. &lt;/p&gt;  &lt;p&gt;You create a subscription using the RDC with the AMQP command ‘basic consume’. You pass in the name of the queue you want to consume from. &lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;channel.BasicConsume(ackNackQueue, noAck, consumer);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;If you use the default QueueingBasicConsumer, the RabbitMQ server then takes messages from the queue you specified and sends them over the network to the RDC. The RDC has a dedicated worker thread that listens to a TCP socket and pulls the messages off as they arrive and places them on a shared thread-safe queue. The client application, in my case EasyNetQ, pulls messages off the shared queue on its own thread and processes them as required. Once it has processed the message it can acknowledge that it has completed by sending an AMQP ‘basic ack’ command. At that point the RabbitMQ server removes the message from its queue.&lt;/p&gt;

&lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="RabbitMQDotNetClient" border="0" alt="RabbitMQDotNetClient" src="http://lh6.ggpht.com/-WLdYFN585f8/ThrYfbiaTjI/AAAAAAAABTc/kbYWspq0P2Y/RabbitMQDotNetClient%25255B10%25255D.png?imgmax=800" width="323" height="436" /&gt;&lt;/p&gt;



&lt;p&gt;Now, what happens if messages are arriving faster than the user application can process them? The shared queue will gradually fill up with messages and eventually the process will run out of memory. That’s a bad thing. To fix this, you can limit the number of messages that RabbitMQ will send to the RDC before they are acknowledged with the Quality of Service prefetchCount setting.&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;channel.BasicQos(0, prefetchCount, &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The default value for prefetchCount is zero, which means that there is no limit. If you set prefetchCount to any other positive value, that will be the maximum number of messages that the RDC’s queue will hold at any one time. Setting the prefectchCount to a reasonably high number will allow RabbitMQ to more efficiently stream messages across the network.&lt;/p&gt;

&lt;p&gt;What happens if the shared queue is full of messages and my client application crashes? Won’t all the messages be lost? No, because messages are only removed from the RabbitMQ queue when the user application sends the basic ack message. The messages queued in the RDC’s shared queue are not acknowledged and so will not yet have been removed from the RabbitMQ queue.&lt;/p&gt;

&lt;p&gt;However, if when you call ‘basic consume’ you pass in true for ‘noAck’ then the messages will be removed from the RabbitMQ queue as they are transmitted across the network. You would use this setting if you’re not worried about loosing some messages, but need them to be transmitted as efficiently as possible.&lt;/p&gt;

&lt;p&gt;For EasyNetQ, I’ve made the default settings as follows: 1000 messages for the prefetchCount and noAck to be false. I’m assuming that most users will value reliability over performance. Eventually I hope to provide some dial with setting like ‘high throughput, low reliability’, ‘low throughput, high reliability’, but for now I’m going for reliability.&lt;/p&gt;

&lt;p&gt;I’d be very interested to hear from anyone who’s using RabbitMQ with .NET and how they have configured these settings.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-5555004231201844042?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/NI8a2kjdKGc/rabbitmq-subscriptions-with-dotnet.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-WLdYFN585f8/ThrYfbiaTjI/AAAAAAAABTc/kbYWspq0P2Y/s72-c/RabbitMQDotNetClient%25255B10%25255D.png?imgmax=800" height="72" width="72" /><thr:total>2</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/rabbitmq-subscriptions-with-dotnet.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-1464299996301677375</guid><pubDate>Sun, 10 Jul 2011 10:34:00 +0000</pubDate><atom:updated>2011-07-10T11:34:37.933+01:00</atom:updated><title>What is a Closure?</title><description>&lt;p&gt;This question came up at the last Brighton ALT.NET Beers. It proved almost impossible to discuss in words without seeing some code, so here’s my attempt to explain closures in C#. &lt;a href="http://en.wikipedia.org/wiki/Closure_(computer_science)"&gt;Wikipedia says&lt;/a&gt;:&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;In &lt;a href="http://en.wikipedia.org/wiki/Computer_science"&gt;computer science&lt;/a&gt;, a &lt;b&gt;closure&lt;/b&gt; (also &lt;b&gt;lexical closure&lt;/b&gt;, &lt;b&gt;function closure&lt;/b&gt; or &lt;b&gt;function value&lt;/b&gt;) is a &lt;a href="http://en.wikipedia.org/wiki/Function_(computer_science)"&gt;function&lt;/a&gt; together with a referencing environment for the nonlocal names (&lt;a href="http://en.wikipedia.org/wiki/Free_variables_and_bound_variables"&gt;free variables&lt;/a&gt;) of that function. Such a function is said to be &amp;quot;closed over&amp;quot; its free variables. The referencing environment &lt;a href="http://en.wikipedia.org/wiki/Name_binding"&gt;binds&lt;/a&gt; the nonlocal names to the corresponding variables in &lt;a href="http://en.wikipedia.org/wiki/Scope_(computer_science)"&gt;scope&lt;/a&gt; at the time the closure is created, additionally extending their lifetime to at least as long as the lifetime of the closure itself.&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;So a closure is a function that ‘captures’ or ‘closes over’ variables that it references from the scope in which it was created. Yes, hard to picture, but actually much easier to understand when you see some code.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var x = 1;&lt;br /&gt;&lt;br /&gt;Action action = () =&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt;&lt;br /&gt;{&lt;br /&gt;    var y = 2;&lt;br /&gt;    var result = x + y;&lt;br /&gt;    Console.Out.WriteLine(&amp;quot;result = {0}&amp;quot;, result);&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;action();&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Here we first define a variable ‘x’ with a value of 1. We then define an anonymous function delegate (a lambda expression) of type Action. Action takes no parameters and returns no result, but if you look at the definition of ‘action’, you can see that ‘x’ is used. It is ‘captured’ or ‘closed over’ and automatically added to action’s environment.&lt;/p&gt;

&lt;p&gt;When we execute action it prints out the expected result. Note that the original ‘x’ can be out of scope by the time we execute action and it will still work.&lt;/p&gt;

&lt;p&gt;It’s interesting to look at ‘action’ in the debugger. We can see that the C# compiler has created a Target class for us and populated it with x:&lt;/p&gt;

&lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="closure_in_debugger" border="0" alt="closure_in_debugger" src="http://lh6.ggpht.com/-uopkxLJf7N0/ThmAPRDDZ4I/AAAAAAAABTY/8FHLcCPT_vM/closure_in_debugger%25255B5%25255D.png?imgmax=800" width="540" height="287" /&gt;&lt;/p&gt;

&lt;p&gt;Closures (along with higher order functions) are incredibly useful. If you’ve ever done any serious Javascript programming you’ll know that they can be used to &lt;a href="http://mikehadlow.blogspot.com/2010/12/javascript-defining-classes-with.html"&gt;replace much of the functionality of object oriented languages&lt;/a&gt; like C#. I wrote an example playing with &lt;a href="http://mikehadlow.blogspot.com/2010/12/c-closure-constructors.html"&gt;this idea in C# a while back&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As usual, John Skeet covers closures in far more detail. Check &lt;a href="http://csharpindepth.com/Articles/Chapter5/Closures.aspx"&gt;this chapter from C# in Depth&lt;/a&gt; for more information, including the common pitfalls you can run into.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-1464299996301677375?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/Gz8jFOburP8/what-is-closure.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-uopkxLJf7N0/ThmAPRDDZ4I/AAAAAAAABTY/8FHLcCPT_vM/s72-c/closure_in_debugger%25255B5%25255D.png?imgmax=800" height="72" width="72" /><thr:total>8</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/what-is-closure.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-1786671109577390961</guid><pubDate>Tue, 05 Jul 2011 14:13:00 +0000</pubDate><atom:updated>2011-07-05T15:16:16.485+01:00</atom:updated><title>The First Rule of Threading: You Don’t Need Threads!</title><description>&lt;p&gt;I’ve recently been introduced to a code base that illustrates a very common threading anti-pattern. Say you’ve got a batch of data that you need to process, but processing each item takes a significant amount of time. Doing each item sequentially means that the entire batch takes an unacceptably long time. A naive approach to solving this problem is to create a new thread to process each item. Something like this:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var item &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; batch)&lt;br /&gt;{&lt;br /&gt;    var itemToProcess = item;&lt;br /&gt;    var thread = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Thread(_ =&amp;gt; ProcessItem(itemToProcess));&lt;br /&gt;    thread.Start();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;The problem with this is that each thread takes significant resources to setup and maintain. If there are hundreds of items in the batch we could find ourselves short of memory.&lt;/p&gt;

&lt;p&gt;It’s worth considering &lt;em&gt;why&lt;/em&gt; ProcessItem takes so long. Most business applications don’t do processor intensive work. If you’re not protein folding, the reason your process is talking a long time is usually because it’s waiting on IO – communicating with the database or web services somewhere, or reading and writing files. Remember, IO operations aren’t somewhat slower than processor bound ones, they are many many orders of magnitude slower. As Gustavo Duarte says in his excellent post &lt;a href="http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait"&gt;What Your Computer Does While You Wait&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Reading from L1 cache is like grabbing a piece of paper from your desk (3 seconds), L2 cache is picking up a book from a nearby shelf (14 seconds), and main system memory is taking a 4-minute walk down the hall to buy a Twix bar. Keeping with the office analogy, waiting for a hard drive seek is like leaving the building to roam the earth for &lt;b&gt;one year and three months&lt;/b&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You don’t need to keep a thread around while you’re waiting for an IO operation to complete. Windows will look after the IO operation for you, so long as you use the correct API. If you are writing these kinds of batch operations, you should always favour asynchronous IO over spawning threads. Most (but not all unfortunately) IO operations in the Base Class Library (BCL) have asynchronous versions based on the &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163467.aspx"&gt;Asynchronous Programming Model&lt;/a&gt; (APM). So, for example:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; MyIoOperation(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; arg)&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Would have an equivalent pair of APM methods:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;IAsyncResult BeginMyIoOperation(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; arg, AsyncCallback callback, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state);&lt;br /&gt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; EndMyIoOperation(IAsyncResult);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;You typically ignore the return value from BeginXXX and call the EndXXX inside a delegate you provide for the AsyncCallback:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;BeginMyIoOperation(&lt;span style="color: #006080"&gt;&amp;quot;Hello World&amp;quot;&lt;/span&gt;, asyncResult =&amp;gt; &lt;br /&gt;{&lt;br /&gt;    var result = EndMyIoOperation(asyncResult);&lt;br /&gt;}, &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Your main thread doesn’t block when you call BeginMyIoOperation, so you can run hundreds of them in short order. Eventually your IO operations will complete and the callback you defined will be run on a worker thread in the CLR’s thread pool. Profiling your application will show that only a handful of threads are used while your hundreds of IO operations happily run in parallel. Much nicer!&lt;/p&gt;

&lt;p&gt;Of course all this will become much easier with the async features of C# 5, but that’s no excuse not to do the right thing today with the APM.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-1786671109577390961?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/GIrbB28VU28/first-rule-of-threading-you-dont-need.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>5</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/07/first-rule-of-threading-you-dont-need.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-8244217374075065648</guid><pubDate>Wed, 29 Jun 2011 08:27:00 +0000</pubDate><atom:updated>2011-06-29T09:27:55.046+01:00</atom:updated><title>I Don’t Have Time for Unit Tests</title><description>&lt;p&gt;I’ve helped several organisations adopt &lt;a href="http://en.wikipedia.org/wiki/Test-driven_development"&gt;Test Driven Development&lt;/a&gt; (TDD). The initial worry that almost everyone has, is that it will hurt productivity. It seems intuitively correct, because of course, the developer has to write the unit tests as well as the production code. However when you actually look at how developers spend their time, actually writing code is a small part of it. Check out this study by Peter Hallam from the Visual Studio team at Microsoft:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://blogs.msdn.com/b/peterhal/archive/2006/01/04/509302.aspx"&gt;http://blogs.msdn.com/b/peterhal/archive/2006/01/04/509302.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;According to Peter, developers actually spend their days like this (while not reading Code Rant of course :):&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Writing new code 5%&lt;/li&gt;    &lt;li&gt;Modifying existing code 25%&lt;/li&gt;    &lt;li&gt;Understanding Code 70%&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;If some technique allowed you to half the modifying/understanding parts of the job by doubling the new-code part, you’d now be taking only ~60% of the time you previously took to deliver a feature, almost doubling your productivity.&lt;/p&gt;  &lt;p&gt;TDD is that technique. In my experience the productivity gains come from:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;Allows safe modifications. If you break something when you modify some code, the unit tests fail.&lt;/li&gt;    &lt;li&gt;Shortening the iteration cycle between writing/running code. No more need to step through your application to part where your new code gets exercised.&lt;/li&gt;    &lt;li&gt;Mistakes in your code are shallow and obvious. No more need to step through code in the debugger, wondering which part of your application is broken.&lt;/li&gt;    &lt;li&gt;Code is self-documenting. The unit tests explicitly show how the author expected the code to be used.&lt;/li&gt;    &lt;li&gt;Code is decoupled. You can’t do TDD without decoupling. This alone makes the code easier to understand as a unit and much safer to modify.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Note that I’m just talking about feature-delivery productivity here. I haven’t mentioned the huge gains in stability and drop in bug-counts that you also get.&lt;/p&gt;  &lt;p&gt;Now I don’t deny that TDD is a fundamentally different way of working that takes time to learn. Undoubtedly productivity may drop while a developer is getting up to speed. But in my experience the argument that a developer doing TDD is slower than a developer who doesn’t do it is simply not true.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-8244217374075065648?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/82GGpjLtWGQ/i-dont-have-time-for-unit-tests.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>8</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/06/i-dont-have-time-for-unit-tests.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-8691446393990675089</guid><pubDate>Mon, 27 Jun 2011 14:16:00 +0000</pubDate><atom:updated>2011-06-27T15:16:21.977+01:00</atom:updated><title>RabbitMQ, Subscription, and Bouncing Servers in EasyNetQ</title><description>&lt;p&gt;If you are a regular reader of my blog, you’ll know that I’m currently working on a .NET friendly API for RabbitMQ, &lt;a href="http://mikehadlow.blogspot.com/2011/05/easynetq-simple-net-api-for-rabbitmq.html"&gt;EasyNetQ&lt;/a&gt;. EasyNetQ is opinionated software. It takes away much of the complexity of AMQP and replaces it with a simple interface that relies on the .NET type system for routing messages.&lt;/p&gt;  &lt;p&gt;One of the things that I want to remove from the ‘application space’ and push down into the API is all the plumbing for reporting and handling error conditions. One side of this to provide infrastructure to record and handle exceptions thrown by applications that use EasyNetQ. I’ll be covering this in a future post. The other consideration, and the one I want to address in this post, is how EasyNetQ should gracefully handle network connection or server failure.&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://en.wikipedia.org/wiki/Fallacies_of_Distributed_Computing"&gt;Fallacies of Distributed Computing&lt;/a&gt; tell us that, no matter how reliable RabbitMQ and the Erlang platform might be, there will still be times when a RabbitMQ server will go away for whatever reason.&lt;/p&gt;  &lt;p&gt;One of the challenges of programming against a messaging system as compared with a relational database, is the length of time that the application holds connections open. A typical database connection is opened, some operation is run over it – select, insert, update, etc – and then it’s closed. Messaging system subscriptions, however, require that the client, or subscriber, holds an open connection for the lifetime of the application.&lt;/p&gt;  &lt;p&gt;If you simply program against the low level C# AMQP API provided by RabbitHQ to create a simple subscription, you’ll notice that after a RabbitMQ server bounce, the subscription no longer works. This is because the channel you opened to subscribe to the queue, and the consumption loops attached to them, are no longer valid. You need to detect the closed channel and then attempt to rebuild the subscription once the server is available again.&lt;/p&gt;  &lt;p&gt;The excellent &lt;a href="http://www.manning.com/videla/"&gt;RabbitMQ in Action&lt;/a&gt; by Videla and Williams describes how to do this in chapter 6, ‘Writing code that survives failure’. Here’s their Python code example:&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="rabbit_mq_in_action_failure_detecting_subscriber" border="0" alt="rabbit_mq_in_action_failure_detecting_subscriber" src="http://lh6.ggpht.com/-VqON4Q8wQgw/TgiQtLWNfYI/AAAAAAAABTU/VOgLS3ihjHk/rabbit_mq_in_action_failure_detecting_subscriber%25255B5%25255D.png?imgmax=800" width="731" height="582" /&gt;&lt;/p&gt;  &lt;p&gt;EasyNetQ needs to do something similar, but as a generic solution so that all subscribers automatically get re-subscribed after a server bounce. &lt;/p&gt;  &lt;p&gt;Here’s how it works.&lt;/p&gt;  &lt;p&gt;Firstly, all subscriptions are created in a closure:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Subscribe&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; subscriptionId, Action&amp;lt;T&amp;gt; onMessage)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (onMessage == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;throw&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; ArgumentNullException(&lt;span style="color: #006080"&gt;&amp;quot;onMessage&amp;quot;&lt;/span&gt;);&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    var typeName = serializeType(&lt;span style="color: #0000ff"&gt;typeof&lt;/span&gt;(T));&lt;br /&gt;    var subscriptionQueue = &lt;span style="color: #0000ff"&gt;string&lt;/span&gt;.Format(&lt;span style="color: #006080"&gt;&amp;quot;{0}_{1}&amp;quot;&lt;/span&gt;, subscriptionId, typeName);&lt;br /&gt;&lt;br /&gt;    Action subscribeAction = () =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        var channel = connection.CreateModel();&lt;br /&gt;        DeclarePublishExchange(channel, typeName);&lt;br /&gt;&lt;br /&gt;        var queue = channel.QueueDeclare(&lt;br /&gt;            subscriptionQueue,  &lt;span style="color: #008000"&gt;// queue&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;,               &lt;span style="color: #008000"&gt;// durable&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;,              &lt;span style="color: #008000"&gt;// exclusive&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;,              &lt;span style="color: #008000"&gt;// autoDelete&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;);              &lt;span style="color: #008000"&gt;// arguments&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;        channel.QueueBind(queue, typeName, typeName);  &lt;br /&gt;&lt;br /&gt;        var consumer = consumerFactory.CreateConsumer(channel, &lt;br /&gt;            (consumerTag, deliveryTag, redelivered, exchange, routingKey, properties, body) =&amp;gt;&lt;br /&gt;            {&lt;br /&gt;                var message = serializer.BytesToMessage&amp;lt;T&amp;gt;(body);&lt;br /&gt;                onMessage(message);&lt;br /&gt;            });&lt;br /&gt;&lt;br /&gt;        channel.BasicConsume(&lt;br /&gt;            subscriptionQueue,      &lt;span style="color: #008000"&gt;// queue&lt;/span&gt;&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;,                   &lt;span style="color: #008000"&gt;// noAck &lt;/span&gt;&lt;br /&gt;            consumer.ConsumerTag,   &lt;span style="color: #008000"&gt;// consumerTag&lt;/span&gt;&lt;br /&gt;            consumer);              &lt;span style="color: #008000"&gt;// consumer&lt;/span&gt;&lt;br /&gt;    };&lt;br /&gt;&lt;br /&gt;    connection.AddSubscriptionAction(subscribeAction);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The connection.AddSubscriptionAction(subscribeAction) line passes the closure to a PersistentConnection class that wraps an AMQP connection and provides all the disconnect detection and re-subscription code. Here’s AddSubscriptionAction:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; AddSubscriptionAction(Action subscriptionAction)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (IsConnected) subscriptionAction();&lt;br /&gt;    subscribeActions.Add(subscriptionAction);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;If there’s an open connection, it runs the subscription straight away. It also stores the subscription closure in a List&amp;lt;Action&amp;gt;.&lt;/p&gt;

&lt;p&gt;When the connection gets closed for whatever reason, the AMQP ConnectionShutdown event fires which runs the OnConnectionShutdown method:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; OnConnectionShutdown(IConnection _, ShutdownEventArgs reason)&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (disposed) &lt;span style="color: #0000ff"&gt;return&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Disconnected != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) Disconnected();&lt;br /&gt;&lt;br /&gt;    Thread.Sleep(100);&lt;br /&gt;    TryToConnect();&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;We wait for a little while, and then try to reconnect:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;void&lt;/span&gt; TryToConnect()&lt;br /&gt;{&lt;br /&gt;    ThreadPool.QueueUserWorkItem(state =&amp;gt;&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;while&lt;/span&gt; (connection == &lt;span style="color: #0000ff"&gt;null&lt;/span&gt; || !connection.IsOpen)&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;try&lt;/span&gt;&lt;br /&gt;            {&lt;br /&gt;                connection = connectionFactory.CreateConnection();&lt;br /&gt;                connection.ConnectionShutdown += OnConnectionShutdown;&lt;br /&gt;&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;if&lt;/span&gt; (Connected != &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;) Connected();&lt;br /&gt;            }&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;catch&lt;/span&gt; (RabbitMQ.Client.Exceptions.BrokerUnreachableException)&lt;br /&gt;            {&lt;br /&gt;                Thread.Sleep(100);&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;foreach&lt;/span&gt; (var subscribeAction &lt;span style="color: #0000ff"&gt;in&lt;/span&gt; subscribeActions)&lt;br /&gt;        {&lt;br /&gt;            subscribeAction();&lt;br /&gt;        }&lt;br /&gt;    });&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This spins up a thread that simply loops trying to connect back to the server. Once the connection is established, it runs all the stored subscribe closures (subscribeActions).&lt;/p&gt;

&lt;p&gt;In my tests, this solution has worked very nicely. My clients automatically re-subscribe to the same queues and continue to receive messages. One of the main motivations to writing this post, however, was to try and elicit feedback, so if you’ve used RabbitMQ with .NET, I’d love to hear about your experiences and especially any comments about my code or how you solved this problem.&lt;/p&gt;

&lt;p&gt;The &lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;EasyNetQ code is up on GitHub&lt;/a&gt;. It’s still very early days and is in no way production ready. You have been warned.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-8691446393990675089?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/t4TiS9P91KI/rabbitmq-subscription-and-bouncing.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh6.ggpht.com/-VqON4Q8wQgw/TgiQtLWNfYI/AAAAAAAABTU/VOgLS3ihjHk/s72-c/rabbit_mq_in_action_failure_detecting_subscriber%25255B5%25255D.png?imgmax=800" height="72" width="72" /><thr:total>3</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/06/rabbitmq-subscription-and-bouncing.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-8082048035636606198</guid><pubDate>Thu, 02 Jun 2011 11:05:00 +0000</pubDate><atom:updated>2011-06-02T13:17:31.212+01:00</atom:updated><title>Some Thoughts on Windows 8</title><description>&lt;p&gt;Microsoft is the rabbit caught in Apple’s headlights… and about to be run over by the Google juggernaut. Microsoft’s income comes from two major sources, Windows and Office. The need to maintain the stream of licence fees for these two products is at the very core of everything Microsoft does. Windows has three major groups of customers: consumer PCs, business PCs and business servers. Microsoft is the incumbent in all three markets, it can’t grow any more by taking market share, its income can only increase at the rate of growth for these markets as a whole. The only way Microsoft can break out of this static lock is by creating new markets for its products. But it must be careful not to injure it’s existing Windows franchises in attempts to get market share elsewhere.&lt;/p&gt;  &lt;p&gt;But it’s a tough world to be selling operating system licences. Unfortunately for Microsoft, the situation is far from static. It’s core source of income is under threat. The iPad has created a new class of consumer product that is eroding Microsoft’s market for consumer PCs. Many people have no need for the full power of a desktop PC. For browsing, reading and writing email and watching YouTube, an iPad, or one of the many Android competitors is perfectly adequate. Android in particular is becoming more and more able to do PC like tasks with every release. For many people a PC is overcomplicated and unreliable.&lt;/p&gt;  &lt;p&gt;Windows is also under threat in business server rooms. The main challenge here is from cloud based services. Why employ expensive people to look after servers running Exchange when you can simply sign up for Google Apps? For many small and medium businesses, cloud based services are a very attractive alternative to running in-house IT.&lt;/p&gt;  &lt;p&gt;Windows is probably safest of all on the business desktop. There’s no real alternative for running productivity applications like Office. However, with many line-of-business applications becoming cloud based, there is a risk that a Chrome OS style browser-only desktop might look attractive to some business. Also when everyone’s got an iPad or an Android tablet at home, having a similar device at work will start to make more sense.&lt;/p&gt;  &lt;p&gt;So is &lt;a href="http://www.youtube.com/watch?v=p92QfWOw88I"&gt;Windows 8&lt;/a&gt; an answer to any of these challenges? It’s obviously designed to answer the first, the erosion of the consumer PC market by iPad and friends. Fundamentally it looks like a simple UI layer, derived from WP7, stuck on top of Windows 7. Microsoft’s strategy seems to be to offer a simple touch-UI for ‘consumer’ tasks, but which allows you to switch back to Windows 7 for running desktop applications like Office. John Gruber &lt;a href="http://daringfireball.net/2011/06/windows_8_fundamentally_flawed"&gt;makes the point here&lt;/a&gt;, that simply skinning Windows 7 for tablets is probably a mistake.&lt;/p&gt;  &lt;p&gt;Microsoft is not the leader in the tablet market, it’s playing catch-up from quite a long way behind. Will consumers be willing to pay the Windows tax when they can simply buy a more mature iPad or Android device? Microsoft can’t start offering it for free like Google does with Android because they would immediately kill one of their main sources of income. There is no way they can do anything other than ask people to pay more, for what will, at least initially, be an inferior device. It doesn’t strike me as a winning strategy.&lt;/p&gt;  &lt;p&gt;The &lt;a href="http://www.youtube.com/watch?v=p92QfWOw88I"&gt;Windows 8&lt;/a&gt; developer story is somewhat odd to say the least. No mention of WPF or Silverlight. Instead developers are being asked to build apps for the Windows 8 touch UI in Javascript and HTML. There are more Javascript developers out there than Silverlight ones, but the people who already care about Microsoft platforms have put considerable investment into WPF and Silverlight. The Windows 8 announcement is a real slap-down for them. &lt;a href="http://channel9.msdn.com/posts/A-quick-look-at-Windows-8"&gt;Just take a look at the anger on Channel 9&lt;/a&gt;. It’s also a snub to Microsoft’s developer division who have put considerable effort into building .NET. Is this a hangover from the Vista debacle? Was WinFX such a failure that the Windows team now want nothing to do with .NET? If so, what’s the point of the developer division?&lt;/p&gt;  &lt;p&gt;It’s a confusing time for us developers. I think Microsoft still have a very strong position on the business desktop. If you are building line-of-business applications for a living with .NET, there’s probably still some millage in that. But the feeling is very much that .NET is now middle age, like many of its developers. No matter how nice the technology is, and it is very nice, it’s part of a platform that’s perceived to be part of the past, not the future.&lt;/p&gt;  &lt;p&gt;But if you are building for the consumer market, or the cloud, then Microsoft is merely a niche player. It’s been obvious for some time that everyone should have Javascript as a core skill. The Windows 8 announcement reinforces that. It’s also clear that UNIX/Linux operating systems (including iOS and Android) are now ubiquitous in the same way that TCP/IP is. You’d probably want to make sure you know how to find your way around a UNIX system. For building server side applications for the cloud the field is wide open. Ruby, Python &amp;amp; Javascript are all well established and for some exotic applications, things like Erlang, Scala and Haskell are very interesting. Personally I’m hoping, optimistically I think, that Mono and &lt;a href="http://www.xamarin.com/"&gt;Xamarin&lt;/a&gt; are a success. The excellent .NET platform deserves more than to be chained to Windows’ declining market share.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-8082048035636606198?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/_TYoDy6ToIQ/some-thoughts-on-windows-8.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>10</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/06/some-thoughts-on-windows-8.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-5163393792254190504</guid><pubDate>Mon, 30 May 2011 21:33:00 +0000</pubDate><atom:updated>2011-05-30T22:33:10.762+01:00</atom:updated><title>Dependency Injection Haskell Style</title><description>&lt;p&gt;Today I was thinking about dependency injection and Haskell. If we think about how an IoC container in a language like C# works, there are several pieces:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;Services are described by types (usually interfaces).&lt;/li&gt;    &lt;li&gt;Components (classes) describe their dependencies with the types of their constructor arguments.&lt;/li&gt;    &lt;li&gt;The components in turn describe the services they provide by implementing service interfaces.&lt;/li&gt;    &lt;li&gt;Service types are registered against implementation types using some API provided by the IoC container.&lt;/li&gt;    &lt;li&gt;A clever piece of framework (the IoC container), that when asked for a service (described by an interface), creates the entire dependency chain and then passes it the caller.&lt;/li&gt; &lt;/ol&gt;  &lt;p&gt;The important point is that we don’t have to manually wire up the dependency chain. We simply build our software in terms of service contracts and implementations, register them with the IoC container which then magically does the wiring up for us.&lt;/p&gt;  &lt;p&gt;If you dig into the source code for any of the .NET IoC containers (such as Windsor, StructureMap, Autofac, Ninject etc) you’ll see they do an awful lot of reflection to work out the dependency graph. I remember reading (or hearing someone say) that reflection is often used to make up for inadequacies in C#’s type system, so I started experimenting to see if Haskell could provide the decoupling of registration and dependency graph building without the need for an IoC container. And indeed it can.&lt;/p&gt;  &lt;p&gt;First let’s define some services:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;-- this could talk to a database&lt;br /&gt;type GetReport = Int -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; IO Report&lt;br /&gt;-- this could talk to an email API&lt;br /&gt;type SendReport = Report -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; IO ()&lt;br /&gt;-- this takes a report id and does something with it&lt;br /&gt;type ProcessReport = Int -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; IO ()&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Now let’s define some implementations:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;-- getReport simply creates a new report with the given id&lt;br /&gt;getReport :: GetReport &lt;br /&gt;getReport id =&lt;br /&gt;    return $ Report id &amp;quot;Hello&amp;quot;&lt;br /&gt;&lt;br /&gt;-- sendReport simply prints the report&lt;br /&gt;sendReport :: SendReport &lt;br /&gt;sendReport report = putStr $ show report&lt;br /&gt;&lt;br /&gt;-- processReport uses a GetReport and a SendReport to process a report&lt;br /&gt;processReport :: GetReport -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; SendReport -&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; ProcessReport &lt;br /&gt;processReport get send id = do&lt;br /&gt;    r &lt;span style="color: #0000ff"&gt;&amp;lt;&lt;/span&gt;- get id&lt;br /&gt;    send r&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Partial function application is equivalent to dependency injection in OO. Here our processReport’s dependencies are given as the first two arguments of the processReport function.&lt;/p&gt;

&lt;p&gt;Now let’s define a type class with a ‘resolve’ member. The resolve member isn’t a function as such, it’s just a label for whatever ‘a’ happens to be when we define instances of the type class:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;class Resolvable a where&lt;br /&gt;    resolve :: a&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Now let’s make each of our services an instance of ‘Resolvable’, and ‘register’ the implementation for each service:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;instance Resolvable GetReport where&lt;br /&gt;    resolve = getReport&lt;br /&gt;&lt;br /&gt;instance Resolvable SendReport where&lt;br /&gt;    resolve = sendReport&lt;br /&gt;&lt;br /&gt;instance Resolvable ProcessReport where&lt;br /&gt;    resolve = processReport resolve resolve&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Note that we partially apply processReport with two resolve calls that will provide implementations of the service types.&lt;/p&gt;

&lt;p&gt;The whole caboodle compiles and we can use resolve to grab a ProcessReport implementation with its dependencies provided:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; let p = resolve :: ProcessReport&lt;br /&gt;&lt;span style="color: #0000ff"&gt;&amp;gt;&lt;/span&gt; p 23&lt;br /&gt;Report 23 &amp;quot;Hello&amp;quot;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;All the functionality of an IoC container without an IoC container. Wonderful.&lt;/p&gt;

&lt;p&gt;So, to sum up, we’ve simply registered implementations against services and let the Haskell type system build the dependency graph for us. The added benefit we get here over reflection based IoC containers in C# and Java, is that this is all typed checked at compile time. No need to run it to find missing dependencies.&lt;/p&gt;

&lt;p&gt;Please bear in mind that I’m a total Haskell novice and this is probably something that real Haskell programmers would never do. But it’s an interesting illustration of the power of the Haskell type system, and how a lot of what we do with reflection in C# is simply to augment the limitations of the language.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-5163393792254190504?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/xaoDQVL2mnk/dependency-injection-haskell-style.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>3</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/05/dependency-injection-haskell-style.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-4165792828822421041</guid><pubDate>Thu, 19 May 2011 13:26:00 +0000</pubDate><atom:updated>2011-05-19T14:26:38.665+01:00</atom:updated><title>FuturePublish with the EasyNetQ RabbitMQ API</title><description>&lt;p&gt;If you’re a regular reader, you’ll remember that &lt;a href="http://mikehadlow.blogspot.com/2011/05/easynetq-simple-net-api-for-rabbitmq.html"&gt;I published an initial version EasyNetQ&lt;/a&gt;, my .NET friendly API for RabbitMQ, earlier this month. Today I want to show off a little addition that allows messages to be scheduled for publishing at a future date.&lt;/p&gt;  &lt;p&gt;Many business scenarios require some kind of scheduling. For example, say I want to send a party invitation, but I know that it will be forgotten if I send it too early. Instead I want it to arrive two days before the party. I’d like to ‘future publish’ my invite at the time I’m planning my party, and let the messaging system worry about sending it two days before hand.&lt;/p&gt;  &lt;p&gt;I’ve added a FuturePublish method to the EasyNetQ, you simply give it a messasge and the time that you want it sent.&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var invitation = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; PartyInvitation&lt;br /&gt;{&lt;br /&gt;    Text = &lt;span style="color: #006080"&gt;&amp;quot;Please come to my party&amp;quot;&lt;/span&gt;,&lt;br /&gt;    Date = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; DateTime(2011, 5, 24)&lt;br /&gt;};&lt;br /&gt;&lt;br /&gt;bus.FuturePublish(invitation.Date.AddDays(-2), invitation);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;That’s cool, how does it work?&lt;/p&gt;

&lt;p&gt;Internally the FuturePublish method wraps the given message in a special ‘Schedule Me’ message. This message is then published to Rabbit as normal. A windows service, EasyNetQ.Scheduler, subscribes to the Schedule Me messages which it writes to its database. At a pre-defined interval, it polls its database looking for messages who’s publish date is the current time, retrieves the wrapped message and publishes it.&lt;/p&gt;

&lt;p&gt;Check out the source on GitHub here:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;https://github.com/mikehadlow/EasyNetQ&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-4165792828822421041?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/eTzg5lU_Lu4/futurepublish-with-easynetq-rabbitmq.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>4</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/05/futurepublish-with-easynetq-rabbitmq.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2678662913537545913</guid><pubDate>Wed, 04 May 2011 16:06:00 +0000</pubDate><atom:updated>2011-05-04T17:06:51.644+01:00</atom:updated><title>EasyNetQ, a simple .NET API for RabbitMQ</title><description>&lt;p&gt;After pondering the results of our &lt;a href="http://mikehadlow.blogspot.com/2011/04/message-queue-shootout.html"&gt;message queue shootout&lt;/a&gt;, we decided to run with &lt;a href="http://www.rabbitmq.com/"&gt;Rabbit MQ&lt;/a&gt;. Rabbit ticks all of the boxes, it’s supported (by Spring Source and then VMware ultimately), scales and has the features and performance we need. The &lt;a href="http://www.rabbitmq.com/releases/rabbitmq-dotnet-client/v2.4.1/rabbitmq-dotnet-client-2.4.1-user-guide.pdf"&gt;RabbitMQ.Client&lt;/a&gt; provided by Spring Source is a thin wrapper that quite faithfully exposes the &lt;a href="http://en.wikipedia.org/wiki/Advanced_Message_Queuing_Protocol"&gt;AMQP&lt;/a&gt; protocol, so it expects messages as byte arrays. &lt;/p&gt;  &lt;p&gt;For the shootout tests spraying byte arrays around was fine, but in the real world, we want our messages to be .NET types. I also wanted to provide developers with a very simple API that abstracted away the Exchange/Binding/Queue model of AMQP and instead provides a simple publish/subscribe and request/response model. My inspiration was the excellent work done by &lt;a href="http://codebetter.com/drusellers/author/drusellers/"&gt;Dru Sellers&lt;/a&gt; and &lt;a href="http://blog.phatboyg.com/"&gt;Chris Patterson&lt;/a&gt; with &lt;a href="http://docs.masstransit-project.com/en/latest/configuration/quickstart.html"&gt;MassTransit&lt;/a&gt; (the new V2.0 beta is just out).&lt;/p&gt;  &lt;p&gt;The code is on GitHub here:&lt;/p&gt;  &lt;p&gt;&lt;a href="https://github.com/mikehadlow/EasyNetQ"&gt;https://github.com/mikehadlow/EasyNetQ&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;The API centres around an IBus interface that looks like this:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;/// Provides a simple Publish/Subscribe and Request/Response API for a message bus.&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; IBus : IDisposable&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// Publishes a message.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;The message type&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;message&amp;quot;&amp;gt;The message to publish&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Publish&amp;lt;T&amp;gt;(T message);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// Subscribes to a stream of messages that match a .NET type.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;T&amp;quot;&amp;gt;The type to subscribe to&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;subscriptionId&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// A unique identifier for the subscription. Two subscriptions with the same subscriptionId&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// and type will get messages delivered in turn. This is useful if you want multiple subscribers&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// to load balance a subscription in a round-robin fashion.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;onMessage&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// The action to run when a message arrives.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Subscribe&amp;lt;T&amp;gt;(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; subscriptionId, Action&amp;lt;T&amp;gt; onMessage);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// Makes an RPC style asynchronous request.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;TRequest&amp;quot;&amp;gt;The request type.&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;TResponse&amp;quot;&amp;gt;The response type.&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;request&amp;quot;&amp;gt;The request message.&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;onResponse&amp;quot;&amp;gt;The action to run when the response is received.&amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Request&amp;lt;TRequest, TResponse&amp;gt;(TRequest request, Action&amp;lt;TResponse&amp;gt; onResponse);&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// Responds to an RPC request.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/summary&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;TRequest&amp;quot;&amp;gt;The request type.&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;typeparam name=&amp;quot;TResponse&amp;quot;&amp;gt;The response type.&amp;lt;/typeparam&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;param name=&amp;quot;responder&amp;quot;&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// A function to run when the request is received. It should return the response.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;/// &amp;lt;/param&amp;gt;&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; Respond&amp;lt;TRequest, TResponse&amp;gt;(Func&amp;lt;TRequest, TResponse&amp;gt; responder);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;To create a bus, just use a RabbitHutch, sorry I couldn’t resist it :)&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var bus = RabbitHutch.CreateRabbitBus(&lt;span style="color: #006080"&gt;&amp;quot;localhost&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;You can just pass in the name of the server to use the default Rabbit virtual host ‘/’, or you can specify a named virtual host like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var bus = RabbitHutch.CreateRabbitBus(&lt;span style="color: #006080"&gt;&amp;quot;localhost/myVirtualHost&amp;quot;&lt;/span&gt;);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;The first messaging pattern I wanted to support was publish/subscribe. Once you’ve got a bus instance, you can publish a message like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var message = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MyMessage {Text = &lt;span style="color: #006080"&gt;&amp;quot;Hello!&amp;quot;&lt;/span&gt;};&lt;br /&gt;bus.Publish(message);&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This publishes the message to an exchange named by the message type.&lt;/p&gt;

&lt;p&gt;You subscribe to a message like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;bus.Subscribe&amp;lt;MyMessage&amp;gt;(&lt;span style="color: #006080"&gt;&amp;quot;test&amp;quot;&lt;/span&gt;, message =&amp;gt; Console.WriteLine(message.Text));&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This creates a queue named ‘test_&amp;lt;message type&amp;gt;’ and binds it to the message type’s exchange. When a message is received it is passed to the Action&amp;lt;T&amp;gt; delegate.&amp;#160; If there are more than one subscribers to the same message type named ‘test’, Rabbit will hand out the messages in a round-robin fashion, so you get simple load balancing out of the box. Subscribers to the same message type, but with different names will each get a copy of the message, as you’d expect.&lt;/p&gt;

&lt;p&gt;The second messaging pattern is an asynchronous RPC. You can call a remote service like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;var request = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TestRequestMessage {Text = &lt;span style="color: #006080"&gt;&amp;quot;Hello from the client! &amp;quot;&lt;/span&gt;};&lt;br /&gt;&lt;br /&gt;bus.Request&amp;lt;TestRequestMessage, TestResponseMessage&amp;gt;(request, response =&amp;gt; &lt;br /&gt;    Console.WriteLine(&lt;span style="color: #006080"&gt;&amp;quot;Got response: '{0}'&amp;quot;&lt;/span&gt;, response.Text));&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This first creates a new temporary queue for the TestResponseMessage. It then publishes the TestRequestMessage with a return address to the temporary queue. When the TestResponseMessage is received, it passes it to the Action&amp;lt;T&amp;gt; delegate. RabbitMQ happily creates temporary queues and provides a return address header, so this was very easy to implement.&lt;/p&gt;

&lt;p&gt;To write an RPC server. Simple use the Respond method like this:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;bus.Respond&amp;lt;TestRequestMessage, TestResponseMessage&amp;gt;(request =&amp;gt; &lt;br /&gt;    &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; TestResponseMessage { Text = request.Text + &lt;span style="color: #006080"&gt;&amp;quot; all done!&amp;quot;&lt;/span&gt; });&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;This creates a subscription for the TestRequestMessage. When a message is received, the Func&amp;lt;TRequest, TResponse&amp;gt; delegate is passed the request and returns the response. The response message is then published to the temporary client queue.&lt;/p&gt;

&lt;p&gt;Once again, scaling RPC servers is simply a question of running up new instances. Rabbit will automatically distribute messages to them.&lt;/p&gt;

&lt;p&gt;The features of AMQP (and Rabbit) make creating this kind of API a breeze. Check it out and let me know what you think.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2678662913537545913?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/MFaVN-8Kq9A/easynetq-simple-net-api-for-rabbitmq.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>11</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/05/easynetq-simple-net-api-for-rabbitmq.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2561015806039826597</guid><pubDate>Wed, 13 Apr 2011 20:37:00 +0000</pubDate><atom:updated>2011-04-13T21:37:06.236+01:00</atom:updated><title>Serializing Continuations</title><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/b/ericlippert/archive/2010/10/21/continuation-passing-style-revisited-part-one.aspx"&gt;Continuations are cool&lt;/a&gt;. They allow you to write some code that uses the enclosing state, but which can be executed in some other context at some other time. What if we could serialize the continuation and put it away in a database, a file, or even a message on a service bus only to be grabbed later and then executed? Well we can.&lt;/p&gt;  &lt;p&gt;Take a look at this code:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;div id="codeSnippetWrapper"&gt;     &lt;div id="codeSnippetWrapper"&gt;       &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; filePath = &lt;span style="color: #006080"&gt;@&amp;quot;C:\temp\serialized.action&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; BuryIt()&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;string&lt;/span&gt; message = &lt;span style="color: #006080"&gt;&amp;quot;Hello!&amp;quot;&lt;/span&gt;;&lt;br /&gt;&lt;br /&gt;    var bytes = DoSomethingLater(name =&amp;gt; Console.WriteLine(message + &lt;span style="color: #006080"&gt;&amp;quot; &amp;quot;&lt;/span&gt; + name));&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;using&lt;/span&gt;(var stream = File.Create(filePath))&lt;br /&gt;    {&lt;br /&gt;        stream.Write(bytes, 0, bytes.Length);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ResurrectIt()&lt;br /&gt;{&lt;br /&gt;    var bytes = File.ReadAllBytes(filePath);&lt;br /&gt;    ExecuteSerializedAction(&lt;span style="color: #006080"&gt;&amp;quot;Mike&amp;quot;&lt;/span&gt;, bytes);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] DoSomethingLater(Action&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt; action)&lt;br /&gt;{&lt;br /&gt;    var formatter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BinaryFormatter();&lt;br /&gt;    var stream = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MemoryStream();&lt;br /&gt;&lt;br /&gt;    formatter.Serialize(stream, action);&lt;br /&gt;    stream.Position = 0;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; stream.GetBuffer();&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; &lt;span style="color: #0000ff"&gt;void&lt;/span&gt; ExecuteSerializedAction(&lt;span style="color: #0000ff"&gt;string&lt;/span&gt; name, &lt;span style="color: #0000ff"&gt;byte&lt;/span&gt;[] serializedAction)&lt;br /&gt;{&lt;br /&gt;    var formatter = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; BinaryFormatter();&lt;br /&gt;    var stream = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; MemoryStream(serializedAction);&lt;br /&gt;    var action = (Action&amp;lt;&lt;span style="color: #0000ff"&gt;string&lt;/span&gt;&amp;gt;)formatter.Deserialize(stream);&lt;br /&gt;&lt;br /&gt;    action(name);&lt;br /&gt;}&lt;/pre&gt;

      &lt;br /&gt;&lt;/div&gt;
In ‘BuryIt’ we call DoSomethingLater passing to it a continuation that includes some of TryIt’s state, namely the message string “Hello!”. DoSomethingLater takes takes the ‘action’ delegate, serializes it and returns the serialized bytes to its caller. BuryIt then writes the bytes to a file.&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;Next we execute ResurrectIt. It opens the file, reads the bytes and&amp;#160; hands them to ExecuteSerializedAction. This de-serializes the byte array back to an Action&amp;lt;string&amp;gt; and then executes it with the name parameter. When you execute it, it prints out:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;Hello! Mike&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;
This is very cool. Say we had some long running process where we wanted to execute each continuation at an interval&amp;#160; and have the process go away in between each execution. We could use this technique to store the state at each stage in a database and have each instance carry on where the previous one left off.




  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2561015806039826597?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/KtdtpcAtPCA/serializing-continuations.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>18</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/04/serializing-continuations.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2595112625861190688</guid><pubDate>Sun, 10 Apr 2011 21:21:00 +0000</pubDate><atom:updated>2011-04-10T22:27:49.235+01:00</atom:updated><title>Message Queue Shootout!</title><description>&lt;p&gt;I’ve spent an interesting week evaluating various Message Queue products. The motivation behind this is a client that has somewhat high performance requirements. They have bursts of over a million simultaneous messages. Currently they’re using a SQL server based solution, but it’s not ideal, and I’m suggesting they look at Message Queuing products as an alternative.&lt;/p&gt;  &lt;p&gt;In order to get a completely unscientific feel for the performance of some likely contenders, I put together a little test. Each queue would be asked to send one million 1K messages and receive them again. The test was done in somewhat of a hurry, so I haven’t tweaked any settings, just installed each MQ and done the simplest send and receive I could manage after a quick glance at the docs. So this is true out-of-the-box performance. I fully accept that this is going to penalise MQ products that have conservative default configurations.&lt;/p&gt;  &lt;p&gt;The candidates are:&lt;/p&gt;  &lt;ul&gt;   &lt;li&gt;MSMQ. The default choice for where only the products of Redmond are considered worthy. For my clients, if MSMQ can rise to the challenge, then they should use it. The main points against it are its lack of sophistication; nothing but send and receive; and it’s arbitrary hard limits, such as the 4MB maximum message size. However, with something like MassTransit or NServiceBus layered on top, it’s entirely possible to do serious work with it.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://activemq.apache.org/"&gt;ActiveMQ&lt;/a&gt;. The stalwart of the Java world. It has long service and ubiquity going for it. It’s also cross platform and would provide a natural integration point for non-Microsoft platform products. However, it would have to perform better than MSMQ to have a look-in.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.rabbitmq.com/"&gt;RabbitMQ&lt;/a&gt;. I’ve been hearing excellent things about this message broker written in Erlang. It supports the open AMQP (Advanced Message Queuing Protocol) with the potential to avoid vendor lock-in and benefiting from a wide range of clients in almost every language. AMQP provides some pretty sophisticated messaging patterns, so there’s less need for a MassTransit or NServiceBus. It also has ‘enterprise’ resilience and durability. That’s something my client is very interested in.&lt;/li&gt;    &lt;li&gt;&lt;a href="http://www.zeromq.org/"&gt;ZeroMQ&lt;/a&gt;. I only discovered this MQ product when I was researching AMQP. The company that created it were part of the AMQP group and had a product called OpenAMQ. However, they parted company with AMQP quite dramatically, complaining that it had lost its way and was becoming over complicated. You can read their Dear John letter &lt;a href="http://www.imatix.com/articles:whats-wrong-with-amqp"&gt;here&lt;/a&gt;. ZeroMQ has a unique broker-less model which means that unlike all the other products under test, you don’t need to install and run a message queuing server, or broker. Simply reference the ZeroMQ library, it’s on NuGet, and you can happily send messages between your applications. Interestingly, they are also placing it as a way of creating Erlang style actors in any language by leveraging ZeroMQ’s blazingly fast in-process messaging.&lt;/li&gt; &lt;/ul&gt;  &lt;p&gt;Getting all four MQ products up and running was fun. There’s a definite overhead when you have to install a product based on a non-Windows platform. ActiveMQ required Java on the target machine and RabbitMQ needed Erlang. Both installed without a hitch, but I’m concerned that it’s another layer that can go wrong in production. I would be asking the infrastructure people to understand and maintain unfamiliar runtimes if we chose either of these. ActiveMQ, RabbitMQ and MSMQ all have server processes that need to be monitored and configured, another support concern. &lt;/p&gt;  &lt;p&gt;ZeroMQ, with its brokerless architecture doesn’t require any server process or runtime. In effect, your application endpoints play the server role. This makes deployment simpler, but the worry is that there’s no obvious place to go looking when things go wrong. ZeroMQ, as far as I can tell, only provides non-durable queues. You are expected to provide your own auditing and recovery where you need it. To be honest, I’m not even sure that ZeroMQ should be in this test, it’s such a different concept than the other MQ products.&lt;/p&gt;  &lt;p&gt;So without further chit-chat, here are the results. This is messages per second, for send and receive. During the transmission of one million 1K messages. The tests were executed a machine running Windows Vista.&lt;/p&gt;  &lt;p&gt;&lt;img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="mqshootout" border="0" alt="mqshootout" src="http://lh4.ggpht.com/_159eVJubwBA/TaIfaYZkbLI/AAAAAAAABTQ/_7HNkLE5rhQ/mqshootout%5B5%5D.jpg?imgmax=800" width="621" height="457" /&gt;&lt;/p&gt;  &lt;p&gt;As you can see, there’s ZeroMQ and the others. Its performance is staggering. To be fair, ZeroMQ is quite a different beast from the others, but even so, the results are clear: if you want one application to send messages to another as quickly as possible, you need ZeroMQ. This is especially true if you don’t particularly mind loosing the occasional message.&lt;/p&gt;  &lt;p&gt;To be honest, I was hoping for more from Rabbit. However much one tries to be fair in these things, you inevitably have a favourite, and everything I’d read and heard about Rabbit made me think it was probably the best choice. But with these results, I’m going to be hard pressed to sell it over MSMQ.&lt;/p&gt;  &lt;p&gt;If you’d like to run the tests for yourself, my test code is on GitHub here. I’d be very (no very very) interested in how the tests can be tweaked, so if you can get substantially better figures, please let me know.&lt;/p&gt;  &lt;p&gt;&lt;a href="https://github.com/mikehadlow/Mike.MQShootout"&gt;https://github.com/mikehadlow/Mike.MQShootout&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2595112625861190688?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/XSzhBYgaaVQ/message-queue-shootout.html</link><author>noreply@blogger.com (Mike Hadlow)</author><media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://lh4.ggpht.com/_159eVJubwBA/TaIfaYZkbLI/AAAAAAAABTQ/_7HNkLE5rhQ/s72-c/mqshootout%5B5%5D.jpg?imgmax=800" height="72" width="72" /><thr:total>21</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/04/message-queue-shootout.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-7962831083779777546</guid><pubDate>Thu, 31 Mar 2011 19:49:00 +0000</pubDate><atom:updated>2011-03-31T20:49:42.804+01:00</atom:updated><title>Monads! European Virtual ALT.NET. Video is now available</title><description>&lt;p&gt;Thanks to the excellent &lt;a href="http://vanryswyckjan.blogspot.com/"&gt;Jan Van Ryswyck&lt;/a&gt;, the video of my &lt;a href="http://europevan.blogspot.com/"&gt;EVAN&lt;/a&gt; talk is now on Vimeo here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://vimeo.com/21705972"&gt;http://vimeo.com/21705972&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;It’s very similar to the DDD talk video that I posted before, but I think a little more polished. It’s much easier to follow in this format too.&lt;/p&gt;  &lt;p&gt;I start off with a brief introduction and history of Monads, but spend most of the talk deriving a Monad by attempting to do function composition with two Maybe returning functions. I briefly overview the differences between C#, F# and Haskell Monad handling at the end.&lt;/p&gt;  &lt;p&gt;Check out my series on Monads here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://mikehadlow.blogspot.com/2011/01/monads-in-c1-introduction.html"&gt;http://mikehadlow.blogspot.com/2011/01/monads-in-c1-introduction.html&lt;/a&gt;&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-7962831083779777546?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/vm-KTBNI0pA/monads-european-virtual-altnet-video-is.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>0</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/03/monads-european-virtual-altnet-video-is.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-226326566221259949</guid><pubDate>Sun, 27 Mar 2011 20:28:00 +0000</pubDate><atom:updated>2011-03-27T21:28:37.012+01:00</atom:updated><title>Monads! European Virtual ALT.NET Tuesday 29th March</title><description>&lt;p&gt;Jan Van Ryswyck has very kindly invited me to rant at length on the subject of Monads. I’m going to be giving an hour long presentation for the European Virtual ALT.NET this Tuesday. This will be a first for me, I haven’t attempted an on-line presentation before, so I’m very interested to see how it goes.&lt;/p&gt;  &lt;p&gt;The talk is going to be very similar to the Monad talk I gave at DDD9, but hopefully I’ve improved the flow and code examples, so it might make more sense. The full announcement (and details about how to join in) are here:&lt;/p&gt;  &lt;p&gt;&lt;a href="http://europevan.blogspot.com/2011/03/mike-hadlow-on-monads-on-29-march-2011.html"&gt;http://europevan.blogspot.com/2011/03/mike-hadlow-on-monads-on-29-march-2011.html&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;See you on Tuesday!&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-226326566221259949?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/ViAI576KD5M/monads-european-virtual-altnet-tuesday.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>0</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/03/monads-european-virtual-altnet-tuesday.html</feedburner:origLink></item><item><guid isPermaLink="false">tag:blogger.com,1999:blog-15136575.post-2774271582295527502</guid><pubDate>Wed, 16 Mar 2011 16:20:00 +0000</pubDate><atom:updated>2011-03-16T16:20:20.359Z</atom:updated><title>7000 Concurrent Connections With Asynchronous WCF</title><description>&lt;p&gt;It’s rare that a web service has some intensive processor bound computation to execute. Far more common for business applications, is a web service that executes one or more IO intensive operations. Typically our web service would access a database over the network, read and write files, or maybe call another web service. If we execute these operations synchronously, the thread that processes the web service request will spend most of its time waiting on IO. By executing IO operations asynchronously we can free the thread processing the request to process other requests while waiting for the IO operation to complete.&lt;/p&gt;  &lt;p&gt;In my experiments with a simple self-hosted WCF service, I’ve been able to demonstrate up to 7000 concurrent connections handled by just 12 threads.&lt;/p&gt;  &lt;p&gt;Before I show you how to write an asynchronous WCF service, I want to clear up the commonly held misconception (yes, by me too until a year or so ago), that asynchronous IO operations spawn threads. Many of the APIs in the .NET BCL (Base Class Library) provide asynchronous versions of their methods. So, for example, HttpWebRequest has a BeginGetResponse / EndGetResponse method pair alongside the synchronous method GetResponse. This pattern is called the &lt;a href="http://msdn.microsoft.com/en-us/magazine/cc163467.aspx"&gt;Asynchronous Programming Model&lt;/a&gt; (APM). When the APM supports IO operations, they are implemented using an operating system service called &lt;a href="http://telestarnotes.blogspot.com/2004/08/some-notes-on-iocp-thread-pooling-in-c_30.html"&gt;IO Completion Ports&lt;/a&gt; (IOCP). IOCP provides a queue where IO operations can be parked while the OS waits for them to complete, and provides a thread pool to handle the completed operations. This means that in-progress IO operations do not consume threads.&lt;/p&gt;  &lt;p&gt;The WCF infrastructure allows you to define your operation contracts using APM. Here’s a contract for a GetCustomer operation:&lt;/p&gt;  &lt;div id="codeSnippetWrapper"&gt;   &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[ServiceContract(SessionMode = SessionMode.NotAllowed)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;interface&lt;/span&gt; ICustomerService&lt;br /&gt;{&lt;br /&gt;    [OperationContract(AsyncPattern = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;)]&lt;br /&gt;    IAsyncResult BeginGetCustomerDetails(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; customerId, AsyncCallback callback, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state);&lt;br /&gt;    Customer EndGetCustomerDetails(IAsyncResult asyncResult);&lt;br /&gt;}&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;Essentially ‘GetCustomerDetails’ takes a customerId and returns a Customer. In order to create an asynchronous version of the contract I’ve simply followed the APM pattern and created a BeginGetCustomerDetails and an EndGetCustomerDetails. You tell WCF that you are implementing APM by setting AsyncPattern to true on the operation contract. &lt;/p&gt;

&lt;p&gt;The IAsyncResult that’s returned from the ‘begin’ method and passed as an argument to the ‘end’ method links the two together. Here’s a simple implementation of IAsyncResult that I’ve used for these experiments, you should be able to use it for any asynchronous WCF service:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; SimpleAsyncResult&amp;lt;T&amp;gt; : IAsyncResult&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;readonly&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; accessLock = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt;();&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; isCompleted = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; T result;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; SimpleAsyncResult(&lt;span style="color: #0000ff"&gt;object&lt;/span&gt; asyncState)&lt;br /&gt;    {&lt;br /&gt;        AsyncState = asyncState;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; T Result&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (accessLock)&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; result;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (accessLock)&lt;br /&gt;            {&lt;br /&gt;                result = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; IsCompleted&lt;br /&gt;    {&lt;br /&gt;        get&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (accessLock)&lt;br /&gt;            {&lt;br /&gt;                &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; isCompleted;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        set&lt;br /&gt;        {&lt;br /&gt;            &lt;span style="color: #0000ff"&gt;lock&lt;/span&gt; (accessLock)&lt;br /&gt;            {&lt;br /&gt;                isCompleted = &lt;span style="color: #0000ff"&gt;value&lt;/span&gt;;&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;// WCF seems to use the async callback rather than checking the wait handle&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;// so we can safely return null here.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; WaitHandle AsyncWaitHandle { get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;null&lt;/span&gt;; } }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;// We will always be doing an async operation so completed synchronously should always&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #008000"&gt;// be false.&lt;/span&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;bool&lt;/span&gt; CompletedSynchronously { get { &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;; } }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; AsyncState { get; &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; set; }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;



&lt;p&gt;Now we’ve got an AsyncResult we can implement our APM based service by implementing ICustomerService:&lt;/p&gt;

&lt;div id="codeSnippetWrapper"&gt;
  &lt;pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; border-right-style: none; background-color: #f4f4f4; margin: 0em; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &amp;#39;Courier New&amp;#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; font-size: 8pt; border-left-style: none; overflow: visible; padding-top: 0px" id="codeSnippet"&gt;[ServiceBehavior(&lt;br /&gt;    InstanceContextMode = InstanceContextMode.PerCall,&lt;br /&gt;    ConcurrencyMode = ConcurrencyMode.Multiple)]&lt;br /&gt;&lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;class&lt;/span&gt; CustomerService : ICustomerService&lt;br /&gt;{&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; &lt;span style="color: #0000ff"&gt;const&lt;/span&gt; &lt;span style="color: #0000ff"&gt;int&lt;/span&gt; DelayMilliseconds = 10000;&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; IAsyncResult BeginGetCustomerDetails(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; customerId, AsyncCallback callback, &lt;span style="color: #0000ff"&gt;object&lt;/span&gt; state)&lt;br /&gt;    {&lt;br /&gt;        var asyncResult = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; SimpleAsyncResult&amp;lt;Customer&amp;gt;(state);&lt;br /&gt;&lt;br /&gt;        &lt;span style="color: #008000"&gt;// mimic a long running operation&lt;/span&gt;&lt;br /&gt;        var timer = &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; System.Timers.Timer(DelayMilliseconds);&lt;br /&gt;        timer.Elapsed += (_, args) =&amp;gt;&lt;br /&gt;        {&lt;br /&gt;            asyncResult.Result = GetCustomer(customerId);&lt;br /&gt;            asyncResult.IsCompleted = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;            callback(asyncResult);&lt;br /&gt;            timer.Enabled = &lt;span style="color: #0000ff"&gt;false&lt;/span&gt;;&lt;br /&gt;            timer.Close();&lt;br /&gt;        };&lt;br /&gt;        timer.Enabled = &lt;span style="color: #0000ff"&gt;true&lt;/span&gt;;&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; asyncResult;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;public&lt;/span&gt; Customer EndGetCustomerDetails(IAsyncResult asyncResult)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; ((SimpleAsyncResult&amp;lt;Customer&amp;gt;) asyncResult).Result;&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;    &lt;span style="color: #0000ff"&gt;private&lt;/span&gt; &lt;span style="color: #0000ff"&gt;static&lt;/span&gt; Customer GetCustomer(&lt;span style="color: #0000ff"&gt;int&lt;/span&gt; customerId)&lt;br /&gt;    {&lt;br /&gt;        &lt;span style="color: #0000ff"&gt;return&lt;/span&gt; &lt;span style="color: #0000ff"&gt;new&lt;/span&gt; Customer(customerId, &lt;span style="color: #006080"&gt;&amp;quot;Mike_&amp;quot;&lt;/span&gt; + customerId);&lt;br /&gt;    }&lt;br /&gt;}&lt;br /&gt;&lt;/pre&gt;

  &lt;br /&gt;&lt;/div&gt;

&lt;p&gt;We’re mimicking a long running IO operation by using a timer. I believe that the Timer also uses an IO completion port, but don’t quote me on that. When WCF invokes BeginGetCustomerDetails we first create a new SimpleAsyncResult with WCF’s state object. WCF will pass the AsyncResult to the EndGetCustomerDetails method when the timer completes, so we can use it to pass any response state. In our case this is an instance of Customer.&lt;/p&gt;

&lt;p&gt;Next we set up a Timer and attach a closure to the Elapsed event. When the timer’s Elapsed event fires we create a customer instance, pass it to our AsyncResult and then pass the AsyncResult to WCF’s callback function.&lt;/p&gt;

&lt;p&gt;After the BeginGetCustomerDetails method completes, WCF returns its thread back to the WCF thread pool so that it can service other requests. Ten seconds later, the operating system posts an item on to the IOCP queue, a thread from the pool picks up the item and executes the continuation. This in turn calls WCF’s callback which in turn executes EndGetCustomerDetails. WCF then packages up the Customer as a SOAP response and returns it to the client. &lt;/p&gt;

&lt;p&gt;Only a tiny fraction of that 10 seconds has actually occupied a thread, so we should be able to make thousands of concurrent calls to this service.&lt;/p&gt;

&lt;p&gt;In my tests, this service has been able to handle a little over 7000 concurrent connections.&lt;/p&gt;

&lt;p&gt;The code is here: &lt;a href="https://github.com/mikehadlow/Mike.AsyncWcf"&gt;https://github.com/mikehadlow/Mike.AsyncWcf&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Just follow the README instructions to run the tests and try it out for yourself.&lt;/p&gt;  &lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/15136575-2774271582295527502?l=mikehadlow.blogspot.com' alt='' /&gt;&lt;/div&gt;</description><link>http://feedproxy.google.com/~r/CodeRant/~3/frsbA9dN9rw/7000-concurrent-connections-with.html</link><author>noreply@blogger.com (Mike Hadlow)</author><thr:total>11</thr:total><feedburner:origLink>http://mikehadlow.blogspot.com/2011/03/7000-concurrent-connections-with.html</feedburner:origLink></item></channel></rss>

