Task 1 – Preparing the Application to Run in Windows Azure
The Azure Store is a standard ASP.NET sample that mimics a simple commerce application. It presents a list of products that users can select and add to their shopping cart.Before you begin, you may wish to build and run the solution and become acquainted with its operation. In its initial state, the application runs outside the compute emulator.
In this task, you create a Windows Azure project in Visual Studio to prepare the application to run in Windows Azure.
- Open Visual Studio in elevated administrator mode from Start | All Programs | Microsoft Visual Studio 2010 by right clicking the Microsoft Visual Studio 2010 shortcut and choosing Run as administrator.
- If the User Account Control dialog appears, click Continue.
- In the File menu, choose Open and then Project/Solution. In the Open Project dialog, browse to Ex1-MovingMVCAppsToAzure\Begin or Ex1-MovingWebAppsToAzure\Begin in the Source folder of the lab, select Begin.sln in the folder for the language of your preference (Visual C# or Visual Basic) and click Open.Note:Depending on your needs, you may wish to explore migrating ASP.NET MVC or ASP.NET Web Form applications to Windows Azure. The procedures in this exercise are common to both types of application. Choose the most appropriate solution.
- Next, create a new cloud service project and add it to the solution. In the File menu, point to Add and select New Project. In the Add New Project dialog, expand the language of your preference (Visual C# or Visual Basic) in the Installed Templates list and select Cloud. Choose the Windows Azure Project template, set the Name of the project to AzureStoreService, keep the proposed location in the solution’s folder and then click OK.
Figure 1Configuring the application to run in Windows Azure - In the New Windows Azure Project dialog, click OK without adding new roles to the solution because you will use the existing application as a web role.
Figure 2No additional roles are required - Associate the ASP.NET project to the cloud project. In Solution Explorer, right-click the Roles node in the AzureStoreService project, point to Add and select Web Role Project in solution.
Figure 3Associating the web role project - In the Associate with Role Project dialog, select the AzureStore project and click OK.
Figure 4Associating the Web Role projectNote:When you associate a new role, Visual Studio updates the ServiceDefinition.csdef and the ServiceConfiguration.cscfg files. If either of these files is currently open, make sure that you save it to preserve these changes. - Add a reference to the assemblies required to support the Azure environment. In Solution Explorer, right-click the AzureStore project, select Add Reference, click the .NET tab, select the Microsoft.WindowsAzure.Diagnostics, Microsoft.WindowsAzure.ServiceRuntime, and Microsoft.WindowsAzure.StorageClient components and click OK.
Figure 5Adding a reference to the Windows Azure components - Configure a TraceListener to enable diagnostics logging for the application. To do this, open the Web.config file of the AzureStore project and insert a system.diagnostics section as shown (highlighted) below.(Code Snippet – Building ASP.NET Applications with Windows Azure - Ex01 DiagnosticMonitorTraceListener)
XML<configuration>
...
- <system.diagnostics>
- <trace autoflush="false" indentsize="4">
- <listeners>
- <add name="AzureDiagnostics" type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
- </listeners>
- </trace>
- </system.diagnostics>
</configuration>
Note:These settings in the system.diagnostics section configure a trace listener specific to Windows Azure that allows the application to trace code execution using the classes and methods available in the System.Diagnostics.Trace class.This step is usually unnecessary for roles created in Visual Studio because they already include the necessary settings in their role templates. - In the Global.asax file of the web role, declare the following namespaces. C#
- using Microsoft.WindowsAzure;
- using Microsoft.WindowsAzure.ServiceRuntime;
Visual BasicImports Microsoft.WindowsAzure Imports Microsoft.WindowsAzure.ServiceRuntime
- Next, find the Application_Start
method and insert the following (highlighted) code at the start of the
method to initialize the configuration settings publisher.(Code Snippet – Building ASP.NET Applications with Windows Azure - Ex01 SetConfigurationSettingPublisher – C#)
C#void Application_Start(object sender, EventArgs e)
{
- Microsoft.WindowsAzure.CloudStorageAccount.SetConfigurationSettingPublisher((configName, configSetter) =>
- {
- configSetter(RoleEnvironment.GetConfigurationSettingValue(configName));
- });
...
}
Visual BasicSub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
CloudStorageAccount.SetConfigurationSettingPublisher(Function(configName, configSetter) configSetter(RoleEnvironment.GetConfigurationSettingValue(configName)))
...
End Sub
Note:A configuration setting publisher simplifies the retrieval of storage account configuration settings. Applications only need to set up the publisher once using CloudStorageAccount.SetConfigurationSettingPublisher, and then elsewhere, whenever they require access to a storage account, they only require the name of the corresponding setting to access the storage account using CloudStorageAccount.FromConfigurationSetting.Note:Web roles can define a WebRole class with the entry point of the role. This class contains methods that Windows Azure calls at various stages during the role’s lifetime, for example, the OnStart method is called during role start up. You can use this method to initialize the role. Note, however, that for web roles executing in full IIS mode, Internet Information Server hosts the web application in a separate process (w3wp.exe), while the role entry point executes in a different process (WaIISHost.exe). Consequently, most web role initialization tasks need to be performed in the ASP.NET Application_Start method. - For an ASP.NET MVC project, ensure that the System.Web.MVC assembly is included in the service package that you deploy to Windows Azure. To do this:
- For a Visual C# project, expand the References node in Solution Explorer for the AzureStore project, right-click the System.Web.MVC assembly and select Properties.
- For a Visual Basic project, in Solution Explorer, right-click the AzureStore project and select Properties. In the Project Properties window, switch to the References tab, select the System.Web.MVC assembly, and press F4.
Figure 6Including assemblies in the service package deployed to Windows AzureNote:In general, you need to set Copy Local = True for any assembly that is not installed by default in the Windows Azure VMs to ensure that it is deployed with your application.
source:http://msdn.microsoft.com