Posts

Azure queue with strongly typed messages

I've been reading a lot about cloud patterns recently (e.g.  from microsoft ) and one thing I've really wanted to do is move background processing to a worker role. To get started I decided to move our recurring jobs that we were running in the web role to a worker role because there are  some limitations  that are hard to avoid when running background work in ASP.NET. The idea was simple - set up an Azure scheduler to put items on an Azure Storage Queue and have a worker role polling the queue and reacting to the messages. For simplicity, I created a simple wrapper for the queue messages  public class CustomMessage { public string TypeName { get ; set ; } public string Payload { get ; set ; } } Then when I queue a message I serialize an object to JSON with JSON.Net and create an instance of the CustomMessage class to put on the Queue. I actually have a service which maps from the CustomMessage to a CloudQueueMessage and vice vers...

Azure SQL Server and Collations

Image
This post is more of a warning to other people than anything particularly ground breaking. Please, please, please check the collation you're using when you create a new database on SQL Azure. Choosing the wrong one can be a major annoyance and a source of subtle bugs. On one of our databases we have a default collation of 'Latin1_General_CP1_CI_AS' but it was migrated from a server with a collation of 'Latin1_General_CI_AS' - so most of the columns in the tables had that collation. This may not seem like much of a problem but we have had a few bugs due to it: Entity Framework - collation conflicts (not sure if still an issue), silent failures... Foreign key problems when adding new tables etc. In order to make sure we don't have any conflicting collations lying around I wrote a simple script: select '[' + t . name + '].[' + c . name + ']' as ColumnName , c . collation_name as Collation_Name from ...

Sharepoint 2010 deploy script

I used to work with SharePoint and it gave me some of the most frustrating days of my life. On the plus side, I've been complimented on my "tenacious" approach to problem solving that I'm fairly sure I can give SharePoint the credit for creating. One of the most annoying things I found about SP was deploying packages. I used to hand off a .wsp to the infrastructure team who would valiantly go off and deploy it. As soon as something went wrong, they'd just come back and ask for help - they had enough to deal with without having to learn SharePoint's nuances. In order to help them, I created a simple script that was mostly fire and forget. The code is available as a  gist  it's only designed to work one feature at a time but I think that should be easy enough to modify. ############################################################################################# # This script deploys a feature to a SharePoint 2010 farm. ...

The woes of coding by assumption

In a project we have a scheduler type service that polls an Azure Queue for work and does it, e.g. sending some emails. I've been confused about our number of storage transactions ever since I joined the project. It was at least 100 times more than my back of an envelope calculation estimated there should be. I installed Application Insights to try it out and, after I published, it informed me that one of the apps (the scheduler) was sending 250+ requests per second to Azure Queues. What the hell right? I took at look at the code and it was the sort of thing I was expecting: void Start() { _timer = new Timer(PollQueue, null , _queuePollingInterval, Timeout.Infinite); } void PollQueue( object state) { // check the queue for new work and do it. if (_timer != null ) _timer.Change(_queuePollingInterval, Timeout.Inifinte); } All looks good right? The entry point called Start() which in turn sets up a timer to called PollQueue(null) after the specified _queuePo...

Exporting data from SQL Server by generating an insert script

Image
I was moving some data between a couple of databases the other day (just data from a couple of the tables). Basically, I'd created a new feature, tested it a bit which had generated some entries in a new database table and I wanted to move these to the test system. I was about to do my normal "right click source database -> tasks -> export data..." dance when a colleague mentioned that I could just generate an insert script for the data to either a file, my clipboard or a new query editor window. I couldn't believe I hadn't found this gem before... Although it is reasonably hidden! I gave him the Control+Shift+R short cut for refreshing SSMS intellisense local cache so I guess we're even now. Anyway, the steps: Open up SQL Server Management Studio Expand the databases node in the Object Explorer Right click the database that you want to generate a script for and go to Tasks -> Generate Scripts   Database node context menu ...

Common pitfall with async in C#

Just a quick rant today... :) I love how easy it is to write asynchronous code with Tasks and the async and await keywords in C#. One problem that I keep running into is this kind of thing in code examples on line. public static T Value<T>( this HttpContent content) { var task = content.ReadAsAsync<T>(); task.Wait(); return task.Result; } Now there's a couple of problems with the above code. Whoever wrote it obviously thought it was going to do something magic, after all - they've chosen to use the  ReadAsAsync<T>()  method! Unfortunately, the very next line calls Wait()  on the returned  Task . So the  Thread  that could have been doing other work while the content was being read is just blocking anyway. I.e. there is no benefit at all to using the Async code. There's also the fact that accessing  task.Result  would have blocked anyway meaning the  Wait()  was pointless. This cod...

Creating a simple factory for objects with a fluent configuration API

Image
When I recently came to writing a new component for a system I thought, "I love using fluent configuration in other libraries so I'm going to write this with a fluent configuration API!". Of course, as with most projects, I made a lot of mistakes and had to keep going back and rewriting it from the ground up. I also wasn't able to find a lot of examples/discussion on creating fluent APIs for configuration so I decided to write a simple example that isn't really useful in itself but is useful as a learning exercise (at least it was for me!) I decided to put the full source on Github  rather than uploading a zip as I find downloading examples as a zip annoying! The whole idea for the example was to have a simple way of creating a factory object that is able to create objects of a certain type according to some configured rules. To make life easier, I added a constraint to only allow objects with a parameterless constructor. Or to put it another way: ...