Hosting a Silverlight app in Azure

A quick introduction to how you can get up and running with Microsoft Azure – It is a hands-on guide into creating a silverlight application that uses a REST api to manage SQL data in the cloud.

Who should read this:

This article assumes that:

  • You know (and love!!) the SOLID programming principles
  • You know what WCF is and how to host and consume such services through IIS
  • You have some knowledge of the Entity Framework ORM
  • You want to get something out on windows Azure, but you’re not quite sure how to

 

The concept

I am writing an inventorizer application. The idea is to keep track of my movies and to know where in my house they are supposed to be. This way, I know where to put a stray movie, as well as check that all movies that are supposed to be in a specific shelf actually are there.

Later, I will extend the application to access my movie list from mobile devices, so it’s going to require a REST api right from the start.

Entities and storage

To get started, I defined 3 basic entities for my application:

Entity Detail
Location Room / Area in my home
Storage Shelf, drawer, box, etc. Exists inside a Location
Movie Stored inside a piece of storage

 

Using Entity Framework, I started by creating a model from a blank database:

image

I’ve explicitly given the entities the prefix “Db” in order to separate the objects from my C# domain objects.  Automapper does the conversion for me – pretty straightforward. I keep my domain objects clean, and clear of the Sql Server, as you should too.

SQL Azure

To work with SQL Azure, you need to have a valid Azure account and you also need to have created a database for the purpose. I won’t go into the details of the database creation process; basically, you follow a database creation wizard that does what you expect it to.

Once created, you want to connect your Visual Studio Server explorer to this newly created database. To do that, you first allow yourself through SQL Azures firewall, which is fairly simple, flip to the Firewall Settings tab and click on the button “Add Rule” which brings up this:

 image

Complete the firewall rule by setting your IP number then click OK and flip back to the databases tab to get a connection string:

imageThe connection string does not have your password in it. You’ll have to edit that in after you put it in your settings file. If you need help in pushing your model to Azure SQL, just drop me a line, and I’ll help you out.

Setting up the REST service

Setting up the REST service is a mattter of

  1. Defining your service interface
  2. Implementing the service in some class
  3. Setting up the service endpoint configuration in your service configuration file

Important note:
In order to implement REST and use WebGet and such, you need to include a reference to System.ServiceModel.Web. Make sure in your project properties that you’ve selected the full .Net Framework 4.0 and not the .Net Framework 4.0 Client profile as your target framework, or System.ServiceModel.Web won’t be visible for you to reference.

Defining the service interface

Not much hassle here, the special consideration is the REST way of making the endpoints accessible:

image

Implementing the service

Since we started with the EF model, implementing the service simply means creating a repository interface (for convenience) and then implementing it with the generated context class

image

Setting up the service endpoint configuration

To roll out a successful REST service that serves both POX (plain old xml) and JSON data, I had to actually create two different binding configurations even though they’re equal in configuration.

image
Second, set up a couple of behaviors, differenciating only in the default response format:

image
Finally, set up the endpoints you need:
image

Since we are hosting this in Azure, we do not specify any addresses.

Creating the client

Now that both the database and REST API is up and running, you only need to create a regular silverlight client, point it to the service, and you’re in business. I actually created a SOAP endpoint in addition to the POX and JSON addresses since I do not need to box data between .Net clients, thus my Silverlight client config has the following service reference:
image
Notice the relative address, since I’m hositing the Silverlight client from the same location as the service, I use the relative address to avoid cross-domain issues. This took me some time to figure out. I usually start out with a basicHttpBinding and then swap over to TCP/IP once everything is up and ok.

If you need more details on how to write a silverlight client, just drop me a message.

Azure considerations

So, having completed, tested, and debugged the project here on earth, it was time to deploy the pacakge to Azure. There was one last remaining thing to do, and that is to put a checkmark on your Sql Azure configuration screen in order to allow your services to connect to the database:

image
This is definetely another one of those “easy to forget, hard to figure out” things…

Integration Testing

I wanted to have a set of integration tests that directly referenced the SQL Azure database without destroying any data, so I opted for the transaction approach where you basically begin a transaction before each test, and then roll back all changes after running it. This led me to the following base class:

image

The base class basically implements the TestInitialize and TestCleanup methods to begin a transaction before each test, and roll it back (Dispose()) after each test has run. Any test that throws an exception will then automatically roll back the database.

TIP:
If you use the TestInitialize or TestCleanup in a base class, your derived test class won’t be able to use those attributes. This is why I added the virtual Given() function so that I can do my test setup there, should I need to.

An example of use:
image

The testclass above creates an instance of the class StorageRepositorySql and the test that is run is then packaged inside a transaction scope and rolled back so to not disturb my SQL server data. If you want more details on the base class, just let me know.

Running these tests is surprisingly fast, on my 2Mbit internet line, most of my tests run for less than 50ms each, which is pretty amazing, considering the transactions and that I’m in Norway while the Azure store probably is in Ireland!

Conclusion

Microsoft promises that “going Azure” should be pretty straightforward, and not much different from what you’re already used to. I tend to agree, it has been surprisingly easy to get something up there and running. Most of the challenges were actually in configuring the REST endpoints and figuring out how to allow the WCF services to access the SQL database, but other than that, the rest is straightforward.

At the end of this article, I’ve prepared a short Silverlight application that simply lists the locations in my SQL Server. It should be available through the following URL:

http://digitaldias.cloudapp.net

However, since this is work in progress, you may see a more advanced thing on this page as my application progresses, or something completely different, or, perhaps nothing at all – I make no guarantees, other than that it should be there if this article isn’t too old Smile 

P.

And then, cold came!

DSC_0054

Woke up this morning with the sense of my bed being unusually cozy.. Leaving it did not seem quite the thing to do.

I got me one of those cheap inside/outside termometers, a few years ago, so I tippy-toed downstairs to have  a quick glance at it, and sure, it said 16 negative degrees (That’s 3.2F for the yanks) outside, but worse, inside we had no more than Just over 17C (62.6F). Who unplugged the global heater??

17 Degrees inside, man, ,that’s no cool at all, it’s enough to make you shiver unless you pack a double set of everything; double socks, double sweaters, speak twice as fast to have the friction warm you up a little.

And look at my car (you can click on the images btw)!! DSC_0058

The poor thing, we’re going to IKEA today to get another shelf for the living room, and this  is what greets us!

So I had to go outside, in my jammies, hook up the car with the engine heater, run back in again and defrost my family jewels. And while I wait for the thing to thaw, I snapped a few photos for you to see with my brand new Nikon D700 and 24/70mm 🙂

Happy holidays, all!

Painful correctness

Catching up

Back to work again to my new employer, this time (back) in the role as an IT consultant and a senior one at that. It is refreshing to work in an environment that is constantly challenging my knowledge about various technologies. Now if I only could figure out how to get used to having a laptop computer with me back and forth every single day and I’ll be one happy camper..

Away, fatness!

Managing to work out in the morning without sweating down my clothes afterward is a challenge. As spinning classes now are back to their normal 3-day per week schedule, I need to figure out how to stop sweating after the shower!

Look sharp! Look dry!

A thing that is bugging me, is that it really isn’t easy bringing a gym bag, a laptop backpack AND dress nicely all on top of my mountain bike on the way to the train station. This is getting to the point where I am seriously considering taking the car to the train station just to avoid the hassle. Clearly, being environmentally aware is not compatible with being a commuter – not in the consultant business!

Rough waters

The basement is now dry after the flood now and ready for a fresh coat of paint. We still haven’t moved anything down there, as we are hoping to do something with the roof while we’re going to paint/lay new flooring there.

Chip! chip! chip! chip!

Photography has been dead the last month or so. With the new job taking up all my available brain power, and Stargate SG-1 taking up the spare time, I am saving most of my creative energy for work, but I still have managed to push forward a project for controlling my Pre-Amplifier. Expect a dedicated home page soon. I had a similar project far too many years ago, written in C++ using MFC and virtually no knowledge about agile software development. The new version is being written in C# using a 3-tiered model, complete with a separate unit testing project, installer, user documentation, and scrum as the project management methodology. The thing is practically writing itself!

Tell me about myself!

Oh, I woke up this morning thinking that I have really never written an “about-me” page in spite of having had a home page on the internet since around 1995! Of course, you cannot wake up a Sunday morning thinking a thing like that without immediately scratching your groin, jumping out of bed, and writing one, so.. I did, and here is the result.

Changes

What do you do when your first day of the holiday is here, but your car is still at the workshop, preventing you from taking off to a place where you can relax and charge your batteries?

First, you get really annoyed, of course. You call the workshop, and tell them what they don’t want to hear. Then, you retreat home, and grab a good movie, such as The Brave One, starring my all time favorite actress, Jodie Foster. The morale of the movie is bad, but hey, it’s Jodie!

The very next day, you change the look of your web page and update its statistics engine, of course, leaving you happy again, and life again, smiles!

You might as well drive, looser!

About commuting
A large portion of the rush hour traffic has to be attributed to people living outside of town in suburbs. That’s natural for any major city, as I would like to think that most of us appreciate clean air and quiet when we’re not at work (I guess I’m a freak, I appreciate those two AT work as well, heh)

Anyways, in these panicky days of environmental suspense, everyone says “Take the train” or “take the bus”… (children, look away now, this is going to get ugly) – What retard actually believes that this is going to help? How stupid can you be, if you believe that those of us living outside of the city centers are willing to leave the car when

  • There’s no (safe) parking to accommodate every commuter. The habitual morning “fight” to get to work starts right there, constant fear of not finding a spot to park your car is frustrating. For those living in the northern parts of the world, such as myself, parking your car outside means you’ll have to scrape off snow and ice when you get home, tired from work.
  • Never mind frequent departures of trains and buses, when you commute, you regularly fight the same people for a chance of getting a seat. You recognize all of the people at the station, they’re the same every day, they commute just like you, yet, they’re enemies, looking to take that seat you so sorely wanted…
  • Even when you occasionally win a seat, the stench of far too many people crammed into that train or bus, just makes your breakfast want to jump off at the next stop. It disgusts me how train companies can cut costs so far. And when the morning is bad, you know that the evening is going to be horrible.

There are only two options for people today, you can live with it, and be a complete looser, or you can take the car, and be an environmental jackass. Either way, you loose, and either way, you’re not to blame.

Let me dream a little:

Give me somewhere near the train station to park my car that is under a roof. Keep backup train wagons and extra buses and use them dynamically so that everyone gets a seat. Make me want to take the train to work, and I will.

Choking our economy by raising the prices of gas, upping the cost of ownership for cars, and placing toll charges all over may reduce the number of people that can afford to have a car, but you’re really only hurting those that are trying to raise children in a healthier environment, away from the city centers. They depend on cars, and are usually not the richest people on the planet.

There ya have it, my two cents on commuting.