<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Neil Kidd's Blog</title><link>http://blogs.msdn.com/neilkidd/default.aspx</link><description>Scribblings from the Coding Floor</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>.NET Services workflow is moving to Fx 4‘s workflow engine, but …</title><link>http://blogs.msdn.com/neilkidd/archive/2009/06/17/net-services-workflow-is-moving-to-fx-4-s-workflow-engine-but.aspx</link><pubDate>Wed, 17 Jun 2009 09:55:58 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9767161</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9767161.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9767161</wfw:commentRss><description>&lt;p&gt;… it means that .Net Services workflow will not ship as part of Version 1 of the Azure Services Platform.&lt;/p&gt;  &lt;p&gt;&lt;img style="margin: 0px 10px 0px 0px; display: inline" title="Workflow icon" border="0" alt="Workflow icon" align="left" src="http://www.microsoft.com/azure/images/workflow_icon.gif" /&gt;For more details, and the reasons why, see:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/netservicesannounce/archive/2009/06/12/upcoming-important-changes-to-net-workflow-service.aspx" href="http://blogs.msdn.com/netservicesannounce/archive/2009/06/12/upcoming-important-changes-to-net-workflow-service.aspx"&gt;http://blogs.msdn.com/netservicesannounce/archive/2009/06/12/upcoming-important-changes-to-net-workflow-service.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Note that this change only affects people hosting their workflows in the cloud on the &lt;a href="http://www.microsoft.com/azure/default.mspx"&gt;Azure Service Platform&lt;/a&gt;; it doesn’t affect you if you are hosting the workflow engine in your own application.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9767161" width="1" height="1"&gt;</description></item><item><title>Changes to Azure and MIX day 1 highlights</title><link>http://blogs.msdn.com/neilkidd/archive/2009/03/18/changes-to-azure-and-mix-day-1-highlights.aspx</link><pubDate>Thu, 19 Mar 2009 00:33:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9488130</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9488130.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9488130</wfw:commentRss><description>&lt;p&gt;Ok, so I didn’t get to go to MIX, but we watched the keynote in the MTC boardroom this afternoon – it was almost like being there – no jet lag, no hangover, nice comfy leather seats, coffee on tap – ok, no point me trying to hide it I'm jealous of those that are in Vegas ;-)&lt;/p&gt;  &lt;p&gt;If you haven't seen the keynote, it is well worth a watch – a 2 hour session which is packed full of announcements and new product details from Blend to Windows Azure (sorry, that was a close as A-Z as I could get). I was already aware of most of the announcements, but it is great to see how all the pieces fit together. Sessions like really allow you to take stock of where the technology is today and where it is moving to. In some respects it is kind of scary; I already feel left behind in the design space and we seem to be accelerating our level of innovation there. And we keep making it easier and easier to complex things – hard won skills become commonplace with new releases versions.&lt;/p&gt;  &lt;p&gt;I’m sure that most Microsoft blogs will contain summaries of the session, so I’ll keep this brief.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Windows Azure&lt;/strong&gt;: PHP, FastCGI (ok, I’m unashamedly a .Net guy, but I like the idea that more devs will be able to enjoy Azure), Full Trust for .Net and Geo Location. For more details:&lt;/p&gt;  &lt;p&gt;&amp;#160; &lt;a title="http://blogs.msdn.com/windowsazure/archive/2009/03/18/windows-azure-delivers-new-ctp-capabilities.aspx" href="http://blogs.msdn.com/windowsazure/archive/2009/03/18/windows-azure-delivers-new-ctp-capabilities.aspx"&gt;http://blogs.msdn.com/windowsazure/archive/2009/03/18/windows-azure-delivers-new-ctp-capabilities.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Get details and the download link to the new SDK here:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/jnak/archive/2009/03/18/now-available-march-ctp-of-the-windows-azure-tools-and-sdk.aspx" href="http://blogs.msdn.com/jnak/archive/2009/03/18/now-available-march-ctp-of-the-windows-azure-tools-and-sdk.aspx"&gt;http://blogs.msdn.com/jnak/archive/2009/03/18/now-available-march-ctp-of-the-windows-azure-tools-and-sdk.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Blend 3&lt;/strong&gt;: I was beyond Super Excited and almost reached a state of hyper-jazzed by Jon Harris’ ScetchFlow demo. A major part of what we do in the MTC is around user experience and this is really going to change the way we work.&lt;/p&gt;  &lt;p&gt;&lt;a title="http://channel9.msdn.com/shows/Continuum/First-Look-at-Expression-Blend-3/" href="http://channel9.msdn.com/shows/Continuum/First-Look-at-Expression-Blend-3/"&gt;http://channel9.msdn.com/shows/Continuum/First-Look-at-Expression-Blend-3/&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Silverlight 3&lt;/strong&gt;: Silverlight just gets cooler and cooler. The new streaming support and the free availability IIS Media Services (streaming support for IIS) are really going to lower the bar to entry to online video and raise the bar in terms for user experience. Out of browser support for Windows and Mac will add a new dimension to some POCs – guess we are going to need a Mac to demo on.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Expression Web 3&lt;/strong&gt;: SuperPreview looks to be a very well thought out and needed feature of Expression Web.&lt;/p&gt;  &lt;p&gt;Oh and as for DeepZoom and Playboy – well I guess it was only a matter of time.&lt;/p&gt;  &lt;p&gt;For more details there is a write up of the keynote at:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://visitmix.com/Opinions/MIX09-Live-Blog-2-Advancing-User-Experiences-Scott-Guthrie" href="http://visitmix.com/Opinions/MIX09-Live-Blog-2-Advancing-User-Experiences-Scott-Guthrie"&gt;http://visitmix.com/Opinions/MIX09-Live-Blog-2-Advancing-User-Experiences-Scott-Guthrie&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;But the actual video is well worth a watch.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9488130" width="1" height="1"&gt;</description></item><item><title>Windows Azure Community meeting in the UK</title><link>http://blogs.msdn.com/neilkidd/archive/2009/03/17/windows-azure-community-meeting-in-the-uk.aspx</link><pubDate>Tue, 17 Mar 2009 18:52:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9483968</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9483968.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9483968</wfw:commentRss><description>&lt;p&gt;I’ve just found out about an attempt to start a UK based Windows Azure community. The initial meeting is on 31st March in London. Details are …&lt;/p&gt;  &lt;blockquote&gt;   &lt;p&gt;&lt;em&gt;We would like to invite you to a session titled “Windows Azure Update, What it is &amp;amp; What it isn’t” including a Q&amp;amp;A session with &lt;strong&gt;3 x members of the Azure team from Microsoft Corp &lt;/strong&gt;along with a session that introduces “UK AzureNET” which is the name of what we hope evolves into UK Azure User Group / community! &lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;Please see agenda below along with speakers bio and registration details below:-&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;b&gt;&lt;em&gt;Agenda &lt;/em&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;6.15pm - 6.30pm&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Arrive and Registration        &lt;br /&gt;6.30pm - 7:30pm&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Azure Update “What it is &amp;amp; What it isn’t” + Q&amp;amp;A         &lt;br /&gt;7.30pm - 8:00&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; UKAzurenet “Introduction”&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;8:00pm – 8.30&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160;&amp;#160; Food / drinks / Network&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;b&gt;&lt;em&gt;Matt Rogers (Senior Field Marketing Manager for &lt;/em&gt;&lt;a href="http://www.microsoft.com/azure/windowsazure.mspx"&gt;&lt;em&gt;Windows Azure&lt;/em&gt;&lt;/a&gt;&lt;em&gt;) &lt;/em&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;In this role he is responsible for global go-to-market strategy, understanding customer needs for the new service, and preparing Microsoft's global sales force and partner community for the upcoming launch. &lt;b&gt;&lt;/b&gt;&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;b&gt;&lt;em&gt;James Conard (Senior Director, Azure Services, .NET, and Visual Studio Evangelism) &lt;/em&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;James leads a global team of senior Microsoft evangelists who help customers realize the full potential of their IT investments using Microsoft’s Azure Services Platform, .NET Framework and Visual Studio.&amp;#160; James and his team have worked with customers and Microsoft partners around the world in understanding how these Microsoft offerings reduce IT costs and project timelines while delivering innovation solutions to market.&lt;/em&gt;&lt;/p&gt;    &lt;p&gt;&lt;b&gt;&lt;em&gt;Michael Maggs, Senior Director, Windows Azure Partner Marketing&lt;/em&gt;&lt;/b&gt;&lt;/p&gt;    &lt;p&gt;&lt;em&gt;Michael is responsible for the global partner ecosystem of Windows Azure.&amp;#160; He and his team work with leading system integrators, software development firms and IT consultancies to enable enterprises to adopt Microsoft’s cloud computing platform.&lt;/em&gt;&lt;/p&gt;    &lt;h5&gt;&lt;em&gt;How do I register? &lt;/em&gt;&lt;/h5&gt;    &lt;p&gt;&lt;em&gt;If you would like to attend this event please go to &lt;/em&gt;&lt;a href="http://azureupdate.eventbrite.com/"&gt;&lt;em&gt;http://azureupdate.eventbrite.com/&lt;/em&gt;&lt;/a&gt;&lt;em&gt;, sign up and if you have 2mins introduce yourself and say why you would like to attend.&lt;/em&gt;&lt;/p&gt; &lt;/blockquote&gt;  &lt;p&gt;If you have questions about the platform and future plans, who better to ask that the Product Group. Looks like it should be a good session, I’ll will certainly be trying to get there.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9483968" width="1" height="1"&gt;</description></item><item><title>Big Changes to SDS</title><link>http://blogs.msdn.com/neilkidd/archive/2009/03/10/big-changes-to-sds.aspx</link><pubDate>Wed, 11 Mar 2009 00:08:56 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9469667</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9469667.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9469667</wfw:commentRss><description>&lt;p&gt;&lt;img style="border-bottom: 0px; border-left: 0px; margin: 0px 5px 0px 0px; display: inline; border-top: 0px; border-right: 0px" title="SQLServices_h_rgb_r" border="0" alt="SQLServices_h_rgb_r" align="left" src="http://blogs.msdn.com/blogfiles/neilkidd/WindowsLiveWriter/BigChangestoSDS_1295F/SQLServices_h_rgb_r_3.png" width="111" height="112" /&gt; The SQL Data Services team are making some &lt;em&gt;big&lt;/em&gt; changes – ACE (Authority, Container, Entity) is no longer going to be supported and they are moving to expose TDS – TDS is the protocol that you are using to talk to your current SQL Servers!&lt;/p&gt;  &lt;p&gt;I know that The Product Group have had to make some hard decisions around this and if you have already committed a lot of time and effort to the ACE model, I hope that this change doesn’t hurt you too much. I also hope that the “new” functionality will prove this a worthwhile change.&lt;/p&gt;  &lt;p&gt;Personally I am very excited about this; as much more of the SQL that we know and love is being exposed – Stored Procs, Views, Indexes, ADO.Net Compatibility … for more information:&lt;/p&gt;  &lt;p&gt;&amp;#160; &lt;a title="http://blogs.msdn.com/ssds/archive/2009/03/10/9469228.aspx" href="http://blogs.msdn.com/ssds/archive/2009/03/10/9469228.aspx"&gt;http://blogs.msdn.com/ssds/archive/2009/03/10/9469228.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9469667" width="1" height="1"&gt;</description></item><item><title>Welcome Simon Davies</title><link>http://blogs.msdn.com/neilkidd/archive/2009/02/17/welcome-simon-davies.aspx</link><pubDate>Tue, 17 Feb 2009 22:38:50 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9428330</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9428330.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9428330</wfw:commentRss><description>&lt;p&gt;&lt;a href="http://blogs.msdn.com/simondavies/rss.xml"&gt;Simon&lt;/a&gt; has just started a, long overdue, blog. He is an Architect Evangelist – &lt;em&gt;yes I know we’ve heard all the Evangelist jokes and if I could change the job tile I would&lt;/em&gt; – in DPE in the UK and specialises in our latest and greatest emerging technologies. As he can walk on water and heal the sick; this will be well worth keeping an eye on.&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9428330" width="1" height="1"&gt;</description></item><item><title>Easy messages in Windows Azure</title><link>http://blogs.msdn.com/neilkidd/archive/2009/02/14/easy-messages-in-windows-azure.aspx</link><pubDate>Sat, 14 Feb 2009 16:09:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9421624</guid><dc:creator>neilk</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9421624.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9421624</wfw:commentRss><description>&lt;P&gt;For a while now I have been using a class that wraps and adds extra functionality to the queue in the StorageLib sample in the Windows Azure SDK. There are a few benefits that this wrapper provides, so I thought it might be time to share:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Strongly typed access to the queue. &lt;/LI&gt;
&lt;LI&gt;Warning when you forget to remove a message from the queue. &lt;/LI&gt;
&lt;LI&gt;Automatic serialization/deserialisation of the message content. &lt;/LI&gt;
&lt;LI&gt;Hooks to provide poison message detection and handling &lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;So lets take a look at each of these; if I’m going too slow and you can’t wait to get to the code then there is a link at the bottom of this post.&lt;/P&gt;
&lt;H3&gt;Strongly Typed Access&lt;/H3&gt;
&lt;P&gt;If you are only storing one type of message on your queue, then using Generics we can get rid of all the casting code that you would normally have to write. If we define a .Net type to represent the message that we are going to place on the queue, then we can create an API that becomes cleaner, type safe and more self describing.&lt;/P&gt;
&lt;P&gt;Creating our queue:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;  TypedQueue&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;NewUserRequest&lt;/SPAN&gt;&amp;gt; newUserQueue = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TypedQueue&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;NewUserRequest&lt;/SPAN&gt;&amp;gt;(&lt;SPAN style="COLOR: #a31515"&gt;"new-users"&lt;/SPAN&gt;);&lt;/PRE&gt;
&lt;P&gt;Writing to the queue&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;  NewUserRequest &lt;/SPAN&gt;nur = &lt;SPAN style="COLOR: blue"&gt;new &lt;/SPAN&gt;&lt;SPAN style="COLOR: #2b91af"&gt;NewUserRequest&lt;/SPAN&gt;( ... );
  newUserQueue.PushMessage(nur);&lt;/PRE&gt;
&lt;P&gt;and reading from the queue&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;  DequeuedMessage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;NewUserRequest&lt;/SPAN&gt;&amp;gt; deNur = newUserQueue.PopMessage ();
  ProcessNewUserRequest (deNur.MessageContents);
  newUserQueue.RemoveMessage (deNur);&lt;/PRE&gt;
&lt;P&gt;Ok, there is quite a bit of important code missing from these snippets, but hopefully you get the idea. The use of generics when defining the TypedQueue type allows for us to have a very clean way of working with our queues.&lt;/P&gt;
&lt;H3&gt;Warning when you forget to Remove a Message&lt;/H3&gt;
&lt;P&gt;It is all to easy to forget to call RemoveMessage when you have finished processing the message you just read from the queue. This is where the DequeuedMessage&amp;lt;&amp;gt; type comes in use. It is used to wrap the Message that comes back from the queue, but in debug builds it has a Finalizer method that will generate an Assert if the message was never passed to RemoveMessage. So any missing class to RemoveMessage should show up during your release cycle. This code won’t make it into a release build so there is no perf hit when you get to production.&lt;/P&gt;
&lt;H3&gt;Automatic Serialisation/Deserialisation &lt;/H3&gt;
&lt;P&gt;The TypedQueue class is using the XmlSerialzer to turn the .Net object into a form that can be written to the queue. This does mean that all the properties of your type need to be public and that you have to have a default constructor. This isn’t too much of an impact, but because the current CTP runs code in partial trust, we can’t use the binary formatter. One thing to be aware of is that we only have 8kb to serialise our message to, so if you are writing largish objects to the queue, you might want to use the XmlAttributeAttribute to help control the serialised form of your type.&lt;/P&gt;
&lt;P&gt;I know people that are using WCF’s DataContractSerializer but I have to confess that I have a much better understanding of how the XmlSerialiser works, hence I used the thing I know – if the only tool you have is a hammer; everything starts to look like a nail ;-) If you don’t like my old school serialisation it should be an easy change.&lt;/P&gt;
&lt;H3&gt;Hooks for poison message detection&lt;/H3&gt;
&lt;P&gt;The TypedQueue class allows you to specify a couple of delegates that will be used to detect and process poison messages. I have provided a coupe of implementations of these, but nothing more sophisticated than I blogged about in a &lt;A href="http://blogs.msdn.com/neilkidd/archive/2008/12/07/poison-in-windows-azure.aspx" mce_href="http://blogs.msdn.com/neilkidd/archive/2008/12/07/poison-in-windows-azure.aspx"&gt;previous post&lt;/A&gt;. &lt;/P&gt;
&lt;P&gt;If you define a PoisonMessgeCheck function, the TypedQueue class will use this to determine if a message is potentially poisonous before it tries to deserialise it. If this check says that the message is bad the PoisonMessageHandler delegate is called. I’ve only written very trivial implementations (look in PoisonMessageHelpers.cs) but it wouldn’t be too hard to write on that wrote the message contents to a poison message table. After this call, the TypedQueue will remove the message from the queue effectively deleting it.&lt;/P&gt;
&lt;P&gt;To set up the TypedQueue to look for message that are regarded as poisonous if they have been on the queue for 10 times the visibility timeout, your code would look like:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: #2b91af"&gt;TimeSpan &lt;/SPAN&gt;poisonTime = &lt;SPAN style="COLOR: #2b91af"&gt;TimeSpan&lt;/SPAN&gt;.FromSeconds (10 * newUserQueue.MessageVisabilityTimeOut);
newUserQueue.PoisonMessgeCheck = 
      &lt;SPAN style="COLOR: #2b91af"&gt;PoisonMessageHelpers&lt;/SPAN&gt;.PoisonIfOnQueueForLongerThan (poisonTime); &lt;/PRE&gt;
&lt;P&gt;This check is performed from the PopMessage function. If a message is poisonous, the API will behave exactly the same as if there had been no message on the queue – i.e. return a null. This means we don’t need to include or poison message code in our main queue processing loop allowing it to look like:&lt;/P&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: blue"&gt;  while &lt;/SPAN&gt;(&lt;SPAN style="COLOR: blue"&gt;true&lt;/SPAN&gt;) {
      &lt;SPAN style="COLOR: #2b91af"&gt;DequeuedMessage&lt;/SPAN&gt;&amp;lt;&lt;SPAN style="COLOR: #2b91af"&gt;NewUserRequest&lt;/SPAN&gt;&amp;gt; deNur = newUserQueue.PopMessage ();
      &lt;SPAN style="COLOR: blue"&gt;if &lt;/SPAN&gt;(deNur != &lt;SPAN style="COLOR: blue"&gt;null&lt;/SPAN&gt;) {
          ProcessNewUserRequest (deNur.MessageContents);
          newUserQueue.RemoveMessage (deNur);
      } &lt;SPAN style="COLOR: blue"&gt;else &lt;/SPAN&gt;{
          &lt;SPAN style="COLOR: #2b91af"&gt;Thread&lt;/SPAN&gt;.Sleep (NO_MESSAGE_SLEEP_TIME);
      }
  }&lt;/PRE&gt;
&lt;P&gt;If you haven’t seem my earlier posts and the Thread.Sleep looks a little strange, best have a quick read of &lt;A href="http://blogs.msdn.com/neilkidd/archive/2008/12/08/building-a-reliable-windows-azure-process-part-1.aspx" mce_href="http://blogs.msdn.com/neilkidd/archive/2008/12/08/building-a-reliable-windows-azure-process-part-1.aspx"&gt;this&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;The same pattern can be applied if you want to pull messages of the queue in batches, just pass the max number of messages you want to PopMessages. Any messages that are poisonous will be removed from the queue before any valid messages are returned in a List.&lt;/P&gt;
&lt;H3&gt;Disclaimer&lt;/H3&gt;
&lt;P&gt;The code I’ve attached has been ripped out of the code from a couple of Proof of Concept projects, as a result, you shouldn’t regard this as well tested. The concepts have been proved to work, but this implementation hasn’t.&lt;/P&gt;
&lt;P&gt;Oh, and the TypedQueue should be as thread safe as the StorageLib.Queue it is wrapping – no that wasn’t a statement that it is thread safe do probably best to create a separate instance per thread ;-)&lt;/P&gt;
&lt;P&gt;The code is &lt;A href="http://neilkidd.blob.core.windows.net/blog/code/TypedQueue.zip" mce_href="http://neilkidd.blob.core.windows.net/blog/code/TypedQueue.zip"&gt;here&lt;/A&gt; and bonus points if you notice where the file is stored ;-)&lt;/P&gt;
&lt;P&gt;Hope you find this as useful as I have.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Neil.&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9421624" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Code+Snippets/default.aspx">Code Snippets</category></item><item><title>Azure Briefing in Cambridge</title><link>http://blogs.msdn.com/neilkidd/archive/2009/01/15/azure-briefing-in-cambridge.aspx</link><pubDate>Thu, 15 Jan 2009 19:35:37 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9321123</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9321123.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9321123</wfw:commentRss><description>&lt;p&gt;Ok, last update on this subject, but we can now confirm that we will be in Cambridge on&lt;strong&gt; 10th Feb&lt;/strong&gt; delivering the Azure Briefing. For more details, see:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx" href="http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx"&gt;http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9321123" width="1" height="1"&gt;</description></item><item><title>The Azure tour heads North</title><link>http://blogs.msdn.com/neilkidd/archive/2009/01/14/the-azure-tour-heads-north.aspx</link><pubDate>Wed, 14 Jan 2009 15:02:59 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9318559</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9318559.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9318559</wfw:commentRss><description>&lt;p&gt;I am pleased to say that we have managed to arrange a couple more dates/locations for the Azure Technical Briefing. They are 11th Feb in &lt;strong&gt;Edinburgh&lt;/strong&gt; and 13th Feb in &lt;strong&gt;Bradford&lt;/strong&gt;.&lt;/p&gt;  &lt;p&gt;For more details, please see:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx" href="http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx"&gt;http://blogs.msdn.com/ukisvdev/archive/2009/01/14/new-dates-for-azure-technical-briefing-announced.aspx&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;We are hoping that we can also get to Cambridge that week, but haven’t yet got confirmation on the availability of the facilities, so watch this space. Right I’m off to try and persuade the powers-that-be that I need one of these to help me travel round the UK:&lt;/p&gt;  &lt;p&gt;&lt;a title="http://news.bbc.co.uk/2/hi/africa/7821979.stm" href="http://news.bbc.co.uk/2/hi/africa/7821979.stm"&gt;http://news.bbc.co.uk/2/hi/africa/7821979.stm&lt;/a&gt;&lt;/p&gt;  &lt;p&gt;Hope to see you there,&lt;/p&gt;  &lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9318559" width="1" height="1"&gt;</description></item><item><title>UK Azure Technical Briefings</title><link>http://blogs.msdn.com/neilkidd/archive/2009/01/06/uk-azure-technical-briefings.aspx</link><pubDate>Tue, 06 Jan 2009 16:00:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9285375</guid><dc:creator>neilk</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9285375.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9285375</wfw:commentRss><description>&lt;P&gt;We are running a couple of briefings on the Microsoft cloud technology stack for ISVs in the next few&amp;nbsp;weeks. I’ll be doing a bit on Windows Azure but the Live Platform and SQL Services will also be on the agenda. If you are interested in finding out about all this new cloud &lt;STRIKE&gt;magic&lt;/STRIKE&gt; &lt;STRIKE&gt;shenanigans&lt;/STRIKE&gt; technology then come along; listen to the presentations and join in the discussion afterwards.&lt;/P&gt;
&lt;P&gt;The first one will be in &lt;STRONG&gt;Reading on 20th Jan &lt;/STRONG&gt;and the second on &lt;STRONG&gt;in London 22nd Jan &lt;/STRONG&gt;in our offices in Victoria.&lt;/P&gt;
&lt;P&gt;More information and details of how to register at at:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &lt;A title=http://blogs.msdn.com/ukisvdev/archive/2008/12/18/azure-technical-briefings-for-jan-2009-just-announced.aspx href="http://blogs.msdn.com/ukisvdev/archive/2008/12/18/azure-technical-briefings-for-jan-2009-just-announced.aspx" mce_href="http://blogs.msdn.com/ukisvdev/archive/2008/12/18/azure-technical-briefings-for-jan-2009-just-announced.aspx"&gt;http://blogs.msdn.com/ukisvdev/archive/2008/12/18/azure-technical-briefings-for-jan-2009-just-announced.aspx&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;Now we are very aware that these sessions are very much “in the south” and as none of the presenters are from “the south”, we are considering redelivering this at other locations in the UK, so if you aren’t able to make it to Reading or London then please do let me know..&lt;/P&gt;
&lt;P&gt;Hope to see you there.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Neil&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9285375" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/MSArchitectPortal/default.aspx">MSArchitectPortal</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Events/default.aspx">Events</category></item><item><title>Every cloud has a Silverlight lining</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/18/every-cloud-has-a-silverlight-lining.aspx</link><pubDate>Thu, 18 Dec 2008 17:44:45 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9236286</guid><dc:creator>neilk</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9236286.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9236286</wfw:commentRss><description>&lt;p&gt;On a recent POC we found a bit of a problem with the time it was talking to deploy our application to Azure’s staging environment then deploy it into production. Now I must point out that we had a token that allowed us to have quite a lot of machine instances and those machines were being built and deployed far faster than I could build them – well actually faster than I could find my Windows 2008 DVD! However this time lag didn’t fit very well with the development cycle of our Silverlight UI. The application was basically a big XAP that talked to WCF services hosted in an Azure web role. As we approached the end of the POC, we were making infrequent changes to our service layer, but the UI code was being update very frequently – mostly small cosmetic fixes.&lt;/p&gt;  &lt;p&gt;When deploying an Azure web role, you need to package up the contents of your site - including all the static content, i.e. any Silverlight XAPs. This means that for every UI change that we wanted to preview/test against our Azure services we needed to deploy the whole application. To solve this problem we changed our app so that our XAPs were stored in the Azure blob store; allowing us to upload them whenever we wanted with almost zero turn round time – in fact our last build of the UI was less than 60 seconds before the demo!&lt;/p&gt;  &lt;p&gt;To do this required the following changes:&lt;/p&gt;  &lt;ol&gt;   &lt;li&gt;An ASP.Net page to upload an XAP into the blob store – not strictly necessary as you could use a tool like &lt;a href="http://www.codeplex.com/spaceblock"&gt;SpaceBlock&lt;/a&gt; to do this for you. &lt;/li&gt;    &lt;li&gt;An ASP.Net handler to serve the requests for XAPs &lt;/li&gt;    &lt;li&gt;Changing the embedded Silverlight tags to point at the handler rather than the static file (normally in ClientBin) &lt;/li&gt; &lt;/ol&gt;  &lt;h4&gt;&lt;strong&gt;The upload page&lt;/strong&gt;&lt;/h4&gt;  &lt;p&gt;Ok this won’t win any coding awards, nor should you put this into production, but it worked for our POC and is enough to get you started:&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    protected void &lt;/span&gt;Button1_Click(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
&lt;span style="color: blue"&gt;&lt;/span&gt;    {
        StoreXap(FileUpload1.FileName, &lt;span style="color: blue"&gt;this&lt;/span&gt;.FileUpload1.FileBytes);
    }

&lt;span style="color: blue"&gt;    protected void &lt;/span&gt;StoreXap(&lt;span style="color: #2b91af"&gt;String &lt;/span&gt;name, &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] data)
    {
        BlobContainer container = SLDownloader.GetBlobContainerForSLXaps();

        BlobProperties blobProps = &lt;span style="color: blue"&gt;new &lt;/span&gt;BlobProperties(name);
        blobProps.ContentType = &lt;span style="color: #a31515"&gt;&amp;quot;application/x-silverlight-2&amp;quot;&lt;/span&gt;;

        BlobContents blobConts = &lt;span style="color: blue"&gt;new &lt;/span&gt;BlobContents(data);
        container.CreateBlob(blobProps, blobConts, &lt;span style="color: blue"&gt;true&lt;/span&gt;);
    }

&lt;span style="color: blue"&gt;    private static &lt;/span&gt;BlobContainer GetBlobContainerForSLXaps()
    {
        StorageAccountInfo sai = StorageAccountInfo.GetDefaultBlobStorageAccountFromConfiguration();
        BlobStorage blobStorage = BlobStorage.Create(sai);

        BlobContainer container = blobStorage.GetBlobContainer(&lt;span style="color: #a31515"&gt;&amp;quot;XAP&amp;quot;&lt;/span&gt;);
        container.CreateContainer();
 
        &lt;span style="color: blue"&gt;return &lt;/span&gt;container;
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;/p&gt;

&lt;p&gt;Where FileUpload1 is just an ASP.Net upload control:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: blue"&gt;&amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;form &lt;/span&gt;&lt;span style="color: red"&gt;id&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;form1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
        &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;FileUpload &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;FileUpload1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;div&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
    &amp;lt;&lt;/span&gt;&lt;span style="color: #a31515"&gt;asp&lt;/span&gt;&lt;span style="color: blue"&gt;:&lt;/span&gt;&lt;span style="color: #a31515"&gt;Button &lt;/span&gt;&lt;span style="color: red"&gt;ID&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;runat&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;server&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;onclick&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button1_Click&amp;quot; &lt;/span&gt;&lt;span style="color: red"&gt;Text&lt;/span&gt;&lt;span style="color: blue"&gt;=&amp;quot;Button&amp;quot; /&amp;gt;
    &amp;lt;/&lt;/span&gt;&lt;span style="color: #a31515"&gt;form&lt;/span&gt;&lt;span style="color: blue"&gt;&amp;gt;
&lt;/span&gt;&lt;/pre&gt;

&lt;h4&gt;&lt;strong&gt;The ASP.Net Handler&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt;The ASP.Net handler looks like:&lt;/p&gt;

&lt;pre class="code"&gt;[WebService(Namespace = &lt;span style="color: #a31515"&gt;&amp;quot;http://tempuri.org/&amp;quot;&lt;/span&gt;)]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
&lt;span style="color: blue"&gt;public class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;SLDownloader &lt;/span&gt;: IHttpHandler
{
    &lt;span style="color: blue"&gt;public void &lt;/span&gt;ProcessRequest(HttpContext context)
    {
        WriteImageToHttpResponse(context.Request.QueryString[&lt;span style="color: #a31515"&gt;&amp;quot;xap&amp;quot;&lt;/span&gt;], context.Response);
    }

    &lt;span style="color: blue"&gt;public static void &lt;/span&gt;WriteImageToHttpResponse(&lt;span style="color: #2b91af"&gt;String &lt;/span&gt;xapName, HttpResponse response)
    {
        &lt;span style="color: blue"&gt;byte&lt;/span&gt;[] xapData = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;span style="color: #2b91af"&gt;String &lt;/span&gt;contentType;
        BlobContainer container = GetBlobContainerForSLXaps();

        &lt;span style="color: #2b91af"&gt;MemoryStream &lt;/span&gt;ms = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;MemoryStream&lt;/span&gt;();
        BlobContents blobConts = &lt;span style="color: blue"&gt;new &lt;/span&gt;BlobContents(ms);
        BlobProperties blobProps = &lt;span style="color: blue"&gt;null&lt;/span&gt;;
        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            blobProps = container.GetBlob(xapName, blobConts, &lt;span style="color: blue"&gt;false&lt;/span&gt;);
            xapData = ms.GetBuffer();
            response.ContentType = blobProps.ContentType;
            response.OutputStream.Write(xapData, 0, xapData.Length);
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;(StorageClientException sce) 
        {
            &lt;span style="color: green"&gt;// Ok, the XAP we wasn't isn't there, return a 404?
            // ...
        &lt;/span&gt;}
    }

    &lt;span style="color: blue"&gt;public bool &lt;/span&gt;IsReusable
    {
        &lt;span style="color: blue"&gt;get &lt;/span&gt;{ &lt;span style="color: blue"&gt;return false&lt;/span&gt;; }
    }
}&lt;/pre&gt;

&lt;h4&gt;&lt;strong&gt;Changing the tag&lt;/strong&gt;&lt;/h4&gt;

&lt;p&gt; And finally fix up the Silverlight control so that it gets the XAP from the handler rather than from the more conventional static file in ClientBin: &lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;    protected void &lt;/span&gt;Page_Load(&lt;span style="color: blue"&gt;object &lt;/span&gt;sender, &lt;span style="color: #2b91af"&gt;EventArgs &lt;/span&gt;e)
    {
        Xaml1.Source = &lt;span style="color: #a31515"&gt;&amp;quot;~/SLDownloader.ashx?xap=MySilverlightApp.xap&amp;quot;&lt;/span&gt;;
    }&lt;/pre&gt;

&lt;p&gt;We could have just made the blob &lt;a href="http://msdn.microsoft.com/en-us/library/dd179354.aspx"&gt;container public&lt;/a&gt; pointed the Silverlight straight at the XAP blob, but in this case there were a few reasons for using the handler:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;I knew that using the handler would work – &lt;em&gt;I needed to get it working quickly&lt;/em&gt; &lt;/li&gt;

  &lt;li&gt;Wouldn’t cause any issues with Silverlight’s web &lt;a href="http://msdn.microsoft.com/en-us/library/cc645032(VS.95).aspx"&gt;service policy&lt;/a&gt;&amp;#160; &lt;/li&gt;

  &lt;li&gt;We were using LiveId for authentication and I didn’t want to bypass any checks we were doing and download the XAP to just anyone and &lt;/li&gt;

  &lt;li&gt;I modified the handler to return the file from the ClientBin directory if the blob didn’t exist – i.e. the blob version overrode the one that was shipped – it was a demo app and I was worried that we might forget to upload the XAP to storage ;-) &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Easy and quite a powerful technique to decouple your XAPs – well any static content - from your Azure deployment package. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9236286" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category></item><item><title>Reliable Azure Processes - Part 4 - Scaling Down!</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/15/reliable-azure-processes-part-4-scaling-down.aspx</link><pubDate>Mon, 15 Dec 2008 19:55:49 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9221399</guid><dc:creator>neilk</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9221399.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9221399</wfw:commentRss><description>&lt;p&gt;One thing that you need to consider when designing your Azure application is &amp;quot;&lt;em&gt;what is the minimum deployment&lt;/em&gt;&amp;quot;? If your application has 5 different queues with a worker role to read each queue, we are going to want at least 2 instances per role (for some redundancy) giving you a minimum deployment of 5 packages and 10 instances. Hmmm, if you have very little traffic that seems a little extravagant; so let take a look at how we can scale down our requirements and potentially make deployment easier. If you aren’t used to multi-threaded programming; the first thing that probably springs to mind is to round robin all of our queues in one big loop:&lt;/p&gt;  &lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Start()
    {
        &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;)
        {
            ProcessQueue1();
            ProcessQueue2();
            &lt;span style="color: rgb(0,128,0)"&gt;// ....
&lt;/span&gt;            ProcessQueueN();
        }
    }&lt;/pre&gt;

&lt;p&gt;Assume that queues 1 to N have different messages and require different processing – otherwise we would put them all in the same queue – &lt;em&gt;right?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;While this would work, we can do better. There a couple of things we should look to improve on:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;&lt;strong&gt;Idle CPU time&lt;/strong&gt;: A lot of the time we are going to do something along the lines of; read from queue, write to table/blob/queue/SDS then delete the message from the queue. Now we might end up doing some processing of our message, but there is significant amount of network traffic going on. Every time we read/write to the storage system we are making an off machine call. This means that our application will block until the network call is completed - i.e. our node is doing nothing. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Fault tolerance&lt;/strong&gt;: If there is an error with one of the processors - say a &lt;a href="http://blogs.msdn.com/neilkidd/archive/2008/12/07/poison-in-windows-azure.aspx"&gt;poison message&lt;/a&gt; on Queue 2, then ProcessQueueN is never going to get checked. &lt;/li&gt;

  &lt;li&gt;&lt;strong&gt;Simplicity&lt;/strong&gt;: We might have a high priority queue that we want to check very frequently and a low priority queue that we don't check that often. Any logic we add to achieve this is going to get complex and won't be very easy to change through configuration. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By multi-threading our worker role, we should be able to solve all these issues. One thing to keep in mind is that there is only 1 CPU in the virtual machine that the CTP of Azure builds for you; so multithreading isn't going to help if we are doing lots of CPU bound processing. Oh, and one proc means that the parallel extensions won't help us here as they will only ever give us one thread - thanks to &lt;a href="http://www.danielmoth.com/Blog/"&gt;Daniel&lt;/a&gt; for confirming that over a beer at TechEd.&lt;/p&gt;

&lt;p&gt;There are 2 types of people who &lt;em&gt;don’t&lt;/em&gt; write multi-threaded code; those that don’t understand it and have never tried and those who &lt;em&gt;really &lt;/em&gt;understand it and know how difficult it is to get right. Having said that, here are a few steps that will multi-thread our application without us having to step too far into the multi-threading minefield.&lt;/p&gt;

&lt;p&gt;If you have followed &lt;a href="http://blogs.msdn.com/neilkidd/archive/2008/12/10/reliable-azure-processes-part-3-dealing-with-errors.aspx"&gt;Part 3&lt;/a&gt;, you will know that we currently have a nice class with an instance method that performs all our processing; this positions us very nicely to multi-thread our app. The biggest problem people get into when writing concurrent applications is protecting (or not protecting) access to shared resources (variables, files, queues …). If multiple threads access the same thing at the same time, there could be problems – a bit like two kids grabbing for the ketchup at the same time – there will be tears and ketchup everywhere. By moving our MessageQueue instance into our processor class and making it private should ensure that only that processor instance can access it. If we create one processor per thread and ensure that all the resources that that processor needs are contained in that processor object we no longer have any shared resources – every kid gets their own bottle of ketchup. This neatly lets us step over the first mine.&lt;/p&gt;

&lt;p&gt;For every different queue/message type that we are going to be monitoring, we need to create a new IProcessMessages implementation. If we just want several threads monitoring the same queue, then we would just create several instances of our single processor type and execute each one on a separate thread.&lt;/p&gt;

&lt;p&gt;Because we are going to have several processors executing, we are going to need to keep track of them, so lets create a type to do that:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;class &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;{

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;The last time this thread completed some work
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;DateTime &lt;/span&gt;LastThreadTest { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;set&lt;/span&gt;; }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;The type that represents this processor
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;processorType;

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;The instance of the processor type that we are wrapping
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public &lt;/span&gt;&lt;span style="color: #2b91af"&gt;IProcessMessages &lt;/span&gt;Processor { &lt;span style="color: blue"&gt;get&lt;/span&gt;; &lt;span style="color: blue"&gt;private set&lt;/span&gt;; }

    &lt;span style="color: blue"&gt;public &lt;/span&gt;ProcessorRecord(&lt;span style="color: #2b91af"&gt;Type &lt;/span&gt;type)
    {
        processorType = type;
        ResetProcessor();
    }

    &lt;span style="color: gray"&gt;/// &amp;lt;summary&amp;gt;
    /// &lt;/span&gt;&lt;span style="color: green"&gt;Recreate the processor instance - used in the event of an error.
    &lt;/span&gt;&lt;span style="color: gray"&gt;/// &amp;lt;/summary&amp;gt;
    &lt;/span&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;ResetProcessor()
    {
        Processor = (&lt;span style="color: #2b91af"&gt;IProcessMessages&lt;/span&gt;)&lt;span style="color: #2b91af"&gt;Activator&lt;/span&gt;.CreateInstance(processorType);
        LastThreadTest = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;
    }
}&lt;/pre&gt;

&lt;p&gt;That is all reasonably straight forward. We use the type class rather than an instance of a processor because we will want to re-create the instance if an error occurs – see &lt;a href="http://blogs.msdn.com/neilkidd/archive/2008/12/10/reliable-azure-processes-part-3-dealing-with-errors.aspx"&gt;part 3&lt;/a&gt; for more details. &lt;/p&gt;

&lt;p&gt;Now we come to modifying our main loop. One change that is going to happen here is that we are going to use the main thread to periodically check the status of all our processors. This is better done here rather than in the GetHealthStatus function simply because we have more control over when it gets called. e.g. if GetHealthStatus gets called every 5 seconds and we don’t expect to hear from our threads for 20 seconds then we are executing the health check logic far to often – this wasn't a problem with our single threaded implementation, but now we have several time stamps to check. So the code for GetHealthStatus gets even simpler - so it just reports on the contents of a class variable:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;private &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RoleStatus &lt;/span&gt;status;

&lt;span style="color: blue"&gt;public override &lt;/span&gt;&lt;span style="color: #2b91af"&gt;RoleStatus &lt;/span&gt;GetHealthStatus()
{
    &lt;span style="color: blue"&gt;return &lt;/span&gt;status;
}&lt;/pre&gt;

&lt;p&gt;Our Start function is modified so it starts a thread per processor then to spend the rest of the apps lifetime checking their last reporting times:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public override void &lt;/span&gt;Start()
{
    status = &lt;span style="color: #2b91af"&gt;RoleStatus&lt;/span&gt;.Healthy;

    &lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;[] processors = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProcessorRecord&lt;/span&gt;[] { 
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ProcessQueue1&lt;/span&gt;)),
        &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;(&lt;span style="color: blue"&gt;typeof&lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ProcessQueue2&lt;/span&gt;)),
    };


    &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;pr &lt;span style="color: blue"&gt;in &lt;/span&gt;processors) {
        &lt;span style="color: #2b91af"&gt;Thread &lt;/span&gt;th = &lt;span style="color: blue"&gt;new &lt;/span&gt;&lt;span style="color: #2b91af"&gt;Thread &lt;/span&gt;(RunProcessor);
        th.Start (pr);
    }

    &lt;span style="color: green"&gt;// Now just monitor the health of the threads that we have created.
    //
    &lt;/span&gt;&lt;span style="color: blue"&gt;while &lt;/span&gt;(&lt;span style="color: blue"&gt;true&lt;/span&gt;)
    {
        &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(HEALTH_CHECK_SLEEP);
        &lt;span style="color: #2b91af"&gt;RoleStatus &lt;/span&gt;localStatus = &lt;span style="color: #2b91af"&gt;RoleStatus&lt;/span&gt;.Healthy;

        &lt;span style="color: blue"&gt;lock &lt;/span&gt;(dateTimeLock)
        {
            &lt;span style="color: blue"&gt;foreach &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;pr &lt;span style="color: blue"&gt;in &lt;/span&gt;processors)
            {
                &lt;span style="color: blue"&gt;if &lt;/span&gt;(lastHealthyReport.AddSeconds(DEFAULT_REPORT_TIMEOUT) &amp;lt; &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now)
                {
                    localStatus = &lt;span style="color: #2b91af"&gt;RoleStatus&lt;/span&gt;.Unhealthy;
                    &lt;span style="color: green"&gt;// No need to continue looking.
                    &lt;/span&gt;&lt;span style="color: blue"&gt;break&lt;/span&gt;;
                }
            }
        }
        status = localStatus;
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;And finally the code that was in our Start method at the end of part 3, makes it into the RunProcessor method; this is the start point for our new thread.&lt;/p&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;span style="color: blue"&gt;public void &lt;/span&gt;RunProcessor (&lt;span style="color: #2b91af"&gt;Object &lt;/span&gt;state) {
    &lt;span style="color: #2b91af"&gt;ProcessorRecord &lt;/span&gt;pr = (&lt;span style="color: #2b91af"&gt;ProcessorRecord&lt;/span&gt;)state;
    &lt;span style="color: blue"&gt;int &lt;/span&gt;sleepTimeIndex = 0;

    &lt;span style="color: blue"&gt;while &lt;/span&gt;(&lt;span style="color: blue"&gt;true&lt;/span&gt;)
    {
        &lt;span style="color: blue"&gt;try
        &lt;/span&gt;{
            &lt;span style="color: blue"&gt;if &lt;/span&gt;(pr.Processor.Process() == &lt;span style="color: blue"&gt;false&lt;/span&gt;)
            {
                &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(DEFAULT_SLEEP_TIME);
            }

            &lt;span style="color: green"&gt;// Updating the date time only if an error is not occur
            &lt;/span&gt;&lt;span style="color: blue"&gt;lock &lt;/span&gt;(dateTimeLock)
            {
                pr.LastThreadTest = &lt;span style="color: #2b91af"&gt;DateTime&lt;/span&gt;.Now;
            }

            &lt;span style="color: green"&gt;// reset the sleep time
            &lt;/span&gt;sleepTimeIndex = 0;
        }
        &lt;span style="color: blue"&gt;catch &lt;/span&gt;(&lt;span style="color: #2b91af"&gt;Exception &lt;/span&gt;ex)
        {
            &lt;span style="color: #2b91af"&gt;RoleManager&lt;/span&gt;.WriteToLog(&lt;span style="color: #a31515"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;,
                &lt;span style="color: #a31515"&gt;&amp;quot;Error occured, trying again&amp;quot;
                &lt;/span&gt;+ ex.ToString());
            pr.ResetProcessor();

            &lt;span style="color: green"&gt;// Sleep using our back off
            &lt;/span&gt;sleepTimeIndex = &lt;span style="color: #2b91af"&gt;Math&lt;/span&gt;.Min(sleepTimeIndex++, SLEEP_TIMES.Length - 1);
            &lt;span style="color: #2b91af"&gt;Thread&lt;/span&gt;.Sleep(SLEEP_TIMES[sleepTimeIndex]);
        }
    }
}&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That leaves us with the same reliability that we built into part 3, but enables us to run multiple processors on multiple threads. It isn’t too much of a stretch of the imagination to consider storing the information that the ProcessorRecord class need in table storage. This would be the name of the processor and potentially sleep time and how long the thread is allowed between report times before it goes unhealthy. This would allow us to configure at runtime the number and types of processors running on our worker roles – very useful if we start to see much more traffic of a particular message type than another – add some more threads rather than some more machine instances. More interestingly, it also means that we only have to build and deploy one package to Azure. This model might not suit everyone, but it is a great starting point – especially if you have a CTP token that only allows you to have one type of worker role!&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Neil.&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9221399" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/MSArchitectPortal/default.aspx">MSArchitectPortal</category></item><item><title>Reliable Azure Processes - Part 3 - Dealing with errors</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/10/reliable-azure-processes-part-3-dealing-with-errors.aspx</link><pubDate>Wed, 10 Dec 2008 20:09:22 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9191665</guid><dc:creator>neilk</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9191665.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9191665</wfw:commentRss><description>&lt;p&gt;After the last 2 blog entries, we have our worker process's main loop feed back into Azure's heath checking system. If we go unhealthy, Azure will notice and will eventually restart the worker role. However, this is a bit heavy handed; what if the failure was a temporary network error, a poison message or another bug - do we really want our node to die so easily? By refactoring our code slightly we can make our process far more robust and harder to kill than Chuck Norris!&lt;/p&gt;  &lt;p&gt;Step 1, we need an interface - remember: &lt;em&gt;every computer science problem can be solved by adding a new level of abstraction&lt;/em&gt;; but beware: &lt;em&gt;a new level of abstraction will introduce a new problem&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;Lets call it IProcessMessages - I prefer interface names that are grammatically correct - IDoThings, IHateThings, IDontGetOutMuch:&lt;/p&gt;  &lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;interface&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;IProcessMessages&lt;/span&gt; 
    {
        &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; Process ();
    }&lt;/pre&gt;

&lt;p&gt;Were Process will return true if it found a message and false if the queue was empty; we need this to determine if the node should have a quick sleep before checking the queue again.&lt;/p&gt;

&lt;p&gt;We can now create some processor types:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;class&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; : &lt;span style="color: rgb(43,145,175)"&gt;IProcessMessages&lt;/span&gt; {

        &lt;span style="color: rgb(43,145,175)"&gt;MessageQueue&lt;/span&gt; queue;

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; QueueProcessor ()
        {
            &lt;span style="color: rgb(43,145,175)"&gt;QueueStorage&lt;/span&gt; qs = &lt;span style="color: rgb(43,145,175)"&gt;QueueStorage&lt;/span&gt;.Create (
                      &lt;span style="color: rgb(43,145,175)"&gt;StorageAccountInfo&lt;/span&gt;.GetDefaultQueueStorageAccountFromConfiguration());
            queue = qs.GetQueue (&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;myqueue&amp;quot;&lt;/span&gt;);
        }

        &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;bool&lt;/span&gt; Process()
        {
            &lt;span style="color: rgb(43,145,175)"&gt;Message&lt;/span&gt; msg = queue.GetMessage ();
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (msg != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;) {
                ProcessMessage (msg);
            }
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; msg != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;;
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We've created an instance type and the initialisation of the queue is moved into the constructor. The creation of a queue is an expensive operation, as GetQueue involves a trip to the storage system, so we've moved it out of our processing loop. We can now modify our main loop to look like:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Start()
    {
        &lt;strong&gt;&lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; p = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; ();&lt;/strong&gt;

        &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;)
        {            
            &lt;span style="color: rgb(0,128,0)"&gt;// Updating the date time in the main loop
&lt;/span&gt;            &lt;span style="color: rgb(0,0,255)"&gt;lock&lt;/span&gt; (dateTimeLock)
            {
                lastThreadTest = &lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now;
            }            
            
            &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (&lt;strong&gt;p.Process ()&lt;/strong&gt; == &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;) {
                &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(DEFAULT_SLEEP_TIME);
            }
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Functionally we are now pretty much back to what we had at the end of Part 2. Time for our bulletproof vest ... we can now update our main loop to catch any exceptions thrown from the process method. If we catch an exception, we can destroy the current processor, create a new one and carry on as though nothing happened. Now this isn't going to work for all errors and normally I wouldn't recommend catching exceptions that you can't explicitly deal with; as some of them could be unrecoverable. However, we have Azure's status check watching our back. If we don't update our lastThreadTest counter in the event of an exception, we will eventually go unhealthy and Azure will restart us. So our main loop looks like:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;void&lt;/span&gt; Start()
    {
        &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; p = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; ();

        &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;)
        {            
&lt;strong&gt;            &lt;span style="color: rgb(0,0,255)"&gt;try&lt;/span&gt; {&lt;/strong&gt;
                &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (p.Process () == &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;) {
                    &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(DEFAULT_SLEEP_TIME);
                }

                &lt;span style="color: rgb(0,128,0)"&gt;// Updating the date time only if an error is not occur
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;lock&lt;/span&gt; (dateTimeLock)
                {
                    lastThreadTest = &lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now;
                }            

&lt;strong&gt;            } &lt;span style="color: rgb(0,0,255)"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex) {
                &lt;span style="color: rgb(43,145,175)"&gt;RoleManager&lt;/span&gt;.WriteToLog(&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;,
                    &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Error occurred, trying again&amp;quot;
&lt;/span&gt;                    + ex.ToString());
                p = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; ();
            }&lt;/strong&gt;
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;We might want to adjust the catch block to perform a sleep, just so we don't spin at 100% cpu in the event of repeated errors - this could even be on a back off algorithm, so the first couple of retry's have no sleep, then 100ms,500ms, ... Just so that longer lasting errors (network?) won't keep us to busy throwing exceptions. The easiest way for doing a back of is to declare an array of sleep times:&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;private&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;const&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;int&lt;/span&gt; [] SLEEP_TIMES = { 0, 0, 100, 500, 1000, 2000, 5000, 10000 };&lt;/pre&gt;

&lt;p&gt;and have a local variable keep track of the current sleep time index:&lt;/p&gt;

&lt;pre class="code"&gt;        &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;)
        {            
            &lt;span style="color: rgb(0,0,255)"&gt;try&lt;/span&gt; {
                &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (p.Process () == &lt;span style="color: rgb(0,0,255)"&gt;false&lt;/span&gt;) {
                    &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(DEFAULT_SLEEP_TIME);
                }

                &lt;span style="color: rgb(0,128,0)"&gt;// Updating the date time only if an error is not occur
&lt;/span&gt;                &lt;span style="color: rgb(0,0,255)"&gt;lock&lt;/span&gt; (dateTimeLock)
                {
                    lastThreadTest = &lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now;
                }            

&lt;strong&gt;                &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: rgb(0,128,0)"&gt;// reset the sleep time
&lt;/span&gt;                sleepTimeIndex = 0;&lt;/strong&gt;

            } &lt;span style="color: rgb(0,0,255)"&gt;catch&lt;/span&gt; (&lt;span style="color: rgb(43,145,175)"&gt;Exception&lt;/span&gt; ex) {
                &lt;span style="color: rgb(43,145,175)"&gt;RoleManager&lt;/span&gt;.WriteToLog(&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;,
                    &lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Error occured, trying again&amp;quot;
&lt;/span&gt;                    + ex.ToString());
                p = &lt;span style="color: rgb(0,0,255)"&gt;new&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;QueueProcessor&lt;/span&gt; ();
&lt;strong&gt;&lt;/strong&gt;&lt;/pre&gt;

&lt;pre class="code"&gt;&lt;strong&gt;                &lt;/strong&gt;&lt;strong&gt;&lt;span style="color: rgb(0,128,0)"&gt;// Sleep using our back off
&lt;/span&gt;                sleepTimeIndex = &lt;span style="color: rgb(43,145,175)"&gt;Math&lt;/span&gt;.Min (sleepTimeIndex++, SLEEP_TIMES.Length - 1);
                &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep (SLEEP_TIMES [sleepTimeIndex]);&lt;/strong&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;pre class="code"&gt;&lt;strong&gt;&lt;/strong&gt;            }
        }&lt;/pre&gt;

&lt;p&gt;We can now survive errors with a more appropriate response and only rely on a restart in the event of repeated errors.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Neil&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9191665" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Code+Snippets/default.aspx">Code Snippets</category></item><item><title>Reliable Azure Processes - Part 2 - was part 1 thread safe?</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/08/reliable-azure-processes-part-2-was-part-1-thread-safe.aspx</link><pubDate>Tue, 09 Dec 2008 01:06:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9185581</guid><dc:creator>neilk</dc:creator><slash:comments>1</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9185581.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9185581</wfw:commentRss><description>&lt;P&gt;Last week, for a Azure POC, we implemented something similar to the pattern shown in &lt;A href="http://blogs.msdn.com/neilkidd/archive/2008/12/08/building-a-reliable-windows-azure-process-part-1.aspx" mce_href="http://blogs.msdn.com/neilkidd/archive/2008/12/08/building-a-reliable-windows-azure-process-part-1.aspx"&gt;Part 1&lt;/A&gt;. One revision, that I asked to be made, was to surround the DateTime access code with a lock statement; I was worried that updating a DateTime struct would not be thread safe - i.e the thread querying the lastHeathyReport would be reading its value while the main thread was part way through modifying it. This could lead to the health check comparing against an incorrect or invalid DateTime and unnecessarily mark up the role as unhealthy. The code to perform the locking would look like:&lt;/P&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,128,0)"&gt;// Declaring the date time and our lock
&lt;/SPAN&gt;&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;&lt;/SPAN&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;DateTime&lt;/SPAN&gt; lastThreadTest;

    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;private&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Object&lt;/SPAN&gt; dateTimeLock = &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;new&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;Object&lt;/SPAN&gt;();&lt;/PRE&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;void&lt;/SPAN&gt; Start()
    {
        &lt;SPAN style="COLOR: rgb(0,128,0)"&gt;// This is a sample worker implementation. Replace with your logic.
&lt;/SPAN&gt;        &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;RoleManager&lt;/SPAN&gt;.WriteToLog(&lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Information"&lt;/SPAN&gt;, &lt;SPAN style="COLOR: rgb(163,21,21)"&gt;"Worker Process entry point called"&lt;/SPAN&gt;);

        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;while&lt;/SPAN&gt; (&lt;SPAN style="COLOR: rgb(0,0,255)"&gt;true&lt;/SPAN&gt;)
        {&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;            &lt;SPAN style="COLOR: rgb(0,128,0)"&gt;// Updating the date time in the main loop
&lt;/SPAN&gt;            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (dateTimeLock)
            {
                lastThreadTest = &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;DateTime&lt;/SPAN&gt;.Now;
            }&lt;/PRE&gt;&lt;PRE class=code&gt;            &lt;SPAN style="COLOR: rgb(0,128,0)"&gt;// Do some work&lt;/SPAN&gt;&lt;/PRE&gt;&lt;PRE class=code&gt;&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;&lt;/SPAN&gt;        }
    }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;&lt;PRE class=code&gt;    &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;public&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;override&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;RoleStatus&lt;/SPAN&gt; GetHealthStatus()
    {
&lt;SPAN style="COLOR: rgb(0,128,0)"&gt;        // Querying the date time
&lt;/SPAN&gt;&lt;STRONG&gt;        &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;lock&lt;/SPAN&gt; (dateTimeLock)
&lt;/STRONG&gt;        {
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;if&lt;/SPAN&gt; (lastHealthyReport.AddSeconds(30) &amp;lt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;DateTime&lt;/SPAN&gt;.Now)
            {
                &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;RoleStatus&lt;/SPAN&gt;.Unhealthy;
            }
            &lt;SPAN style="COLOR: rgb(0,0,255)"&gt;return&lt;/SPAN&gt; &lt;SPAN style="COLOR: rgb(43,145,175)"&gt;RoleStatus&lt;/SPAN&gt;.Healthy;
        }
    }&lt;/PRE&gt;&lt;A href="http://11011.net/software/vspaste" mce_href="http://11011.net/software/vspaste"&gt;&lt;/A&gt;
&lt;P&gt;Having researched whether the lock is need, I can categorically state that the answer is ... "&lt;EM&gt;it depends"&lt;/EM&gt;. The DateTime struct in the .Net framework uses an unsigned 64 bit integer to store its value. Assignment of a 64 value on a 32 bit machine &lt;EM&gt;is not&lt;/EM&gt; atomic - it takes 2 machine instructions to write 64 bits, however on a 64 bit machine it &lt;EM&gt;is&lt;/EM&gt; atomic. As windows Azure instances are 64 bit machines, the question of whether to lock is probably going to come down to what your dev machine is. However, taking out an uncontested lock in .Net is a very inexpensive operation, so I think, my recommendation would be to put the lock in.&lt;/P&gt;
&lt;P&gt;If you are interested, the specs of an Azure machine for the Tech Preview are as follows - this is the virtual machine, not the actual physical servers!&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Platform: 64-bit Windows Server 2008&lt;/LI&gt;
&lt;LI&gt;CPU: 1.5-1.7 GHz x64 equivalent&lt;/LI&gt;
&lt;LI&gt;Memory: 1.7 GB&lt;/LI&gt;
&lt;LI&gt;Network: 100 Mbps&lt;/LI&gt;
&lt;LI&gt;Transient local storage: 250 GB&lt;/LI&gt;&lt;/UL&gt;
&lt;P&gt;And for more information on thread safety and torn reads, see &lt;A href="http://www.bluebytesoftware.com/blog/2006/02/08/ThreadsafetyTornReadsAndTheLike.aspx" mce_href="http://www.bluebytesoftware.com/blog/2006/02/08/ThreadsafetyTornReadsAndTheLike.aspx"&gt;Joe Duffy's posting&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&lt;EM&gt;Neil&lt;/EM&gt;&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9185581" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Code+Snippets/default.aspx">Code Snippets</category></item><item><title>Building a Reliable Windows Azure Process - Part 1</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/08/building-a-reliable-windows-azure-process-part-1.aspx</link><pubDate>Mon, 08 Dec 2008 18:59:25 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9184938</guid><dc:creator>neilk</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9184938.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9184938</wfw:commentRss><description>&lt;p&gt;Building a decoupled, queue based system is will give you the ability to scale and the opportunity to create a highly available application. By dispatching work to multiple back end worker roles we are building a system that can survive unfortunate events like bugs, exceptions, hardware failure, fire, flood, pestilence and the other horsemen of the developer apocalypse - ok, maybe I'm getting carried away and our application won't need to survive all that, but you get the idea. If one of our worker roles dies; another can take its place; Due to the &lt;a href="http://blogs.msdn.com/neilkidd/archive/2008/12/07/poison-in-windows-azure.aspx"&gt;reliable nature of the queues&lt;/a&gt;, anything it was working on will be reprocessed by another node and all is well.&lt;/p&gt;  &lt;p&gt;Azure includes a mechanism where it can check on the heath of your nodes. ASP.Net already has this functionality built into it, but for a worker role we need to role this ourselves. All worker roles derive from RoleEntryPoint which defines a method that Azure will call periodically to determine if your role is working. The default implementation looks like:&lt;/p&gt;  &lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;RoleStatus&lt;/span&gt; GetHealthStatus()
    {
        &lt;span style="color: rgb(0,128,0)"&gt;// This is a sample worker implementation. Replace with your logic.
&lt;/span&gt;        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;RoleStatus&lt;/span&gt;.Healthy;
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;That's all well and good if our app is perfect and bug free, but in reality we are going to want to make this a little bit smarter. There are a variety of things that we can return from this function, but for the most part all we will be interested in is &lt;font face="Courier New"&gt;Healthy&lt;/font&gt; or &lt;font face="Courier New"&gt;Unhealthy&lt;/font&gt;. A sensible implementation would be to get our main worker process to periodically update a timestamp to show that it is working correctly and to check this timestamp in GetHealthStatus. So lets build ourselves a main processing loop for a worker role.&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;while&lt;/span&gt; (&lt;span style="color: rgb(0,0,255)"&gt;true&lt;/span&gt;)
    {
        &lt;span style="color: rgb(0,128,0)"&gt;// Report in
&lt;/span&gt;        lastHealthyReport = &lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now;

        &lt;span style="color: rgb(0,128,0)"&gt;// Get message next message to work on
&lt;/span&gt;        Message msg = queue.GetMessage();

        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (msg != &lt;span style="color: rgb(0,0,255)"&gt;null&lt;/span&gt;)
        {
            &lt;span style="color: rgb(0,128,0)"&gt;// process message
&lt;/span&gt;            &lt;span style="color: rgb(0,128,0)"&gt;// ...

&lt;/span&gt;        }
        &lt;span style="color: rgb(0,0,255)"&gt;else
&lt;/span&gt;        {
            &lt;span style="color: rgb(0,128,0)"&gt;// no messages waiting, so sleep for a while
&lt;/span&gt;            &lt;span style="color: rgb(43,145,175)"&gt;Thread&lt;/span&gt;.Sleep(DEFAULT_SLEEP_TIME);
        }
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There are a couple of things going on here:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;At the start of each loop we record the current time in a DateTime member variable. This is effectively a record of the last time the main loop was known to be in a healthy state.&lt;/li&gt;

  &lt;li&gt;If there are no messages waiting on the queue, we sleep - this ensures that we aren't constantly burning up our CPU looking for work when there isn't any. This reduces the power consumption of our app and will presumably reduce the cost of your azure deployment - I say presumably because any costing details are yet to be announced. &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now we come to write our GetHealthStatus status all we have to do is query the lastHealthyReport to ensure that the main thread reported in within an acceptable time.&lt;/p&gt;

&lt;pre class="code"&gt;    &lt;span style="color: rgb(0,0,255)"&gt;public&lt;/span&gt; &lt;span style="color: rgb(0,0,255)"&gt;override&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;RoleStatus&lt;/span&gt; GetHealthStatus()
    {
        &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (lastHealthyReport.AddSeconds(60) &amp;lt; &lt;span style="color: rgb(43,145,175)"&gt;DateTime&lt;/span&gt;.Now)
        {
            &lt;span style="color: rgb(43,145,175)"&gt;RoleManager&lt;/span&gt;.WriteToLog(&lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Error&amp;quot;&lt;/span&gt;, 
                &lt;span style="color: rgb(163,21,21)"&gt;&amp;quot;Node going unhealthy, not heard from main loop since &amp;quot;&lt;/span&gt; 
                + lastHealthyReport.ToUniversalTime().ToString());
            &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;RoleStatus&lt;/span&gt;.Unhealthy;
        }
        &lt;span style="color: rgb(0,0,255)"&gt;return&lt;/span&gt; &lt;span style="color: rgb(43,145,175)"&gt;RoleStatus&lt;/span&gt;.Healthy;
    }&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The above code assumes that each message should take no longer that 60 seconds to process. If it does take longer than that it will be reporting that the node is unhealthy; allowing Azure to tear down the node and build us another one. Oh, and logging the error in Universal Time (or Greenwich Mean Time if you are British ;-) means that it will be easier to look in any other log files to see what was happening at about the time the node went down.&lt;/p&gt;

&lt;p&gt;Neil.&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9184938" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Architecture/default.aspx">Architecture</category></item><item><title>Poison in Windows Azure</title><link>http://blogs.msdn.com/neilkidd/archive/2008/12/07/poison-in-windows-azure.aspx</link><pubDate>Sun, 07 Dec 2008 21:33:26 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:9182132</guid><dc:creator>neilk</dc:creator><slash:comments>3</slash:comments><comments>http://blogs.msdn.com/neilkidd/comments/9182132.aspx</comments><wfw:commentRss>http://blogs.msdn.com/neilkidd/commentrss.aspx?PostID=9182132</wfw:commentRss><description>&lt;p&gt;If you are developing a queue based system in Windows Azure - and lets face it, if you want a highly scalable and reliable application, you going to be using queue - you are going to have to deal with poison messages. A poison message is a message that your application logic can't deal with. For example, lets assume that we have just placed a message on a queue that contains some incorrectly formed data - hey these things happen in even the best designed and tested apps. When we come to read the message, the bad data causes our message parser to throw an exception and the message processor will die - hence the name poison message. &lt;/p&gt;  &lt;p&gt;In this situation, life actually gets a little worse in Windows Azure. Azure queues ensure that a message will be processed at least once. So, after our visibility timeout expires, the poison message will come back to life and get picked up by another of our processors - causing this to fall over as well. Eventually, with enough poison messages in the queue all our processing nodes will only ever get poison messages, fall over, restart, ... and we will stop doing any real work.&lt;/p&gt;  &lt;p&gt;Currently we can't find out how many times a message has been read of a queue, so the only way to check for a poison message is to see what time it was placed on the queue. If the message has been on the queue for 30 mins and your visibility timeout is 60 seconds, it is probably poisonous. Ok, so this doesn't allow for periods of down time longer than 30 mins, but it is moving us in the right direction.&lt;/p&gt;  &lt;p&gt;What to do with a poison message is probably a trickier issue. A generic solution would be to write the message to a poison message table for later manual inspection.&lt;/p&gt;  &lt;p&gt;Code to do this is going to look like:&lt;/p&gt;  &lt;pre class="code"&gt;    &lt;span style="color: rgb(0,128,0)"&gt;// Get message with 60 sec visibility
&lt;/span&gt;    Message msg = queue.GetMessage(60);

    &lt;span style="color: rgb(0,128,0)"&gt;// Poison check
&lt;/span&gt;    &lt;span style="color: rgb(0,0,255)"&gt;if&lt;/span&gt; (msg.InsertionTime.AddMinutes(30) &amp;lt; DateTime.Now)
    {
      &lt;span style="color: rgb(0,128,0)"&gt;// Treat as poisonous - but don't look at in the message!
&lt;/span&gt;      &lt;span style="color: rgb(0,0,255)"&gt;byte&lt;/span&gt; [] msgBody = msg.ContentAsBytes();
      PoisonMessageTable.Insert(msgBody);
      
      &lt;span style="color: rgb(0,128,0)"&gt;// Stop it repeating on us
&lt;/span&gt;      queue.DeleteMessage (msg);
    } &lt;span style="color: rgb(0,0,255)"&gt;else&lt;/span&gt; {
      &lt;span style="color: rgb(0,128,0)"&gt;// process normally
&lt;/span&gt;      &lt;span style="color: rgb(0,128,0)"&gt;// ...
&lt;/span&gt;    }&lt;/pre&gt;

&lt;p&gt;If you aren't interested in seeing the poison message, you can achieve this more easily by setting the message expiration time to 30 mins. This means that Azure will delete the message after it has been on the queue for 30 min; you have kept your system up and running, but you have lost what ever was in the message and the opportunity of fixing the bug that is causing the problem.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Neil&lt;/em&gt;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=9182132" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Azure/default.aspx">Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Windows+Azure/default.aspx">Windows Azure</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Cloud/default.aspx">Cloud</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Tips/default.aspx">Tips</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Architecture/default.aspx">Architecture</category><category domain="http://blogs.msdn.com/neilkidd/archive/tags/Code+Snippets/default.aspx">Code Snippets</category></item></channel></rss>