Deploying to Windows Azure from Visual Studio 2010
In order to be able to deploy a Windows Azure application onto the Windows Azure servers, you will be required to create an hosted service that will host and execute your code and a storage account that will hold the packaged Azure application before the deployment. Deploying an Azure application onto the hosted service from Visual Studio 2010 will require setting up certificates.
The next section will explain the entire process of deployment end-to-end. Later chapters in the book will describe how to run and debug applications on the local developer machine.
Creating the hosting service
In order to create the hosting service, you will need to complete the following steps:
1. Log on to the Windows Azure Management Portal at https://windows.azure.com.
2. Go to the Hosted Services, Storage Accounts & CDN section and click on Create a New Hosted Service.
3. Give the hosted service a friendly name for you to keep track of and also a suitable URL prefix. The URL prefix is public, so it must be globally unique. In this example, the complete URL https://packtdemoapp.cloudapp.net has been used.
4. Select the option to Create a New Affinity Group. On the next screen, when asked about the location where your services should be hosted, select one that is close to your physical location. For this example, South Central US has been selected. Creating a new affinity group allows you to group your services together at the same physical location. This is useful for making sure that the servers hosting and executing the Azure application are in the same data center as the storage accounts:
Note
Affinity groups
Ensure that while deploying an Azure application, the hosted service and the storage account are hosted in the same data center by using affinity groups. Having your services hosted in different data centers can cause a lot of latency in your application, as well as potentially cause you to be charged with bandwidth costs between data centers.
Just selecting a location for your service, without setting an affinity group does not guarantee that your services will be in the same data center in that location. Confirm that the affinity group is set on your services to avoid this.
5. If you had an Azure application already packaged, you could select to immediately upload and deploy the application on this screen. In this example, we are deploying to Azure directly from Visual Studio 2010, so select Do not deploy.
6. Click on OK to create the hosted service, which will be ready for us to deploy an Azure application, as shown in the following screenshot:
Creating the storage account
When the Windows Azure Management Portal is used to deploy your Azure application, you can directly upload your packaged Azure application onto the Azure servers. However, when deploying from Visual Studio 2010, the packaged application needs to be stored in the Azure Storage, so that it can be deployed onto the Azure servers from there.
Instead of the Windows Azure Management Portal, create a storage account and again give it a suitable endpoint name. The following screenshot shows an endpoint being created within the South Central US affinity group with the endpoint name packtdemo:
Deploying applications onto Azure servers
In order to be able to deploy an application onto Azure servers, you will first need an application to deploy. Next, you will create a new Azure project that will be used as our example:
1. Open Visual Studio 2010, create a new Windows Azure Project, and give the project a suitable name, such as PacktAzureDeploymentDemo, as shown in the following screenshot:
2. Add a single ASP.NET Web Role into the project. An Azure solution with a single web role added to it can be seen in the following screenshot. Other roles are visible and selectable, but this exercise will only be utilizing the web role:
3. Visual Studio will create the solution with two projects as can be seen in the following screenshot. The first project PacktAzureDeploymentDemo is the Azure project that defines the metadata for all of the roles and instances that will be created when deployed onto Azure servers. The second project WebRole1 is a standard ASP.Net project, where all the logic of your project will reside:
4. Once the project has been created, feel free to modify the
Default.aspx
file in WebRole1 slightly, by adding your own name to the header similar to what is shown in the following screenshot. In this way, once the application is deployed, you can see that it is indeed your application:
The project is now ready to be deployed onto the Azure servers.
Creating certificates to authenticate deployment
In order to allow Visual Studio 2010 to communicate with the Windows Azure deployment API, management certificates are required to authenticate the requests. Visual Studio has a wizard that helps with the creation and uploading of the required certificates:
1. With your Azure solution still open, right-click on the Azure project (not the ASP.Net project) and select Publish, as shown in the following screenshot:
2. This will bring up a new Publish Windows Azure Application dialog box as shown in the following screenshot. If you had previously configured your credentials, you could select them here. As this is the first time you are opening this dialog box, you will be required to configure your credentials. Click on Sign in to download credentials to begin the process:
3. A web browser will be opened and request that you sign into your Azure account. Once you have signed in, you will be prompted by the browser to download the credential file.
4. Once the credential file has been downloaded, you can return to Visual Studio 2010 and import the file.
5. Once your credentials have been configured, you will now be able to select the hosted service and environment to deploy your application (production or staging). More details about the different deployment environments will be discussed in later chapters. A deployment to production has been chosen in the following screenshot:
6. Click on Publish and Visual Studio 2010 will begin the process of deploying your application. The following screenshot shows the starting of the deployment process. The Visual Studio packages the project, connects to the Azure servers, and then uploads the package:
7. The deployment process has been completed in the following screenshot. The package was successfully uploaded and the web role was initialized and brought online, ready to service user requests. Be aware that the first deployment to your instances can take a long time, sometimes up to 15 minutes before they are fully running. There are ways to shorten the deployment time during the development by using web deployment after the initial deployment:
Note
Web deployment
To shorten the development cycle, it is possible to use web deployment to update an existing deployment. You can only have a single compute instance running and the changes made are only temporary until the compute instance restarts. This is a development enhancer and should never be used on a production deployment. More details about this feature can be found on the Windows Azure Tools blog at the following URL:
8. Once Visual Studio 2010 has completed the deployment and the servers have reported back that the service has started successfully, you will finally be able to view your application.
9. Click on the website URL in the Windows Azure Activity Log window to browse to the site, this will be the URL you configured when creating the hosted service. If all goes well, you should see your website running with your modifications, similar to the following screenshot:
10. Remember to remove your deployment when you are finished, so that you are not charged for it. You can do this through the Azure Management Portal. You can also do it quickly from within the Visual Studio by right-clicking on Deployment in the Windows Azure Activity Log window and selecting Delete.