Create Azure Tables at startup

You know that thing you need to do before accessing your azure table for the first time? Yes, I’m looking at you CreateTableIfNotExist(string tableName). You need to do this before persisting you first entity or writing you first query.

This annoys me. Mostly because I tend to forget about it and then get reminded by an exception. So here’s what I did in order to never ever run into the same issue again (right…).

Reflection?

Yes, reflection! As I usually organize my Azure projects I have a folder called Entities where I have all… well entities that I can read and write from/to Azure Table Storage. Illustrated below.

projectstructure

To be honest, I would probably put the entities in a separate project, but this will do as an example.

So, the idea is, if I know the namespace where I always put my entities, then I should also be able to find all my entities by looking into that namespace using reflection.

But reflection is quite bad for performance…

Yes… yes, it is. Therefore I prefer only using it when I need it and when my application won’t suffer from it. For example – at startup.

Now, before proceeding I recommend that you take a look at a previous blog post – Making the most out of your WorkerRoles. What I did there was basically to create worker that implemented IWorker and then using an IoC container I looped over all implementations in the Run method. We will do the same thing here, but instead of an IWorker we will create an IStartupTask.

Now we can get started. What we’ll need to do is:

  • Find a way to programmatically find all Entities
  • Implement IStartupTask
  • Make sure that all (one at the moment) startup tasks are executed in the OnStart method of the WorkerRole

Finding the entities

So reflection is not one of my strongest .NET skills. Luckily, others such as Fredrik Mörk are more competent in the area. So I used his solution.

Implement IStartupTask

Here we go.

Neat.

Execute the task in the OnStart method

Same procedure as in the previous blog post I mentioned above. We will just loop through all instances of IStartupTask, that is registered in our IoC container, and then start them.

That’s it really.

What happens now?

Basically once our WorkerRoles starts we will make sure that all of our Azure tables exists. If not, we will create them. No more exceptions telling us that the table does not exist. Ever.

worker

Each and every time we add a new entity it will automatically get sucked in by our startup task and then create the table. You could use a similar approach to Azure Queues if you for example serialize objects onto the queue.

But now when you have a nice infrastructure for startup task you can use them for so much more. We (that is Jayway), for example, use them to set up a socket that another server use to push in data. The sky’s is the limit.

This Post Has One Comment

  1. I simply love your illustrations… :D
    Haha, the WorkerRole ensures the table exists… Brilliant!

Leave a Reply

Close Menu