Exercises

stickyraffleΛογισμικό & κατασκευή λογ/κού

4 Νοε 2013 (πριν από 3 χρόνια και 11 μήνες)

88 εμφανίσεις

Page |
1






Hands
-
On Lab

Advanced Web And Worker Role
s



Lab version:

1
.
0
.0

Last updated:

11/5/2013
















Page |
2



CONTENTS

OVERVIEW

................................
................................
................................
................................
...................

3

EXERCISE 1: REGISTER
ING SITES, APPLICATI
ONS, AND VIRTUAL DIR
ECTORIES

.........................

5

Task 1


Defining Multiple Sites in a Web Role Using the Service Model

................................
............

6

Task 2


Creatin
g Virtual Applications and Virtual Directories

................................
...........................

13

EXERCISE 2: USING ST
ART
-
UP TASKS TO REGISTER

A COM COMPONENT
................................
..

16

Task 1


Registering a COM Component

................................
................................
.............................

17

EXERCISE 3: USING ST
ART
-
UP TASKS TO INSTALL
PHP WITH THE WEB PLA
TFORM INSTALLER

................................
................................
................................
................................
................................
....

25

Task 1



Installing PHP with the Web Platform installer

................................
................................
.....

25

SUMMARY

................................
................................
................................
................................
..................

30



Page |
3



Overview


When Windows Azure was first released, there were a few, but significant restrict
ions in the
programming model.
Things like Full Trust, Administrative Access, and the full IIS feature
-
set were
initially re
stricted 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 f
ull 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 exercis
e your administrative control.
You can
b
ootstrap 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 a
s 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
h
ands
-
o
n
l
ab, you will learn how to:



Use a
dvanced service model features that enable hosting a Web role in IIS
.



Host multiple site
s

in a
Web
role

and bind them
to the same endpoint using host headers
.



Create virtual applications and map them to selected sites
.



Utilize u
ser virtual directories to s
hare common content between sites
.



Set up the role environment by executing start
-
up tasks to register a COM
.



Install complex components

required by a web role
, such as a
scripting
language's binary

files.



Page |
4



Note:

This lab

shows advanced feature
s

of Web and Worker roles in Windows Azure;
it
assume
s

that
you have sufficient knowledge

of Windows Azure
.

If you are beginner
i
n 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



Mic
rosoft Visual C++ 2010

(
r
equired for
E
xercise
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’s

Source

folder.

2.

Double
-
click the
Setup.cmd

file in this fold
er 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 Vi
sual 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.

Page |
5



Exercises

This
h
ands
-
o
n l
ab
includes

the following exercises:



Registering

Sites, Applications, and Virtual Directories



Using Start
-
Up Tasks to
Register a COM Component



Using
Start
-
Up Tasks to
I
n
stall 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 Vir
tual Directories

When hosted in
IIS Hosted Web Core (HWC)
, Windows Azure Web roles can

support a single application
bound to no more than a single HTTP and a single HTTPS end
point. 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 m
odel, as shown in the figure below.


Figure
1

Page |
6



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 eac
h 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.


Tas
k 1



Defining Multiple Sites in a Web
Role Using the Service Model

Page |
7



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 a
nd
use host headers to isolate them.

Note:

This task
requires the

hosts

file to be updated.

If you did not execute the setup instruction for
this lab
, this exercise will not work.

P
roceed with the setup instructions before starting with this task.


1.

Start M
icrosoft Visual Studio 2010 as an administrator.

2.

Open the
Begin

solution located in
Ex
1
-
FullIIS

in the
Source

folder of the lab.

3.

In the
Windows Azure
project, open the service model file,
ServiceDefinition.cs
def
. 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
physi
calDirectory

attribute to this
element that points to

"
..
\
SampleWeb
A
pp
"
.

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.


5.

Now, locate the single
Binding

element for the
Fabrikam

site and add a
hostHeader

attribute
with a value of
"
www.fabrikam.com
"
. The updated site definition should appear as shown in the
following figure.


Figure
3

Page |
8



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
www.fabrikam.com
.


6.

Next, define a new site for
Co
ntoso

by inserting the following (highlighted) definition inside the
Sites

element.

(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex1
-
01
-
ContosoSite
-
XML
)

XML

<
WebRole

name
=
"
SampleWebApp
"
>


<
Sites
>


<
Site

name
=
"
Fabrikam
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
Bindings
>


<
Binding

name
=
"
HttpIn
"

endpointName
=
"
HttpIn
"

hostHeader
=
"
www.fabrikam.com
"

/>


</
Bindings
>


</
Site
>


<
Site

name
=
"
Contoso
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
Bindings
>


<
Binding

name
=
"
HttpIn
"



endpointName
=
"
HttpIn
"



hostHeader
=
"
www.contoso.com
"

/>


</
Bindings
>


</
Site
>


</
Sites
>


...

</
WebRole
>


7.

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

(
Code Snippet


AdvancedWebAndWorkerRoles
-
Ex1
-
0
2
-
Litware
Site
-
XML
)

XML

<
WebRole

name
=
"
SampleWebApp
"
>


<
Sites
>


<
Site

name
=
"
Fabrikam
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
Bindings
>


<
Binding

name
=
"
HttpIn
"



endpointName
=
"
HttpIn
"



hostHeader
=
"
www.fabrikam.com
"

/>


</
Bindings
>


</
Site
>


<
Site

name
=
"
Contoso
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>

Page |
9




<
Bindings
>


<
Binding

name
=
"
HttpIn
"



endpointName
=
"
HttpIn
"



hostHeader
=
"
www.
contoso
.com
"

/>


</
Bindings
>


</
Site
>


<
Site

name
=
"
Litware
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
Bindings
>


<
Binding

name
=
"
HttpIn
"



endpointName
=
"
HttpIn
"



hostHeader
=
"
www.
litware
.com
"

/>


</
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.


8.

Press

F5

to build and run

the
Windows Azure

p
roject
. 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.


9.

Start Internet Information Services (IIS) Manager.

10.

Browse to the
Sites

node and
notice that

three new sites were created for
Fabrikam
,
Contoso

and
Litware
. T
he name of the site is
derived from
the
Windows Azure
deployment ID
, the Web
role name, the instance ID,

and the name of the s
ite
in

the service model file.

Page |
10




Figure
4

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


11.

In the (IIS)

Manager, click Contoso node and take note of the
port

where it is hosted. You will
find

the

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


12.

Now, select the
Application Pools

node and review the pools created to support the
applications hos
ted by the Web role.

Page |
11




Figure
6

Application pools defined for the Web role sites


13.

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. F
or
example, if the browser is currently showing

http://127.0.0.1:81
, set the address to
http://www.contoso.com:82
.

Note:

The setup procedure updated the hosts file, adding three new

entries for
www.contoso.com
,
www.fabrikam.com
, and
www.litware.com
, all pointing to the loopback
address (127.
255
.0.
0
).


14.

Notice that the
title shown in the home page is “My ASP.NET Application”.

15.

In Visual Studio, o
pen the
Site.M
aster

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
.

R
eplace its
content, which should be “My ASP.NET Application”, with the following
(highlighted)
expression.

(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex1
-
03
-
SampleWebAppTitle
-
HTML
)

HTML

...

<
body
>


<
form

runat
="server">


<
div

class
="page">


<
div

class
=
"header">


<
div

class
="title">


<
h1
>

Page |
12




<%
=

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



</
h1
>


</
div
>


<
div

class
="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.


16.

Save the updated
file.

17.

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)
,

change
s only be
come

effective after redeploying

the
service
.


18.

Open a new browser window and navigate to
www.litware.com
. Notice that the W
eb site title
in the new window

is
Litware
.

Similarly, open a third browser window and navigate to
www.fabrikam.com
.

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


Page |
13




Figure
7

Multiple sites hosted in the Web role


Note:

In this particular case, a
ll 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.


19.

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.

Page |
14



1.

In Visual Studio, a
dd 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

definiti
on, as
shown (highlighted) below.

(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex1
-
04
-
ContosoVirtualApplicationDefinition
-
XML
)

XML

<
WebRole

name
=
"
SampleWebApp
"
>


<
Sites
>


<
Site

name
=
"
Fabrikam
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


...


</
Site
>


<
Site

name
=
"
Contoso
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
VirtualApplication

name
=
"
CRM
"


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


<
VirtualDirectory

name
=
"
Scripts
"


physicalDirectory
=
"
..
\
SampleWebApp
\
Scripts
"

/>


<
VirtualDirectory

name
=
"
Styles
"


physicalDirectory
=
"
..
\
SampleWebApp
\
Styles
"

/>


</
VirtualApplication
>


...


</
Site
>


<
Site

name
=
"
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
h
ttp://www.contoso.com/CRM
.

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 t
he main site.


4.

Similarly, enable the CRM application for the
Litware

site by inserting the following (highlighted)
virtual application definition.

Page |
15



(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex1
-
05
-
LitwareVirtualApplicationDefinition
-
XML
)

XML

<
WebRole

name
=
"
SampleWebApp
"
>


<
Sites
>


<
Site

name
=
"
Fabrikam
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


...


</
Site
>


<
Site

name
=
"
Contoso
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


...


</
Site
>


<
Site

name
=
"
Litware
"

physicalDirectory
=
"
..
\
SampleWebApp
"
>


<
VirtualApplication

name
=
"
CRM
"


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


<
VirtualDirectory

name
=
"
Scripts
"


physicalDirectory
=
"
..
\
SampleWebApp
\
Scripts
"

/>


<
VirtualDirectory

name
=
"
Styles
"


physicalDirectory
=
"
..
\
Sample
Web
App
\
Styles
"

/>


</
VirtualApplication
>


...


</
Site
>


</
Sites
>


...

</
WebRole
>


5.

Press
F5

to build and launch the application in the
compute emulator
.

6.

In the browser window, navigate to
http://www.contoso.com
, making sure that you specify the
port number for the running deployment, if this is different from the standard HTTP port (80).

7.

Now, navigate to
http://www.contoso.com/CRM

to view the CRM application running in the
Contoso

site.

Page |
16




Figure
8

CRM virtual application mapped to the Contoso site


8.

Repeat the previous step, only this time visit the sa
me CRM application running in the
Litware

site.

9.

Close the browser window.



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

A start
-
up task is a command, either an executable or a script, executed prior to the start of a role
instance. The
command can perform set up tasks that are required to prepare the environment for the
application, such as installing applications, registering COM components, configuring IIS settings, or
registering performance counters.

In this exercise, you explore the

use of start
-
up tasks to configure the environment where a service
executes. To do this, you will use a sample Web application that requires a COM component to work.


Page |
17



Task 1


Registering a COM Component

Many applications today still rely on functionality

provided by “legacy” COM components. Moving these
applications to the cloud requires that each virtual machine instance hosting the application have the
necessary components installed and registered. Registration needs to be carried out upon role start up

and requires administrative privileges.

In this task, you create a start
-
up task that registers the COM component required by the sample
application.

Important
:
Make sure that you have launched the dependency checker to set up the lab before
starting this task. The setup procedure builds the COM component required by the solution from
source code in the
Assets

folder. Note that you need to have Visual C++ installe
d for this purpose.

In addition, to use the COM component, the
identity of the process instantiating
it

must have
access
to
the directory

where the component is installed.
To ensure this,
a
void

launching the
service

from a
location inside your user profile

folder and instead, copy the project files to a folder with unrestricted
permissions, for example,
to

a folder within the default installation location of the training kit,
C:
\
WAPTK.


1.

Start Microsoft Visual Studio 2010 as an administrator.

2.

Open the
Begin

solution located in
Ex
2
-
StartupTasks

in the
Source

folder of the lab. The
solution contains a
Windows Azure

project and a Web role that makes use of the
LegacyCOM

component.

Note:

The
LegacyCOM

project, located in the
Assets

folder of the lab, implements
a very
simple COM library using Active Template Library (ATL). The library contains a single
component class with a method that receives a name parameter and returns a greeting
message.


3.

Press
F5

to build and run the application. During the build process
in Visual Studio, a pre
-
build
event registers the COM component. A message box should notify you when the registration
succeeds. Click
OK

to proceed.


Figure
9

Page |
18



Successful COM component registration


Note:
Normally, the Visual C++ development environment takes care of the registration. Here,
the automatic registration in the
LegacyCOM

project has been intentionally disabled and
instead, a post
-
build event configured to handle this requirement and to produce
an explicit
message to notify you when this occurs. Later in this task, you will see that the component,
which only needs to be registered to build the solution, will be unregistered by a post
-
build
step.


4.

Wait for the application to launch in the
Compute

Emulator
and for the Home page to open in
your browser. Then, enter your name and then click
Greet me
. Notice that the application
responds with a greeting message generated by the COM component.


Figure
10

Running application sho
wing the output from the COM component


5.

Close the browser wi
ndow to stop debugging and shut

down the application.

Page |
19



6.

In the
Build Events

tab of the
SampleWebApp

project
’s properties
, add a post
-
build event

to
unregister the COM component after the Web applica
tion is built. Use the following command
line.

Post
-
build event command line

IF DEFINED PROCESSOR_ARCHITEW6432 (SET DEV_PLATFORM=%PROCESSOR_ARCHITEW6432%)
ELSE (SET DEV_PLATFORM=%PROCESSOR_ARCHITECTURE%)

regsvr32.exe
/u
"$(ProjectDir
)
\
%DEV_PLATFORM%
\
LegacyCOM.dll"


Note:

To build the Web application, the component must be registered on the development
machine. However, because you are using the same machine to launch the application in the
compute emulator
, you need to ensure that the

COM component is not registered when you
test the
startup

task; otherwise, you will be unable to determine whether the task succeeded.


7.

You will now execute the application without the required COM registration. Press
F5

to build
and run the application in the
compute emulator
. Notice that when the solution builds, you first
receive a notification that the COM component has been registered, and then, after the build
completes, a second notification indicating that the com
ponent has been unregistered. Click
OK

to dismiss both message boxes.


Figure
11

Successful unregistration of the COM component after the build completes


8.

Wait for the application to launch in the
compute emulator

and for the Home

page to open in
your browser. In the browser window, enter your name and click
Greet me
. Notice that after
submitting the form, an exception is raised and execution halts in the debugger.

Page |
20




Figure
12

COMException raised when the
COM component is not registered


9.

Press
F5

to continue execution and allow the exception to be handled by ASP.NET.

Page |
21




Figure
13

ASP.NET unhandled exception handler


10.

Close the browser window to stop debugging and shut down the applica
tion.

11.

Next, define a
startup

task to set up the role and register the COM component. To do this, open
the
ServiceDefinition.csdef

file in the
Windows Azure
project, locate the
WebRole

element in
the service model and inside it, insert a
Startup

element with a single task, as shown below.


(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex2
-
01
-
StartUpTask
-
XML
)

XML

<
ServiceDefinition

name
=
"
CloudService
"

xmlns
=
"
http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition
"
>


<
WebRole

name
=
"
Sampl
eWebApp
"
>


<
Sites
>


<
Site

name
=
"
Web
"
>


<
Bindings
>


<
Binding

name
=
"
HttpIn
"

endpointName
=
"
HttpIn
"

/>


</
Bindings
>

Page |
22




</
Site
>


</
Sites
>


<
ConfigurationSettings
>


</
ConfigurationSettings
>


<
Endpoints
>


<
InputEndpoint

name
=
"
HttpIn
"

protocol
=
"
http
"

port
=
"
80
"

/>


</
Endpoints
>


<
Imports
>


<
Import

moduleName
=
"
Diagnostics
"

/>


</
Imports
>


<
Startup
>


<
Task

commandLine
=
"
Register.
cmd
"

executionContext
=
"
elevated
"

taskType
=
"
simple
"

/>


</
Startup
>


</
WebRole
>

</
ServiceDefinition
>


Note:
The
Startup

element contains one or more
Tasks

that the role executes dur
ing its
bootstrapping process.
Each task includes a
commandLine

parameter that defines a program
or a batch file to execute.

Tasks can specify an
executionContext

that allows them to run in the same security context as
the role or with administrative privileges. Because registering a COM component requires
writing information to a registry hive that is not accessible to regular
users, the registration
task uses an elevated execution context.

Additionally, tasks can specify a type that determines how they are scheduled. The following
types are available:

Simple:

the
startup

process launches the task and does not proceed until the

task completes.

Background:

the
startup

process launches the task in the background and then continues.

Foreground:

similar to a background task, but the role cannot shut down until all the
foreground tasks have ended.


12.

Create a new text file in the root of the
SampleWebApp

project and name it
Register.
cmd
. This
file will contain a script to register the COM component.

13.

Add the following content to the
Register.
cmd

script.

Command

echo off

regsvr32.exe
/s

"%~dp0%
PROCESSOR
_ARCHITECTURE
%
\
LegacyCOM.dll"


Page |
23



Note:
The script shown above registers the COM component using the
regsvr32.exe

utility that
is normally present in any Windows distribution and is available in the Windows Azure Guest
OS.

It registers the version of the comp
onent appropriate to the platform where the role is
running. Notice that the path that specifies the component to register uses the
%PROCESSOR_ARCHITECTURE% environment variable to select the folder that matches the
current platform, either
amd64

for 64
-
bi
t systems or
x86

for 32
-
bit systems. This allows you to
run the same script locally, during development, if you are working on a 32
-
bit OS, or in the
cloud, when you deploy the application to the 64
-
bit Windows Azure environment. Note that
the lab setup pr
ocedure builds the COM component for both platforms.

Normally, during registration, the
regsvr32.exe

utility produces a message box that requires
confirmation. When hosted in the
compute emulator
, you can close the dialog and proceed
with the
startup

process. However, when deploying to Windows Azure, if the task stops to wait
for user input, it will block, causing the role to remain in a busy state and never start. For this
reason, the registration is performed in silent mode by appending an
/s

parame
ter to the
command line.


14.

Set the
Copy to Output Directory

property of the
script file
to
Copy always

and

make sure that
its

Build Action

property is set to
None
.

Page |
24




Figure
14

Copying the startup script to the
output directory


15.

Once

again, press
F5

to build and run the application in the
compute emulator.


16.

Wait for the application to launch in the
Compute Emulator
and for the Home page to open in
your browser. Then, enter your name and then click
Greet me
. Notice that the application

responds with a greeting message confirming that the start
-
up task successfully registered the
COM component.

17.

Close the browser window.

18.

Optionally, you may want to deploy the service package to Windows Azure and test the COM
registration process in the cl
oud.

Page |
25



Note:
For this lab, you have access to the source code for the COM component and are able to
build and deploy the 64
-
bit version when deploying to Windows Azure. If you need to register
a COM

component for which you do not have source code, be aware that 32
-
bit components
cannot be accessed directly by a 64
-
bit role process, and instead need to be launched in a
separate surrogate process.



Exercise 3: Using Start
-
Up tasks to install
PHP
with

t
he Web Platform Installer

Applications deployed to the cloud usually have a set of prerequisites that must be installed on the host
computer to be able to work correctly. In this kind of scenario, having the capabilities of the
Microsoft
Web Platform I
nstaller

(Web PI) at your service can be very handy.

In this exercise, you
explore

another possible use for

start
-
up tasks

in conjunction with the Web
Platform Installer
. You will learn how to install complex components such as
the binary files for
a
new
s
cripting
language.
To do this, you will
create a simple PHP page,
requiring

that
the
PHP
script processor
be installed
in order
to work
.


Task 1


Installing PHP
with

the Web Platform installer

In this task, you will use
multiple

start
-
up tasks to install
the Web Platform Installer and then install PHP
using the Web PI.

1.

Start Microsoft Visual Studio 2010 as an administrator.

2.

Open the
Begin

solution located in
Ex
3
-
InstallPHP

in the
Source

folder of the lab. The solution
contains a
Windows Azure
project and a

Web role to which you will add a PHP page.

3.

Add a new PHP page at the root of the
PHPWebRole

project named
info
.
php
.

To do this, you
can select the
Text

File

template in the
Add

New

Item

dialog and type
info.php

in the
Name

field.

Page |
26




Figure
15


Creating the info.php page


4.

Add the following code to the
info
.
php

file.

PHP

<?
php



phpinfo();

?>


5.

Now, o
pen the
Web.config

file
, locate the
system.webServer

element, and then insert the
following (highlighted)
defaultDocument

element
to
set the

default
document
as

info.php
.

(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex3
-
0
1
-
DefaultDocument
-
XML
)

XML

<
configuration
>


...


<
system.webServer
>


<
modules

runAllManagedModulesForAllRequests
=
"
true
"
/>


<
defaultDocument
>


<
files
>


<
add

value
=
"
info.php
"

/>

Page |
27




</
files
>


</
defaultDocument
>


</
system.webServer
>

</
configuration
>


6.

Set the
InstallPHP

project as the solution's start
-
up project.

7.

Press
F5

to launch the Web Role and wait for the web browser window to appear, which will
indicate that the role has been initialized. As PHP is not
installed,

you will receive an error
message like the one shown in the following figure.

If the default document is

not shown by
default, try browsing to
info.php
.


Figure
16

Error when PHP is not installed


8.

Close the web browser window.

9.

In
Solution Explorer
, double
-
click
InstallPHP.cmd

in the
PHPWebRole

project to examine this
script file in
the editor.

Page |
28




Figure
17

Startup script to install PHP using the Web Platform Installer


Note:

The
InstallPHP.cmd

script installs the most recent version of PHP using the
Web
Platform Installer command line tool
. This tool simplifies the installation of the latest
components of the Microsoft Web Platform, such as IIS, PHP, and SQL Server Express, among
othe
rs, and
enables you to automate the application and service installation
of a
Windows
Azure

role.

The script first enables the Windows Update service to allow the Web Platform Installer to use
it for downloading components required by the PHP installation.

Next, it launches the Web PI
command line tool to install PHP. Once the installation is complete, it stops the Windows
Update service and restores its startup mode to disabled.


Important
:

All startup script files have their
Build

Action

set to
None

and
Copy to Output
Directory

property set to
Copy always
.


10.

In Visual Studio, open the
ServiceDefinition.csdef

file in the
InstallPHP

Web role project.

11.

Add the (highlighted)
Startup

configuration elements in the following code snippet, below the
ConfigurationS
ettings

element closing tag. In general, this section defines startup tasks and, in
this case, includes the script required to install PHP on a Windows Azure host.

(Code Snippet


AdvancedWebAndWorkerRoles
-
Ex
3
-
0
2
-
StartUpTask
s
-
XML
)

XML

...

<
WebRole

name
=
"
PHPWebRole
"
>


<
Sites
>


...

Page |
29




</
Sites
>


<
ConfigurationSettings
>


...


</
ConfigurationSettings
>


<
Startup
>


<
Task

commandLine
=
"
InstallPHP.cmd
"

executionContext
=
"
elevated
"

taskType
=
"
simple
"

/>


</
Startup
>


<
Endpoints
>


...


</
Endpoints
>


<
Imports
>


...


</
Imports
>

</
WebRole
>

...


12.

Press
F5

to launch the
Web
r
ole

in the
compute emulator
.


13.

Wait for the role to start and show its default page. While the
startup

tasks execute to install
PHP, Visual Studio may warn you one or more times that the role is taking longer than expected
to start. If this happens, click
Yes

to continue waiting.


Figure
18

Visual Studio warning during the PHP ins
tallation


Note:
Running
the start
-
up task

may take

some time. Before
continuing

with the following
steps, you should make sure PHP has been installed. You can check
this

in

the
Programs

and

Features

list that can be accessed through the
Control

Panel
.


14.

Once the role starts successfully, a

page similar to the one shown in the following figure is
displayed, confirming that PHP was installed

correctly
.

Page |
30




Figure
19

Confirm
ing

that
the
PHP installation was successful



Summary

In this
hands
-
on lab,
you
reviewed some

advanced Windows Azure service model features.
You saw how
to enable hosting a Web role in Internet Information Server (IIS) to access its full set of features. By
enabling IIS, you were able to host multiple sites i
n a single Web role and create virtual applications and
directories. Additionally, you
explored
start
-
up tasks that you can use to prepare the
role
environment
,
and how to use them to register a COM component
,

as well as
install
ing

entire binary file sets
for a
scripting
language
.