Creating My First Windows Azure AppFabric Application

Microsoft recently made available the Windows Azure AppFabric June CTP to get started developing Windows Azure AppFabric applications.  Read the announcement post from the AppFabric Team for a good overview of the Azure AppFabric application model.  Be sure to download the CTP SDK too.  You can also get a FREE 30-day Windows Azure pass at http://bit.ly/MikeOnAzure, using the promo code “MikeOnAzure”.

Once you have the SDK installed, you’ll want to get started creating your first Azure AppFabric application.  Launch Visual Studio 2010 and create a new project.  In the New Project Template wizard, you’ll see a new “AppFabric” section.  You might have expected the Azure AppFabric projects to be located under the “Cloud” area, as that is where the other Azure project templates are.  I did too.  My assumption here is that this is CTP and the location of the templates are likely to change before final, or that Microsoft is looking to further merge the AppFabric’s (Windows Server and Windows Azure), thus making the choice a deployment target and not a specific type of project.

For the purposes of this walk-through, I’m going to create a new “AppFabric Application”.  I’ll pick a cool name for my project and let Visual Studio create it.  This will create an empty AppFabric Application in which I can start to add my services/components.  There are a few oddities with the new project, and I will chalk this up to being an early CTP.  The first is that there is no visual indication that the project is an “AppFabric” application.  For instance, the project icon doesn’t contain the “AppFabric” icon overlay that is on the New Project template selection.  The second thing I find odd is the “App.cs” file.  At first glance, this looks like a regular C# file.  There’s no visual clue that it is a special type of file.  Double-clicking on the App.cs file will open the Design View for your AppFabric Application.

I can now add a new service by clicking on the “Add New Service” link/button in the upper-right corner of the Design View window.  In this case I’m going to add a new ASP.NET MVC2 project.  When that is done, the standard ASP.NET MVC2 project is added to the solution.  Since I’m adding this application as part of a Windows Azure AppFabric Application, Visual Studio automatically adds a few new assemblies related to hosting my web app in the AppFabric Container.  The new assemblies for the AppFabric Container are:

  • Microsoft.ApplicationServer.ApplicationModel
  • Microsoft.ApplicationServer.Web
  • Microsoft.ApplicationServer.Web.Hosting

The Design View will also update to reflect the addition of my new ASP.NET MVC2 application.  From there I can tell that my web app contains one endpoint and it does not reference any other services.

At this point I’m ready to go.  To see the basic setup, I select “Start without debugging” (or press CTRL + F5) from the Build menu.  There is no F5 debugging option in this CTP release.  Visual Studio will kick of the build and deployment process.  I can view the deployment output in the Output window.  Notice here that Visual Studio is starting a new emulator, the Windows Azure AppFabric Emulator.  Visual Studio will also start the standard Windows Azure compute and storage emulators.

From the Output window I can see the Application Id for your deployment.  Also of interest is the path to the various log files that are created as part of running in the Windows Azure AppFabric container.  It is my assumption that these log files would be similar to the logging available when running the application in a production Azure AppFabric container (upcoming post to walk-through that process).

Visual Studio will also launch a new console window that includes in the title the Application Id for your application.  This is the new emulator for Windows Azure AppFabric Applications.  Right now it’s pretty rough-looking – I’d expect it to get better before the final release.  I don’t see a whole lot here while the application is running.  I did notice the first time I started the application that the web browser (if creating a web application) will launch and Visual Studio will report the app is deployed and ready a little prematurely.  In that case, wait until the AppFabric Application emulator reports the app is ready and then refresh the browser.  I only noticed this on the initial deployment for the application – subsequent deployments seem to work as expected.

 

Now I would like to add a new SQL Azure “service” to my application.  Notice that the component is supposedly a SQL Azure service, yet the description indicates a SQL Server database (nearly any variant).  Possibly a CTP issue?

At this point Visual Studio will set up the SQL Azure information for me.  The design view will indicate a new SQL Azure instance is available, and viewing the Properties will allow me to set the database name, connection string, and actions to take against the database upon deployment and tear down of the application.  I did notice the provisioning process did not create the database for me as I would have expected.  (UPDATE: Learned from Neil MacKenzie’s recent post that the SQL actions to provision and de-provision apply only if using a DAC pack.)  Also, the default setup is to use SQL Express, not SQL Azure.  Although changing to be SQL Azure should be a simple connection string change.

Now I need to wire up the connection between my web app and the database.  To do so, simply add the SQL service as a Service Reference.

At this point, I can easily add some code to query my newly created database (note that I created a database manually and configured the connection details in the Properties panel). 

using (SqlConnection sqlConnection = ServiceReferences.CreateSQLAzureImport())
{
 sqlConnection.Open();
 var cmd = sqlConnection.CreateCommand();
 cmd.CommandText = “SELECT top 1 ProjectName FROM Project”;
 cmd.CommandType = CommandType.Text;
 result = (string)cmd.ExecuteScalar();
}

At this point I can press CTRL+F5 to run my application.  I expected my home page to show some data from my SQL connection.  Instead, I was greeted with an error message stating that I didn’t have permissions to access the database.  That’s odd . . . I’m an admin on the machine!

Applications running in the AppFabric container run under the NT AUTHORITY\NETWORK SERVICE security context, and as such don’t have many rights.  I changed the SQL connection string from the default Integrated Security to instead use SQL authentication (by setting User ID and Password properties).

To complete my fun little app, I decided to add a reference to Windows Azure AppFabric Cache to cache data instead of hitting the SQL Azure database each time.  To use it simply entered my cache authorization token and cache service URL in the service properties panel.

The code to work with AF Cache in an AF Application is pretty darn easy.

DataCache cache = ServiceReferences.CreateDataCacheImport();
time = cache.Get(“time”) as DateTime?;
if (time == null)
 
{
      time = DateTime.UtcNow;
      cache.Add(“time”, time, TimeSpan.FromMinutes(5));
}

When done, the deployment diagram for my simple app looks like this:

Overall, I’m pretty happy with this first CTP of the Windows Azure AppFabric Application SDK.  It has it’s rough edges, but it’s pretty nice overall.  I can definitely see the value-add and the direction looks really good.  Exciting!!

In an upcoming post I’ll walk through the process of deploying this application to the Windows Azure AppFabric container.

Advertisements