Hands-On Lab

Advanced Web And Worker Roles

Lab version:1.0.0

Last updated:10/28/2018

Contents

Overview

Exercise 1: Registering Sites, Applications, and Virtual Directories

Task 1 – Defining Multiple Sites in a Web Role Using the Service Model

Task 2 – Creating Virtual Applications and Virtual Directories

Exercise 2: Using Start-Up Tasks to Register a COM Component

Task 1 – Registering a COM Component

Exercise 3: Using Start-Up tasks to install PHP with the Web Platform Installer

Task 1 – Installing PHP with the Web Platform installer

Summary

Overview

When Windows Azure was first released, there were a few, but significant restrictions in the programming model. Things like Full Trust, Administrative Access, and the full IIS feature-set were initially restricted for security reasons. This impacted the types of applications that could be created in Windows Azure because even small changes to things like configuration settings were often blocked by lack of administrative control over the VM Instances. Over time, those restrictions were lifted – first Full Trust, and now the ultimate control: Administrative access and Full IIS support.

With the latest SDK release, you can choose to run your web sites under IIS7, not in Hosted Web Core as in the past, but in full IIS. This means you can use all the facilities of IIS now like custom modules, multiple websites, VDIR support, application pool isolation, and more.

Additionally, you can now choose two different ways to exercise your administrative control. You can bootstrap the machine as an administrator using something called “Startup Tasks” shown in this lab. This temporarily raises your permissions to administrative and allows you perform small setups, update configuration settings, or other bootstrapping tasks. Once completed, your code will run as a normal, unprivileged user. The second method is that you can now configure your role to simply run as an administrator the entire time. In most cases, the Startup Tasks are the right choice as running your role with administrative permissions the entire time has security implications.

This lab introduces these new capabilities that are unlocked in Windows Azure and allow more advanced application scenarios.

Objectives

In this hands-on lab, you will learn how to:

  • Use advanced service model features that enable hosting a Web role in IIS.
  • Host multiple sites in a Web role and bind them to the same endpoint using host headers.
  • Create virtual applications and map them to selected sites.
  • Utilize user virtual directories to share common content between sites.
  • Set up the role environment by executing start-up tasks to register a COM.
  • Install complex componentsrequired by a web role, such as a scripting language's binary files.

Note:This lab shows advanced features of Web and Worker roles in Windows Azure; it assumes that you have sufficient knowledge of Windows Azure.If you are beginner in Windows Azure, see the Introduction to Windows Azure lab in this training kit first.

Prerequisites

The following is required to complete this hands-on lab:

  • IIS 7 (with ASP.NET, WCF HTTP Activation)
  • Microsoft .NET Framework 4.0
  • Microsoft Visual Studio 2010

◦Microsoft Visual C++ 2010 (required for Exercise 2 of the lab)

  • Windows Azure Tools for Microsoft Visual Studio 1.6
  • SQL Server 2005 Express Edition (or later)

Setup

In order to execute the exercises in this hands-on lab you need to set up your environment.

  1. Open a Windows Explorer window and browse to the lab’sSourcefolder.
  2. Double-click the Setup.cmd file in this folder to launch the setup process that will configure your environment and install the Visual Studio code snippets for this lab.
  3. If the User Account Control dialog is shown, confirm the action to proceed.

Note: Make sure you have checked all the dependencies for this lab before running the setup.

Using the Code Snippets

Throughout the lab document, you will be instructed to insert code blocks. For your convenience, most of that code is provided as Visual Studio Code Snippets, which you can use from within Visual Studio 2010 to avoid having to add it manually.

If you are not familiar with the Visual Studio Code Snippets, and want to learn how to use them, you can refer to the Setup.docx document in the Assets folder of the training kit, which contains a section describing how to use them.

Exercises

This hands-on lab includes the following exercises:

  • Registering Sites, Applications, and Virtual Directories
  • Using Start-Up Tasks to Register a COM Component
  • Using Start-Up Tasks to Install PHP with the Web Platform Installer

Estimated time to complete this lab: 60 minutes.

Note:When you first start Visual Studio, you must select one of the predefined settings collections. Every predefined collection is designed to match a particular development style and determines window layouts, editor behavior, IntelliSense code snippets, and dialog box options. The procedures in this lab describe the actions necessary to accomplish a given task in Visual Studio when using the General Development Settings collection. If you choose a different settings collection for your development environment, there may be differences in these procedures that you need to take into account.

Exercise 1: Registering Sites, Applications, and Virtual Directories

When hosted in IIS Hosted Web Core (HWC), Windows Azure Web roles cansupport a single application bound to no more than a single HTTP and a single HTTPS endpoint. This model is enabled with minimal configuration and was the only one originally supported in Windows Azure when it was first introduced. To use HWC, you only need to specify the HTTP and HTTPS endpoints bound to the Web application in the service model, as shown in the figure below.

Figure 1

Simplified service model for Hosted Web Core (HWC) hosting

While this approach is still valid and you can use it in many scenarios where a single application per role is sufficient or does not require multiple endpoints, more advanced capabilities are now available that provide full access to all IIS features. In this advanced model, applications are hosted in IIS instead, both in Windows Azure and in the compute emulator, allowing each role to support multiple sites, virtual applications and virtual directories, as well as providing support for binding each site to multiple endpoints.

To enable the Full IIS capabilities, the service model defines a Sites element that can contain one or more Site definitions, where each site is bound to one or more endpoints, as shown in the following figure.

Figure 2

Advanced service model for Full IIS hosting

In this exercise, you will learn how to define different sites, applications, and virtual directories within a Web role using a single Web role application, which you will map to multiple sites. To show site customization, you will modify the master page of the application to show the site name on each page. During the exercise, you will create a Virtual Application for a hypothetical CRM application and enable it for selected sites. Additionally, you will use Virtual Directories to share common content between applications.

Task 1 – Defining Multiple Sites in a Web Role Using the Service Model

The service model in Windows Azure is determined by the service definition file, which defines the roles that comprise a service, optional local storage resources, configuration settings, certificates for SSL endpoints, and, as you will see in the following exercise, start-up tasks.

In this task, you edit the service model to define three separate sites that map to the same Web application, namely, Contoso, Fabrikam, and Litware. These sites are all bound to a single endpoint and use host headers to isolate them.

Note:This task requires thehosts file to be updated. If you did not execute the setup instruction for this lab, this exercise will not work.Proceed with the setup instructions before starting with this task.

  1. Start Microsoft Visual Studio 2010 as an administrator.
  2. Open the Begin solution located in Ex1-FullIIS in the Source folder of the lab.
  3. In the Windows Azure project, open the service model file, ServiceDefinition.csdef. Notice that it defines a single Web role named SampleWebApp, which in turn defines a site named Web.
  4. In the Sites element of the SampleWebApp Web role, locate the nested Site element named Web and change its name attribute to Fabrikam. Next, add a physicalDirectory attribute to this element that points to"..\SampleWebApp".

Note: This step changes the name of the site from its Web default value to Fabrikam. Note that for sites other than the default Web site, you need to specify a physical directory for the site’s content.

  1. Now, locate the single Binding element for the Fabrikam site and add a hostHeader attribute with a value of "". The updated site definition should appear as shown in the following figure.

Figure 3

Definition for the Fabrikam site

Note: The updated endpoint binding ensures that the site only responds to traffic that specifies the appropriate host header, namely requests for .

  1. Next, define a new site for Contoso by inserting the following (highlighted) definition inside the Sites element.

(Code Snippet – AdvancedWebAndWorkerRoles-Ex1-01-ContosoSite-XML)

XML

WebRolename="SampleWebApp"

<Sites

<Sitename="Fabrikam"physicalDirectory="..\SampleWebApp"

<Bindings

<Bindingname="HttpIn"endpointName="HttpIn"hostHeader=" />

</Bindings

</Site

<Sitename="Contoso"physicalDirectory="..\SampleWebApp"

<Bindings

<Bindingname="HttpIn"

endpointName="HttpIn"

hostHeader=" />

</Bindings

</Site

</Sites

...

</WebRole

  1. Similarly, create a site for Litware, as shown (highlighted) below.

(Code Snippet – AdvancedWebAndWorkerRoles-Ex1-02-LitwareSite-XML)

XML

WebRolename="SampleWebApp"

<Sites

<Sitename="Fabrikam"physicalDirectory="..\SampleWebApp"

<Bindings

<Bindingname="HttpIn"

endpointName="HttpIn"

hostHeader=" />

</Bindings

</Site

<Sitename="Contoso"physicalDirectory="..\SampleWebApp"

<Bindings

<Bindingname="HttpIn"

endpointName="HttpIn"

hostHeader=" />

</Bindings

</Site

<Sitename="Litware"physicalDirectory="..\SampleWebApp"

<Bindings

<Bindingname="HttpIn"

endpointName="HttpIn"

hostHeader=" />

</Bindings

</Site

</Sites

...

</WebRole

Note: All three sites are mapped to the same physical directory ("..\SampleWebApp") and bound to the same HTTP endpoint (HttpIn). The endpoint binding, however, specifies a different host header value for each site.

  1. PressF5 to build and run the Windows Azureproject. Wait for the application to launch in the compute emulator and for the browser to open pointing at the default site address. Notice that it shows an error page with a status “HTTP 400 Bad Request”.

Note: By default, the Windows Azure Tools for Visual Studio opens the default Web site in your browser. In this case, however, you have removed the default mapping and there is no longer any site accessible without the use of a host header, which explains the error response.

  1. Start Internet Information Services (IIS) Manager.
  2. Browse to the Sites node and notice that three new sites were created for Fabrikam, Contoso and Litware. The name of the site is derived from the Windows Azure deployment ID, the Web role name, the instance ID,and the name of the site in the service model file.

Figure 4

Internet Information Services (IIS) Manager showing the sites created by the Web role

  1. In the (IIS) Manager, click Contoso node and take note of the port where it is hosted. You will findthe port number in the right pane, inside Manage Web Site. Repeat this action with Fabrikam and Litware nodes. You will use these ports later in this exercise.

Figure 5

Application Port

  1. Now, select the Application Pools node and review the pools created to support the applications hosted by the Web role.

Figure 6

Application pools defined for the Web role sites

  1. In the browser window, change the address to that of the Contoso site. Make sure to use the same port number you obtained in the step 11. For example, if the browser is currently showing set the address to

Note: The setup procedure updated the hosts file, adding three new entries for and all pointing to the loopback address (127.255.0.0).

  1. Notice that the title shown in the home page is “My ASP.NET Application”.
  2. In Visual Studio, open the Site.Master file in the SampleWebApp project. Inside the body of the page, locate the h1 element nested inside a div element with its class set to title.Replace its content, which should be “My ASP.NET Application”, with the following (highlighted) expression.

(Code Snippet – AdvancedWebAndWorkerRoles-Ex1-03-SampleWebAppTitle-HTML)

HTML

...

body

formrunat="server">

divclass="page">

divclass="header">

divclass="title">

h1

<%=System.Text.RegularExpressions.Regex.Match(System.Web.Hosting.HostingEnvironment.ApplicationHost.GetSiteName(), "[^_]+$").Value%>

</h1

</div

divclass="loginDisplay">

...

</form

</body

...

Note: The added code retrieves the Web site name using the ApplicationHost class in the System.Web.Hosting.Environment namespace and then uses a regular expression to extract a friendly name for the site.

  1. Save the updated file.
  2. In the browser window, refresh the page and notice how the title of the page now shows “Contoso” instead of “My ASP.NET Application”.

Note: Editing the site content while the application is running is a useful technique to use during debugging, allowing you to apply changes without restarting the deployment. Be aware that this requires the role to be hosted in IIS; otherwise, when using the simple service model that uses Hosted Web Core (HWC), changes only become effective after redeploying the service.

  1. Open a new browser window and navigate to Notice that the Web site title in the new window is Litware.Similarly, open a third browser window and navigate to

Note: Remember to specify the correct port number for each address if the deployment is not using the standard HTTP port (80).

Figure 7

Multiple sites hosted in the Web role

Note:In this particular case, all three sites are mapped to the same physical directory but use host headers to identify which site responds to a request. A Web role application can extract the site name from the request, as was shown earlier for the title, and then customize the output to render the pages differently depending on the requested site. You can, of course, map each site to a different physical directory and use different content for each one.

  1. Close all browser windows.

Task 2 – Creating Virtual Applications and Virtual Directories

In this task, you create a new Virtual Application for an application that you only enable for selected sites.

  1. In Visual Studio, add the sample CRM application project located in the Assets folder of the lab to the solution.
  2. Now, in the Windows Azure project, open the service model file ServiceDefinition.csdef.
  3. Locate to the Site element for the Contoso site and insert the VirtualApplication definition, as shown (highlighted) below.

(Code Snippet – AdvancedWebAndWorkerRoles-Ex1-04-ContosoVirtualApplicationDefinition-XML)

XML

WebRolename="SampleWebApp"

<Sites

<Sitename="Fabrikam"physicalDirectory="..\SampleWebApp"

...

</Site

<Sitename="Contoso"physicalDirectory="..\SampleWebApp"

<VirtualApplicationname="CRM"

physicalDirectory="..\..\..\..\Assets\CRM"

<VirtualDirectoryname="Scripts"

physicalDirectory="..\SampleWebApp\Scripts" />

<VirtualDirectoryname="Styles"

physicalDirectory="..\SampleWebApp\Styles" />

</VirtualApplication

...

</Site

<Sitename="Litware"physicalDirectory="..\SampleWebApp"

...

</Site

</Sites

...

</WebRole

Note: The virtual application definition shown above creates a new Web application in the Contoso site and maps it to the CRM path, thus, making it reachable at .

Because the CRM site shares content with the main site, in particular, its Scripts and Styles folders, these are created as virtual directories under the CRM site and mapped to the corresponding physical directories present in the main site.

  1. Similarly, enable the CRM application for the Litware site by inserting the following (highlighted) virtual application definition.

(Code Snippet – AdvancedWebAndWorkerRoles-Ex1-05-LitwareVirtualApplicationDefinition-XML)

XML

WebRolename="SampleWebApp"

<Sites

<Sitename="Fabrikam"physicalDirectory="..\SampleWebApp"

...

</Site

<Sitename="Contoso"physicalDirectory="..\SampleWebApp"

...

</Site

<Sitename="Litware"physicalDirectory="..\SampleWebApp"

<VirtualApplicationname="CRM"

physicalDirectory="..\..\..\..\Assets\CRM"

<VirtualDirectoryname="Scripts"

physicalDirectory="..\SampleWebApp\Scripts" />

<VirtualDirectoryname="Styles"

physicalDirectory="..\SampleWebApp\Styles" />