Saturday, May 21, 2011

Multiple Sites in a single Cloud Project: Using Virtual Applications

Another very simple way to host multiple websites in a single Windows Azure deployment is to integrate a website into an existing WebRole as a virtual application. You might want to chose this way when you need to quickly integrate an existing website to your cloud deployment.

Virtual applications have the same “look and feel” as known from on premise IIS applications. In this example we are going to publish a WebRole at

http://myproject.cloudapp.net and integrate another website at
http://myproject.cloudapp.net/application

At first, create a new Windows Azure Cloud Project in Visual Studio and add a ASP.NET WebRole to this project. Your solution explorer should then look like this:VirtualApplicationSolution

After that you either chose to add a new Web Project to that solution or add an existing one. In the example I created a new ASP.NET Web application inside the Solution folder. This results in the solution folder looking like this:

VirtualApplicationSolution2

This time connecting the second website to an endpoint cannot be done in the WebRole’s properties page but needs to be set manually in the ServiceDefinition.csdef file.

By default inside the service definition there is a single <WebRole> element containing a single <Site> named “Web”. Within this <Site> tag we can add a <VirtualApplication> element. This element has two attributes that need to be set: name and physicalDirectory.
The name attribute contains the string that needs to be appended to the root URL of our website to access the second website within the WebRole. So if we want our second website to be accessible at http://myproject.cloudapp.net/application, we need to set the name attribute to “application”.
The physicalDirectory attribute needs to contain the path to where Visual Studio can find the files of this website to publish it to the cloud. This path can either be set as an absolute path or as an relative one. A relative path must be set relative to the ServiceDefinition.csdef file.

After adding the <VirtualApplication> tag the ServiceDefinition-file looks like this.

  1: <ServiceDefinition name="MultiSitesByVirtualApplication" xmlns="...">
  2:   <WebRole name="WebRole1">
  3:     <Sites>
  4:       <Site name="Web">
  5:         <VirtualApplication name="application" physicalDirectory="../WebApplication1" />
  6:         <Bindings>
  7:           <Binding name="Endpoint1" endpointName="Endpoint1" />
  8:         </Bindings>
  9:       </Site>
 10:     </Sites>
 11:     <Endpoints>
 12:       <InputEndpoint name="Endpoint1" protocol="http" port="80" />
 13:     </Endpoints>
 14:     ...
 15:   </WebRole>
 16: </ServiceDefinition>

You can download the source of this example project here: MultiSitesByVirtualApplication.zip

No comments:

Post a Comment