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.
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.
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.
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:
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:
- Endpoint information. E.g., HTTP is used for web role.
- Storage information. E.g., local storage or Azure storage.
- Any custom configuration information.
This is what the ServiceDefinition.csdef file looks like in my Azure project:
="1.0"="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:
="1.0"="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.
Right click the Azure project and select the Publish menu item. This will bring up the Publish Windows Azure Application window.
- Select your subscription.
- Select your setting. Give a host service name. This name will be used as your subdomain name.
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.
- 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.
You can also view the deployment status in your Azure control panel:
After the deployment completes, the Windows Azure Activity Log window shows the status as completed and all the deployment information.
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.
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.