Click here to Skip to main content
Click here to Skip to main content
Go to top

MVC on Azure for Beginners

, 4 Dec 2011
Rate this:
Please Sign up or sign in to vote.
This article will show how to develop and deploy an ASP.NET MVC web application to the Windows Azure platform.

Introduction

Windows Azure platform is Microsoft’s cloud solution. Compared to other cloud solutions, the biggest advantage is it seamlessly integrates with Microsoft .NET Framework and the development environment, therefore, regular .NET applications can be moved to Azure effortlessly. This article will show how to develop and deploy an ASP.NET MVC web application to the Windows Azure platform.

Develop an MVC application with the Azure SDK

In order to develop an application that can run on Windows Azure, you must have the Windows Azure SDK installed on your computer. The Azure SDK can be downloaded from the Microsoft Windows Azure portal site.

Windows Azure Site

After installing the Windows Azure SDK, you can open up Visual Studio to start a new project. There is a new project category Cloud in your Visual Studio project template. It has only one project template, Windows Azure Project.

Windows Azure Solution

After selecting Windows Azure Project, you can choose different Roles. Role represents the application running in the Windows Azure platform. There are three types of roles defined in the Windows Azure platform, web role, Windows role, and VM role. Web role stands for a web application, Windows role stands for a Windows service application, and VM role stands for a standalone Windows environment. In Visual Studio, you can create an ASP.NET Web Role, ASP.NET MVC 3 Web Role, ASP.NET MVC 2 Web Role, and WCF Service Web Role. Not all applications are suitable for running in Windows Azure.

New Windows Azure Project

If you select ASP.NET MVC 3 Web Role project, you can follow the project wizard to create the web application. This project wizard is identical to the normal ASP.NET MVC 3 project wizard. Below is what your solution will look like:

Project Detail

If you compare this solution with a normal ASP.NET MVC solution, you will see an additional Azure project is in here. It contains configurations of our Windows Azure project. There are two kinds of configuration file: Service definition file ServiceDefinition.csdef, and service configuration file ServiceConfiguration.cscfg.

The service definition file defines all the roles in your solution and their information that includes:

  1. Endpoint information. E.g., HTTP is used for web role.
  2. Storage information. E.g., local storage or Azure storage.
  3. Any custom configuration information.

This is what the ServiceDefinition.csdef file looks like in my Azure project:

<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MVConAzure" 
  xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
<WebRole name="Demo.MvcWebRole" vmsize="Small">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="Endpoint1" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="Endpoint1" 
    protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
</Imports>
</WebRole>
</ServiceDefinition>

The service definition cannot be changed at runtime. Any change requires a new deployment of your service. Your service is restricted in using only the network endpoints and resources that are defined in this file. You can think of this piece of configuration as defining what the infrastructure of your service is and how the parts fit together.

The service configuration file includes the entire configuration needed for the role instances in your service. Each role has its own configuration. The contents of the configuration file can be changed at runtime, which saves the need to redeploy your application when some part of the role configuration changes. You can also access the configuration in code in a similar manner that you might read a web.config file in an ASP.NET application.

This is what ServiceConfiguration looks like in my Azure project:

<?xml version="1.0" encoding="utf-8"?>
<ServiceConfiguration serviceName="MVConAzure" 
    xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceConfiguration" 
    osFamily="1" osVersion="*">
<Role name="Demo.MvcWebRole">
<Instances count="1" />
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" 
        value="UseDevelopmentStorage=true" />
</ConfigurationSettings>
</Role>
</ServiceConfiguration>

You can compile and run the application now. The application does not run on the Visual Studio Cassini local web server as a normal ASP.NET MVC application. It runs in the Windows Azure Emulator. The Windows Azure platform includes three major pieces: Windows Azure, SQL Azure, and Windows Azure AppFabric. The emulator acts very similar to the Windows Azure platform. It allows the developer to run and debug applications without deploying to the public cloud. Note: sometimes you will see this error:

Windows Azure Tools: Failed to initialize Windows Azure storage emulator. 
  Unable to start Development Storage. Failed to start Storage Emulator: the SQL Server instance 
  ‘localhost\SQLExpress’ could not be found. Please configure the SQL Server instance 
  for Storage Emulator using the ‘DSInit’ utility in the Windows Azure SDK.

This is because Windows Azure storage emulator by default uses SQLExpress as the backend database. If you don’t have a SQLExpress database installed or it is not installed with the default name, then the Storage Eumlator will not be able to find it. To fix this error, you can use DSInit to change the storage emulator to point to your local SQL Server database.

Deploy Application to Windows Azure Public Cloud

To deploy the application to the Windows Azure platform, you need to have a Windows Azure account first. You can register a Windows Azure account for a 90 days free trial. However, you need to provide your credit card information so Microsoft can charge you to your credit card for any over the limit usage. The deployment is very simple. You can publish your Windows Azure based application directly to the Windows Azure public cloud in Visual Studio.

Publish

Right click the Azure project and select the Publish menu item. This will bring up the Publish Windows Azure Application window.

  1. Select your subscription.
  2. Select your setting. Give a host service name. This name will be used as your subdomain name.
  3. Note: if this is your first time publishing an application to the Windows Azure platform, you will be prompted to add a Windows Azure authentication setting.

  4. Click the Pushing button to start the publish process. Visual Studio will compile your application first, and then connect to the Windows Azure platform with your account information to start deployment. There is a new tab, Windows Azure Active Log, that shows up in your Visual Studio IDE to display the progress of the deployment. The entire deployment could take several minutes.

Publish Status in Visual Studio

You can also view the deployment status in your Azure control panel:

Publish Status in Azure

After the deployment completes, the Windows Azure Activity Log window shows the status as completed and all the deployment information.

Windows Azure Activity Log

You can use the assigned URL, which is http://mvconazure.cloudapp.net here, to view just the deployed MVC web application in the Windows Azure public cloud.

Web Page

Summary

Cloud computing is getting popular right now because of the benefits of cost saving, scalability, and easy implementation. The Windows Azure platform is the primary cloud computing choice for .NET application developers because it allows us to leverage all our .NET development knowledge to join the new trend.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Henry He
Software Developer (Senior)
United States United States
Senior Software Developer from New Jersey, USA

Have 12+ years experience on enterprise application development with various technologies.

Comments and Discussions

 
QuestionGood job Pinmemberzubairy20-Aug-12 11:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140926.1 | Last Updated 4 Dec 2011
Article Copyright 2011 by Henry He
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid