Saturday, May 28, 2011

Multiple Sites in a single Cloud Project: Using Virtual Directories

The third way to host multiple websites in a single Windows Azure deployment is similar to the one using virtual applications. While virtual applications allow to run ASP.NET web applications virtual directories aim at less dynamic content.

So you could use a virtual directory to store resources like icons, pictures or styles that are shared by multiple websites. Instead of copying those resources to each website, they access them via virtual directories.

According to virtual applications, in this example the main website should be accessible at

http://myproject.cloudapp.net and the resources at
http://myproject.cloudapp.net/virtualdirectory

Create a new Windows Azure Cloud Project and add a ASP.NET WebRole to this project. After that, create a new WebSite (not a web application) in the solution folder or add an existing one to the solution. The solution explorer should look like this:

image

For this example, I created a new WebSite named “VirtualDirectory” in the solution folder and added a picture “circle.png” in a folder named “Images”.

Now open the ServiceDefinition.csdef file and add to the XML element <WebRole> inside its default <Site> tag a <VirtualDirectory> element. This element has two attributes 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 the resources to be accessible at http://myproject.cloudapp.net/virtualdirectory, we need to set the name attribute to “virtualdirectory”.
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 <VirtualDirectory> tag the ServiceDefinition-file looks like this:

  1: <ServiceDefinition name="MultiSitesByVirtualDirectory" xmlns="...">
  2:   <WebRole name="WebRole1">
  3:     <Sites>
  4:       <Site name="Web">
  5:         <VirtualDirectory name="virtualdirecotry" physicalDirectory="../VirtualDirectory" />
  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 here: MultiSitesByVirtualDirectory.zip

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

Saturday, May 14, 2011

Multiple Sites in a single Cloud Project: Using Ports

One of the most frequently asked questions I get is: “How can I manage to run multiple websites in a single Windows Azure deployment?” And the answer is that there are multiple ways to achieve this goal. So this blog post and the next upcoming three posts will show four ways to host multiple websites.

The easiest way is to create two WebRoles and to make them accessible via different endpoints using different ports. The result in the browser address bar would look like this:

http://myproject.cloudapp.net for the first WebRole
http://myproject.cloudapp.net:8080 for the second WebRole

It’s important to know that you can access a WebRole by more than one endpoints but you cannot access multiple WebRoles by the same endpoint!

The step by step walkthrough is simple: When creating a new Windows Azure Cloud Project, add two WebRoles to the new project.

CreateNewCloudProject

Your solution explorer should look like this after creating the cloud solution:

image

And in this case Visual Studio already did all the work for you. When looking at the WebRoles’ properties pages you can see in the “Endpoints” tab that WebRole1 has an Endpoint mapped to port 80 and WebRole2 has an Endpoint mapped to port 8080.

PortWebRole1 for WebRole1PortWebRole2 for WebRole2

Of course, you may change the Public Port settings to whatever port number serves you best.

You might want to have a look at the CloudProject’s ServiceDefinition-file. This file becomes important in the other ways solving the multiple sites issue.
Inside the definition file there is a <WebRole> block for each web role you created in the project. This block contains in the <Endpoints> child the information about how the role can be accessed. You can find your port settings there in the XML attribute port.

  1: <ServiceDefinition name="MultiSitesByPort" xmlns="...">
  2:   <WebRole name="WebRole1">
  3:    ...
  4:     <Endpoints>
  5:       <InputEndpoint name="Endpoint1" protocol="http" port="80" />
  6:     </Endpoints>
  7:     ...
  8:   </WebRole>
  9: 
 10:   <WebRole name="WebRole2">
 11:    ...
 12:     <Endpoints>
 13:       <InputEndpoint name="Endpoint1" protocol="http" port="8080" />
 14:     </Endpoints>
 15:    ...
 16:   </WebRole>
 17: </ServiceDefinition>