Chapter 9. Windows Azure and ASP.NET

This chapter walks you through the steps of creating a Windows Azure application and deploying it in the cloud. Whenever you see a reference to Windows Azure, it means you're building a web solution that can be deployed in the cloud. By now, you know you can't create WinForms applications in Windows Azure.

An application published in the Microsoft cloud is referred to as a service, even if it's a web site. So, you first create a new Windows Azure service to host the web site. Then, you create a simple ASP.NET application and publish it in the cloud.

Creating a Windows Azure Service

First, you need to set up a Windows Azure service in the cloud so you can deploy a Windows Azure application later. Each Windows Azure service created in the cloud is mapped to a virtual machine; however, you have no control over the virtual machine itself—you can only deploy your applications and configure certain parameters. Follow these steps:

  1. Open Internet Explorer, and go to http://windows.azure.com. You're prompted to sign in with your Windows Live account.

  2. When you've logged in, you see the Windows Azure project you created in Chapter 3. Click your project name; this gives you the list of Azure services you've created so far, as shown in Figure 9-1.

    Note

    This chapter assumes you've signed up for the Windows Azure service. Signing up for the service automatically creates a Windows Azure project in the cloud.

    Windows Azure services summary page

    Figure 9.1. Windows Azure services summary page

  3. Click New Service to create your first Windows Azure service. Doing so brings up a page asking which kind of service you want to create:

  4. Storage Account. Lets you store large amounts of data

  5. Hosted Services. Lets you run services and web applications

  6. Click Hosted Services. Another page opens, and you're prompted to enter a Service Label and a Service Description (see Figure 9-2). The Service Label must be unique across all existing services in the cloud. If the name you enter is already registered, a message says so. Click Next.

    Creating a new Azure service

    Figure 9.2. Creating a new Azure service

  7. The configuration screen in Figure 9-3 allows you to select a unique service name to use for your URL. This URL is available on the public Internet and as such must be globally unique. You also need to select a Service Affinity option. Figure 9-3 shows that you're creating a new service to be made available through http://AzureExample.cloudapp.net and hosted in an existing affinity group that you created when creating your SQL Azure database. The affinity group is called USSouthGroup, and it's located in South Central US.

    Windows Azure service configuration page

    Figure 9.3. Windows Azure service configuration page

As mentioned in Chapter 1, creating an affinity group is very important for two reasons:

  • Price. When a Windows Azure service connects to a SQL Azure database located in the same region, there are no additional charges when transferring data between the service and the database.

  • Failover. If something happens and either the Windows Azure service or the SQL Azure database must failover to another region, all the services in the same affinity group are moved together if possible, keeping the performance and cost structure of the service consistent.

  1. To make sure your service is created successfully, click Check Availability in the configuration window shown in Figure 9-3. A message is returned indicating whether the check was successful. If is the service name is available, click Create. When the service is created, you see a page similar to the one shown in Figure 9-4.

    Windows Azure service management page

    Figure 9.4. Windows Azure service management page

Note

As long as no code is deployed on this service, you aren't charged. However, as soon as you deploy something, the clock starts ticking from a billing standpoint.

Creating a Windows Azure Project

Let's create a simple Windows Azure application in Visual Studio that displays a list of database users. The Windows Azure project is an ASP.NET application created with a special project template: Cloud.

Configuring Your Development Environment

You must first configure the Windows Azure Tools on your development environment to be able to develop a Windows Azure ASP.NET application. You must be running Windows Server 2008 or higher, or Windows Vista or higher. The Windows Azure Tools provide a runtime environment on your machine that allows you to develop and test a Windows Azure project. It basically runs a local cloud for development purposes. After it's installed, you see a new project type: Cloud. When creating your project in Visual Studio, you can select the Cloud project type; an option to install the Windows Azure Tools is available the first time you do so.

Note

If you need to download the Windows Azure Tools, go to the Microsoft Download Center at www.microsoft.com/downloads/. Search for Azure Tools, and pick the version that applies best to your Visual Studio version. Make sure to download and install this extension.

Creating Your First Visual Studio Cloud Project

To create a Visual Studio cloud project, follow these steps:

  1. Start Visual Studio in elevated mode (as an Administrator). To do so, right-click Microsoft Visual Studio 2008 or Microsoft Visual Studio 2010, and select "Run as administrator," as shown in Figure 9-5. Running as Administrator is required by the Windows Azure simulation tools that give you the ability to test your Azure solution locally.

    Start Visual Studio in elevated mode.

    Figure 9.5. Start Visual Studio in elevated mode.

If you don't start Visual Studio in elevated mode, you're able to create the project but you can't run it. If you try, you get an error message telling you to restart Visual Studio, as shown in Figure 9-6.

Error trying to run an Azure project when not in elevated mode

Figure 9.6. Error trying to run an Azure project when not in elevated mode

  1. Choose File → New → New Project to bring up the window shown in Figure 9-7 for Visual Studio 2008 or Figure 9-8 for Visual Studio 2010. Select the Cloud project type (Cloud Service in Visual Studio 2010), and choose Windows Azure Cloud service.

    Cloud project type in Visual Studio 2008

    Figure 9.7. Cloud project type in Visual Studio 2008

    Cloud Service project type in Visual Studio 2010

    Figure 9.8. Cloud Service project type in Visual Studio 2010

  2. Enter a name for your project, and click OK. A new screen comes up (see Figure 9-9) in which you can select a role for your application. The role of your ASP.NET application dictates its primary purpose and what you're able to do with the project. The following roles are available:

    • ASP.NET Web Role. Lets you create a web site with ASP.NET.

    • ASP.NET MVC 2 Web Role. Lets you create an MVC 2 web application (only available in Visual Studio 2010).

    • WCF Web Service Role. Allows you to create a WCF service in Windows Azure.

    • Worker Role. Equivalent to a background service in Windows Azure that has no user interface.

    • CGI Web Role. Lets you create a web application using a technology other than ASP.NET, such as Python.

    Role Types in Visual Studio 2010

    Figure 9.9. Role Types in Visual Studio 2010

  3. For this example, select the first option (ASP.NET Web Role), and click the right arrow (

    Role Types in Visual Studio 2010
  4. To change the name, select WebRole1 and press the F2 key or click the Edit link. To see the Edit link, place your mouse somewhere on the WebRole1 service list item: you see two icons come up on the right. The first one (with the pencil) allows you to edit the name, and the second lets you remove this web role (see Figure 9-10).

    Editing the web role's name

    Figure 9.10. Editing the web role's name

  5. Change the name to wrAzureExample, and press the Enter Key. Don't use AzureExample as the web role name, or there will be a conflict with the solution name provided earlier. The web role should now look like Figure 9-11.

    Web role name changed

    Figure 9.11. Web role name changed

  6. Click OK.

At this point, you've created a new cloud solution. Solution Explorer looks a little different than it does for a typical ASP.NET project. Your cloud solution contains two projects: the AzureExample project and the wrAzureExample web role, which is itself a project, as shown in Figure 9-12. The AzureExample project contains configuration files that will be deployed later in Windows Azure.

Project layout for the ASP.NET web role

Figure 9.12. Project layout for the ASP.NET web role

Note

At the time of this writing, Windows Azure supports .NET 3.5 SP1 and .NET 4.0.

Connecting a GridView to SQL Azure

Continue the example by following these steps:

  1. Add a GridView control on the Default.aspx page, and connect it to a SQL Azure database. Although SqlDataSource is compatible with SQL Azure, it isn't possible to add a SqlDataSource and configure it with the built-in wizard; these steps next show you how to configure the SqlDataSource manually.

  2. Open the Default.aspx page, and select Design view.

  3. Drag a SqlDataSource from the Toolbox. Drag a GridView control on the page as well, and set its Data Source property to SqlDataSource1, as shown in Figure 9-13.

    Changing the GridView's Data Source property

    Figure 9.13. Changing the GridView's Data Source property

  4. Open the web.config file, and enter your connection string. (Chapter 3 explains how to retrieve your connection string from the Azure Portal.) You need to add a connectionStrings node under the configuration node, as shown in the following example:

    <connectionStrings>
         <add
              name="Connection1"
              providerName="System.Data.SqlClient"
             connectionString="Server=tcp:jt4y4mmglp.database.windows.net;
                  Database=EnzoLog;User ID=test@jt4y4mmglp;
                  Password=yourPasswordHere;
                 Trusted_Connection=False;Encrypt=True;"/>
    </connectionStrings>

    Note

    Make sure to specify the user ID as <user name>@<server name>.

  5. Go back to the Default.aspx page, and change the SqlDataSource settings as follows by adding the ConnectionString and the SelectCommand settings manually.

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    ConnectionString="<%$ ConnectionStrings:Connection1 %>"
    SelectCommand="SELECT uid, name FROM sys.sysusers ORDER BY 1" >
</asp:SqlDataSource>

Running the project should give you output similar to that shown in Figure 9-14.

Running the solution locally and fetching data from SQL Azure

Figure 9.14. Running the solution locally and fetching data from SQL Azure

So far, you're running this project on the local machine. Although the Windows Azure Tools are required, this project doesn't use any of the Windows Azure storage options; it connects directly to a live SQL Azure database.

You could have created the same project as a regular ASP.NET application and obtained the same result. However, because you're about to deploy this project on Windows Azure, you must create a cloud project.

Note

This chapter assumes you've configured the SQL Azure firewall correctly. Also, connecting from a Windows Azure service requires you to set the "Allow Microsoft Services access to this server" option in the firewall configuration. If you get a connection error stating that you aren't allowed to connect from a specific IP address, see Chapter 3 for detailed information about how to properly set up the SQL Azure firewall.

Deploying an ASP.NET Application in Windows Azure

You're almost there. In this section, you walk through the steps of deploying the ASP.NET application in the cloud:

  1. You need to publish your project. Right-click AzureExample, and click Publish. This action, upon success, opens two windows:

    • Windows Explorer. This window contains the two files you need to deploy in Windows Azure. You need this window soon, so don't close it.

    • Internet Explorer. The Windows Azure Portal is automatically shown as well, because you need to deploy the binaries there. You may be prompted to sign in first.

  2. When you've logged in, the Internet Explorer window shows you the home page for your Windows Azure projects. Click the Windows Azure project under which you previously created the AzureExample service.

  3. You should see the page shown in Figure 9-1 at the beginning of this chapter. Click AzureExample, which should bring you to what you saw earlier in Figure 9-4.

  4. On the right side of the browser page is a vertical bar with a left-pointing triangle. Click the triangle to open a split page with both Production and Staging sections, as shown in Figure 9-15. Deploying in staging creates a temporary service that you can use to test before promoting to the final public URL. This way, you can test that your application is working as desired before deploying it.

    Viewing the production and staging environments

    Figure 9.15. Viewing the production and staging environments

  5. Click the Deploy button under Staging. Doing so brings up a new page (see Figure 9-16) with the following fields:

    • Application Package. This is the package that Visual Studio built for you. It's the file with extension .cspkg in the Windows Explorer window. Clicking Browse opens an Open form. You may find it easier to copy the entire path from the Windows Explorer window and paste it in the Open form. Select the package file, and click Open.

    • Configuration Settings. This is the configuration file of your cloud service project with a .cscfg extension. Again, click Browse and select the configuration file from the Open form.

    • Operating System Settings. This section allows you to configure the specific version of the operating system you're using for this service. Leaving Upgrade Method set to Automatic ensures that you get the latest security patches and .NET upgrades automatically. You can also choose Manual and select the desired system.

    • Service Deployment Name. Under Service Deployment Name, you need to enter a label for your service. Type TEST001, for example.

    Windows Azure first-time deployment page

    Figure 9.16. Windows Azure first-time deployment page

  6. When the information provided looks like Figure 9-16, click Deploy. Doing so first copies the appropriate files to the cloud and creates the virtual machine in which your project will be deployed (see Figure 9-17). After it's deployed, your service isn't be running just yet; however, you start incurring charges immediately.

    Deploying a package in staging

    Figure 9.17. Deploying a package in staging

Your screen should now look like Figure 9-18. To start your virtual machine, click Run under Staging. This operation may take a few minutes. When the startup operation is completed, you see the status of your service as Ready. At this point, you can test your ASP.NET application in the cloud by clicking the Web Site URL link in Staging. Notice that this isn't the URL you originally requested—the public URL previously registered is reserved for production. Instead, staging is given a deployment GUID that's also part of the web site's URL. In this case, the URL for the staging environment is http://de2d988c570d4c36b43a72686391d4b5.cloudapp.net/

Completed deployment of your service

Figure 9.18. Completed deployment of your service

Clicking the link opens another browser and runs your service in the cloud (see Figure 9-19).

Running the service in the cloud

Figure 9.19. Running the service in the cloud

Note

As mentioned previously, you're now paying for this Windows Azure service. You must first suspend your service (by clicking Suspend) if it's running; however, that alone doesn't stop you from accruing charges. You must also click the Delete button under both your staging and production environments. On the same page is a Delete Service button; it releases your service altogether and doesn't affect how you're billed.

Now that the code has been tested in staging, go back to the service management page (shown earlier in Figure 9-18) and click the center icon (

Running the service in the cloud

You may have noticed that you haven't changed any database settings between staging and production. Staging and production environments refer only to Windows Azure services, not the SQL Azure database running behind the scenes.

As a result all versions of your ASP.NET application (when run locally, in staging or in production) point to the same SQL Azure database by default, unless you specifically change your configuration settings in each environment (see Figure 9-20). You also need to make sure your firewall configuration is correctly set, or you may have a surprise when publishing your web application in the cloud.

Summary of deployment scenarios

Figure 9.20. Summary of deployment scenarios

Conclusion

This chapter showed you how to create a simple ASP.NET application that connects to a SQL Azure database. This requires a few configuration steps, including the creation of a Windows Azure service in the cloud and the installation of a Visual Studio extension called the Windows Azure Tools.

While this chapter focused primarily on deploying an ASP.NET application in the cloud, you can very easily deploy ASP.NET projects on your enterprise IIS servers and configure the connection string to connect to a SQL Azure database. This gives you multiple deployment options to consider, although you must keep in mind that each deployment option has a different cost structure. Data transfer between Windows Azure and SQL Azure is free within the same region, but you pay for compute time in Windows Azure.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset