Category Archives: Getting started

About Windows Azure

Windows Azure is Microsoft’s application platform for the public cloud. This post describes the most important basic principles you must know beforehand.

Execution models

The main thing that we do, as developers, is to write applications. Eventually, this code has to be executed. Windows Azure offers a platform that you can use to execute your applications, by means of three execution models:

  • Virtual machines: it is possible to create virtual machines on demand and run your applications in them – this is also referred to as Infrastructure as a Service (IaaS). These machines are hosted for you in the cloud; which means they will always be accessible by you from anywhere, and you have total control over them. There are several images you can use, from Windows Server 2008 R2 to Linux.
  • Web sites: if you have a web application (which may be static HTML, or an open source application like Drupal, WordPress, Joomla, or written in PHP, ASP.NET…), this model is most likely the one you need. It gives you a managed web environment in IIS to host your web application. Most of the administration is done for you, so that you can focus on what’s important: keep your web application up and running. As traffic increases, you can add more instances that are then load balanced.
  • Cloud services: also known as Software as a Service (SaaS), this model gives you the flexibility to install arbitrary software (something that the web sites model doesn’t offer), without having to handle many aspects of reliability and administration yourself (as you have to do with the virtual machines model) – Windows Azure itself manages all of these things. In this model, you can choose between two roles: web and worker. The main difference between the two is that an instance of a web role runs IIS, while an instance of a worker role does not.

Which model you need depends on the problems you are trying to solve:

  • If you need the most general solution with total control, use the virtual machines model; but remember that you are responsible for its administration.
  • If you have a simple web site with little administration, use the web sites model.
  • If you need more administrative access; have a multi-tier application with different layers for business and data logic; need staging and production environments; need to use networking technologies like virtual network, connect or traffic manager; or use remote desktop to connect to the virtual machine, you should use the cloud services model.

If no single execution model is right, you could even combine models.

As you might notice, it seems there is not a lot of difference between the web sites model and the cloud services model with web role. The main difference is scalability: the web sites model is limited in the amount of instances you can set up.

Data management

In most cases the applications we write need to store data. Of course we could set up a vitual machine and provide everything needed for data access. Of course, this means that we also have to manage all of it ourselves.

To make life easier, Windows Azure provides three data management options that are largely managed for you:

  • SQL database: if you need relational storage, with transactions, and concurrent data access by multiple users with data integrity, this is the option to choose. You can use tools like SQL Server Management Studio to manage your data, and use your favorite data access technologies like Entity Framework.
  • Tables: use this if you need fast access with simple queries to potentially large amounts of typed data. This basically is a key/value store.
  • Blobs: use this if you need to store unstructured and large binary files.

Networking

To host your applications and store your data, Windows Azure runs in several datacenters spread across the United States, Europe, and Asia. Somehow you have to choose what data centers you want to use and how to connect to it:

  • Virtual network: if you already have your own data center, you could extend it with Azure data centers using a virtual network, so that the public cloud is like an extension of your own datacenter – and appears to be on your own network. You could then easily add or remove virtual machines, depending on the power you need.
  • Connect: use this if you want to connect a Windows Azure application to the existing infrastructure of your organization, for example, use an existing database that is on one of your own servers.
  • Traffic Manager: if your application is used by users all over the world, you can use connect to intelligently assign users to application instances – being the ones most close to them so that response times are low, or if this data center is overloaded, automatically route the request to the second best one.

Messaging

If you have a number of applications running, you typically need a way so that these applications can interact with each other. For this, Windows Azure provides:

  • Queues: typically used to handle communication within the same Cloud Services application: one application places a message in a queue, and that message is eventually read by another application. A typical usage is a web application (configured as a cloud service web role) that sends a message to a component (configured as a cloud service worker role), so that this component can execute a task asynchronously. This makes it easy to scale, as you can change the number of web and worker roles as is needed.
  • Service bus: the goal of Windows Azure Service Bus is to let applications running pretty much anywhere (in and outside the cloud) exchange data. This is not the same as queuing, because it provides a publish-and-subscribe mechanism: applications subscribe to certain events they are interested in, which means it’s more like a one-to-many communication. This approach encourages loosely coupled interactions between applications.

Caching

In most cases applications tend to need the same data over and over, and so this is where caching is important, because it holds a copy of that data closer to your application instead of having to retrieve it every time. Windows Azure provides:

  • In-Memory Caching: accessing frequently used data stored in memory is very fast, and so this caching provides you with an in-memory cache to speed up your application. This cache can be distributed.
  • CDN: Content Delivery Network is a caching mechanism that you can use if your users are spread all over the world and you have large blobs that need to be cached. In fact, they are replicated to other data centers.

Installing and using nuget

In Visual Studio, go to Tools / Extension manager. Select the Online Gallery, and type in ‘nuget’ in the search box:

nuget0

Click the Download button, and install Nuget Package Manager:

nuget1

nuget2

nuget3

You need to restart VIsual Studio now. After that, if you click on a solution, you can enable Nuget Package Restore:

nuget4

You will get the following message:

nuget5

If you click the Yes button, the Nuget Package Restore will make sure that if you build your application, it will automatically download all required packages needed by the solution:

nuget6

nuget7

Afterwards, a new solution folder ‘.nuget’ is created:

nuget8

To add a nuget package, for example the Specflow nuget, right click the solution and select ‘Manage NuGet Packages for Solution…’:

nuget9

Select ‘Online’, type in the nuget name and click on the Install button next to the nuget you want to install:

nuget10

nuget11

nuget12

If you then look in the references of your project, you will see that the assembly has been added:

nuget13

Now everyone will be kept up to date with the correct nugets and versions with every build of the solution.

Installing windows 8 in a virtual machine

Installing windows 8 in a virtual machine is quite straightforward, but before you start, make sure that in your bios, the no-execute memory protection setting is enabled. If it isn’t enabled, you will most likely get the following error at the beginning of the installation:

w8_1

So go to your bios and make sure it’s enabled before you continue!

To create a virtual machine, I use VMWare player:

w8_2

Click ‘Create a New Virtual Machine’, and select the source where windows 8 installation files are located. In my case it’s an iso file:

w8_3

Click next.

w8_4

Enter your name and choose a password. Check ‘Log on automatically’ and click next.

w8_5

Enter a name and location for the virtual machine, and click next.

w8_6

You can accept the default here and click next.

w8_7

Click finish. The installation of windows 8 will now begin. Unfortunately, I got the following error:

w8_8

This seems to be a know problem, which can be solved by turning off the floppy drive. To do that, power off the machine, and edit the virtual machine settings so that ‘connect at power on’ is not checked anymore:

w8_9

Click ok, and run the virtual machine again. This time, the installation will start properly. The rest of the installation is easy:

w8_10

w8_11

w8_12

w8_13

w8_14

w8_15

w8_16

w8_17

w8_18

w8_19

w8_20

w8_21

Don’t forget to re-enable the floppy drive, otherwise you will not be able to install VMWare tools. But make sure you check ‘use physical drive’:

w8_22

Restart the virtual machine, and then choose Manage/Install VMWare Tools to start the installation:

w8_23

You will get the following popup:

w8_24

Click the popup, then you will get:

w8_25

Now run setup64.exe and follow the wizard to install VMWare Tools. After installation, restart the machine and that’s it, now it’s time to play around Smile

Note: after restarting, it seems something went wrong because every time the machine restarted, I got the error ‘This Virtual Machine Appears to be in Use’ followed by some instructions to try and take ownership of the system. But that didn’t work. The solution is to delete all folders with the extension of ‘.lck’ in the folder of your virtual machine. After that, everything works again.

Quickstart memcached

We have a legacy application of about ten years old, which uses a database for caching. Each time a request came in, the database was queried to look for that request with the specific input parameters, and if found, the cached version of the data was taken. This worked fine, except when there was a lot of traffic, where it became a bottleneck.

To remove this bottleneck, we needed another way of caching; and so we decided to replace it with memcached. This article describes my first test with setting up and using memcached to see how it works.

Referring to the definition “Free & open source, high-performance, distributed memory object caching system, generic in nature, but intended for use in speeding up dynamic web applications by alleviating database load.”, we can say that memcached is like a short-term memory for your data. You can store simple and more complex objects in it, which makes your web site respond a lot faster than when it would have to get that data from a database each time. You can specify the amount of memory it can use – which means that if the memory is full, older data gets purged in a least recently used order.

It’s important to understand that servers are disconnected from each other, which means that if you have more than one server, each server is unaware of the others, so they are not synchronized. As such, the effect of adding more servers is that you will simply get more capacity. This means the client must have the intelligence to decide what server to use (Hashing/Key Distribution), as each of your data will be stored only once on exactly one of the memcached servers.

Memcached server

First we need to install memcached on the server. On the project site there are no packages, only sources; but you can download a windows package from here. I took the memcached 1.4.5 win64.zip version, which contains 2 files after you unzip it:

mc1

So copy these files somewhere on the server. In my case I used my development computer itself as the server.

Now you have to options: run it as a console application or as a service. Running it as a console application is fine if you are doing R&D, but for real development you should install it as a service of course.

It seems that the latest version cannot be run as a service anymore, it should be wrapped in something else to make it run as a service. Anyway, for my test, I’m just going to run it by starting the memcached executable with some parameters. So run the elevated command prompt (run as administrator), go to the location where you copied memcached and start it as follows:

memcached -p 12345 -m 64 –vv [ENTER]

where

  • p 12345 specifies the portnumber the memcached instance will listen to
  • m 64 specifies the amount of RAM in Mb that memcached will use for storage
  • vv controls verbosity (the amount of information that is logged)

The result will be a running memcached instance:

mc2

To test this, open a new command prompt, and type:

telnet 127.0.0.1 12345 [ENTER]

You will see an empty prompt:

mc3

Now type:

set mymessage 1 0 11 [ENTER]
Hello world [ENTER]

With this command, we have added a new key mymessage, with 1 as flag (arbitrary metadata), with 0 as the expiry (never expires), and with value which is 11 bytes long. If it succeeds, STORED will be displayed:

mc4

To get it back, type:

get mymessage [ENTER]

And you will get back the value again:

mc5

This proves that the memcached instance is running and working properly.

Memcached client

In order to make use of memcached server, we need a .NET client API that allows us to easily access the memcached server’s functionality. It seems that the enyim client is one of the most popular ones with a rich feature set, so I decided to go with this one. Just go to the downloads section, get the latest one (at this time that’s Enyim.Caching.2.9.zip) and unzip it. You should have this:

mc6

Copy this to your references or libraries folder, and from your visual studio project add a reference to Enyim.Caching.dll:

mc7

The next thing to do is to add a global MemcacheClient instance to you application. From the documentation:

Important! MemcachedClient is a “heavy object”, as in creating and initializing the client is quite expensive. (It has a socket pool inside, needs to calculate the tables for the consistent hashing, etc.) So, DO NOT create a client every time you want to perform an operation (well, you can, but 1) you need to Dispose() it after you finished, and 2) this will slow down your application considerably” 

So best practice is to initialize the instance only once, for example in a web application you would typically do this in the Application_Start method of global.asax; but we’re using a simple console application, so:

using Enyim.Caching;
using Enyim.Caching.Memcached;

namespace ConsoleApplication6
{
class Program
{
static void Main(string[] args)
{
using (var client = new MemcachedClient())
{
// Store message
client.Store(StoreMode.Set, "mymessage", "Hello world");

// Get message back from cache
var message = client.Get<string>("mymessage");
}
}
}
}

Before we can use the client, we have to configure it, so extend your application’s config file with:

<?xml version="1.0"?>
<configuration>

<configSections>
<sectionGroup name="enyim.com">
<section name="memcached"
type="Enyim.Caching.Configuration.MemcachedClientSection,
Enyim.Caching"
/>
</sectionGroup>
</configSections>

<enyim.com>
<memcached protocol="Binary">
<servers>
<add address="127.0.0.1" port="12345"/>
</servers>
</memcached>
</enyim.com>

</configuration>

If you build the application, you may get two build errors:

mc8

When you have this, it means probably that the target framework of your project is wrong. To fix it, right click your project and select properties; and make sure .NET Framework 4 is selected:

mc9

Build again, debug it and check whether message contains the correct value:

mc10

This proved that memcached is quite easy to setup.

What about the key?

In the previous example the key was short and simple. But in reality it could happen that keys are big – too big. Memcached allows keys of a maximum size of 250 characters (for a reason). In the case your keys are too big, you are probably better of generating a hash (which is unique) and use that hash as the key. Here’s some code that does that for you:

/// <summary>
/// Calculte the SHA1 hash of the given text.
/// </summary>
/// <param name="text">The text to calculate the hash for.</param>
/// <returns>Hash of specified text.</returns>
private static string HashString(string text)
{
string hash;
using (var sha1 = new SHA1CryptoServiceProvider())
{
var encoder = new UTF8Encoding();
hash = Convert.ToBase64String(sha1.ComputeHash(encoder.GetBytes(text)));
}
return hash;
}

Castle MicroKernel/Windsor QuickStart 2: LifeStyle and LifeCycle

…continued from part 1.

Objectives

In this Hands-On Lab, you will be introduced to the Castle MicroKernel/Windsor framework. In particular, you will:
  • Understand LifeStyle
  • Understand LifeCycle

System requirements

You must have the following items to complete this lab:
  • Microsoft Visual Studio 2008 SP1 (professional edition)

Prequisites

You must have the following skills to understand this lab:
  • Fundamental knowledge of software development in .NET 3.5
  • Basic knowledge of Castle

Understand LifeStyle

LifeStyle is a strategy that is related to an instance of a component. The supported lifestyles are:

  • Singleton: only one instance will be created for the whole life of the container. This is the default lifestyle.
  • Transient: for each request a new instance if the component is created
  • PerThread: for each thread there exists only one instance of a component
  • Pooled: Instances will be pooled to avoid unnecessary constructions
  • Custom: the instantiation of components is handled by your custom ILifestyleManager implementation

The default lifestyle is singleton. To indicate which lifestyle you want to use, you can use attributes to decorate the class:

[Transient]
public class CustomerRepository : ICustomerRepository
{

}

or in the configuration file:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section
name="castle"
type="Castle.Windsor.Configuration.AppDomain.CastleSectionHandler,
Castle.Windsor"
/>
</configSections>
<castle>
<components>
<component
id="customer.business"
service="CastleExample.ICustomerBusiness, CastleExample"
lifestyle="transient"
type="CastleExample.CustomerBusiness, CastleExample" />
<component
id="customer.repository"
service="CastleExample.ICustomerRepository, CastleExample"
lifestyle="transient"
type="CastleExample.MockCustomerRepository, CastleExample" />
</components>
</castle>
</configuration>

I prefer to do it in configuration because otherwise the code will be coupled again to the Castle container, or, in complicated projects, it may be handier to do it in code but in that case, create specific classes that are responsible for these configuration tasks, so that in your code you have as little as dependencies to Castle as possible.

So try this: add a transient lifestyle to the components in the configuration file, and to see the effect, modify the constructor of CustomerBusiness and add constructors to CustomerRepository and MockCustomerRepository as follows:

public class CustomerBusiness : ICustomerBusiness
{
private ICustomerRepository customerRepository;
public CustomerBusiness(ICustomerRepository customerRepository)
{
// using constructor injection the concrete customer repository is injected
this.customerRepository = customerRepository;
Console.WriteLine("CustomerBusiness constructor");
}

}

public class CustomerRepository : ICustomerRepository
{
public CustomerRepository()
{
Console.WriteLine("MockCustomerRepository constructor");
}

}

public class MockCustomerRepository : ICustomerRepository
{
public MockCustomerRepository()
{
Console.WriteLine("MockCustomerRepository constructor");
}

}

Modify Program.cs:

static void Main(string[] args)
{

// Use business
UseRegisteredBusiness();
UseRegisteredBusiness();
UseRegisteredBusiness();
}

If you run the application, you will notice that each constructor is only called once and so there is only one instance created of the components for all calls:

Change the configuration that components use the transient lifestyle, and then run the application again to see the difference:

Now for each call of the business component, the constructors are called, which means that new objects are created for each call.

Understand LifeCycle

On the other hand there is LifeCycle, which is a strategy that is related to the different phases of an instance. It means that we can implement a number of interfaces; and these implementations are executed at the right time. Out of the box there are two groups of lifecycles:

  • Commission: these are executed when the component is being created. Interfaces are:
    • Castle.Model.IInitializable
    • System.ComponentModel.ISupportInitialize
  • Decommission: these are executed when the component is being destructed. Interface is:
    • System.IDisposable

So, bottom line: if you implement one of these interfaces for your component, Castle will take care of calling your implementations at the right time. Notice that if you use these, the coupling to Castle container will be higher.