Sunday 25 March 2012

Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role - Part 1

Introduction:

In the initial days of Azure, the only possibility to host our application in Azure environment is under IIS Hosted Web Core. HWC is a low-level component which is used for running Web applications without WAS (Windows Process Activation Service). HWS allows us to run under only one application pool with only one web site.

So this makes user feel uncomfortable while publishing Web applications in Azure. So, Azure SDK v1.3 introduces running web role under WAS, which allows us running multiple web sites, web applications and virtual directories using IIS.

In this way, we are able to publish multiple sites in a single Web Role and the cost we are paying also going to get reduce (if we can accommodate multiple site in a single web role) instead of publishing separate Web role for each sites.

WAS manages application pool configuration and the creation and lifetime of worker processes for HTTP and other protocol.

Before going for actual implementation, we will understand about building blocks of Web Server.

A Web Server has three important blocks which works together to provide the information to the user across intranet or intranet or extranet. Each resource can be accessible thro’ an URI.

  1. Sites
    A Web Server contains one or more Web Site, which is a top hierarchy. By default the Default Web Site will present while installing IIS. The Web Site can be accessible through one or more uri based on the bindings.
  2. Applications
    Web application is a program that runs under an application pool which delivers web content such as text, images etc., and can be accessible thro’ an uri based on a binding (For ex: when we bind http, it is accessible using http://WebSiteName/ApplicationName).
  3. Virtual Directories
    A virtual directory can be created under web site which refers a physical directory path. The files under the virtual directory (i.e., the files under physical directory referred) can be referred thro’ an uri. The files under the sub directories can also be accessible by providing the directory name after the virtual directory name (For Ex: http://WebSiteName/VirtualDirectoryName/filename and http://WebSiteName/VirtualDirectoryName/SubDirecoryName/filename)

    In IIS 7, each application must have a virtual directory, which is named the root virtual directory, and which maps the application to the physical directory that contains the application's content. However, an application can have more than one virtual directory.

Below image shows the same –



For more information, pls refer below urls –
http://technet.microsoft.com/en-us/library/cc754437(WS.10).aspx
http://technet.microsoft.com/en-us/library/cc735238(WS.10).aspx
http://msdn.microsoft.com/en-us/library/ms734677.aspx
http://blogs.msdn.com/b/wenlong/archive/2006/11/22/virtual-application-vs-virtual-directory.aspx

With this keep in mind, let us go for implementation.

Running Sub Sites and Sub Application in a Single Web Role

Creating the Project

Step 1: Create a Cloud Project in Visual Studio.
Step 2: As we are running multiple sites in a single role, we will keep a sample message in the Default.aspx page for understanding which site is running.
Open the Default.aspx and modify the page content to Web Role Application.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    Web Role Application
</asp:Content>
Step 3: Run the application and verify the content.

Adding a New Sub site

Step 1: Now we are going to add one sub site into the Web Role. So right click the solution select Add -> New Web Site.
Step 2: Visual Studio will open Add New Web Site window. Select the location where the solution is. The location of the project can be anywhere in the system, but better to keep it in the solution itself.

If we select the location in the solution folder, it can be referred as relative path otherwise we required to refer as physical path only.

Below is the file location


Step 3: For identifying the site, open the Default.aspx page and add a message Web Site 1.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    Web Site 1
</asp:Content>
Step 4: Now we required to modify the configuration file for accessing the sub site. So open the ServiceDefinition.csdef file and add a VirtualApplication under Site node.
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MultiWebApp" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole" vmsize="Small">
    <Sites>
      <Site name="Web">
        <VirtualApplication name="SubSite1" physicalDirectory="../SubSite1" />
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
  </WebRole>
</ServiceDefinition>
If we have location which is difficult to mention as relative url, can be referred as physical path.
<VirtualApplication name="SubSite1" physicalDirectory="D:\Blog\MultiWebApp\SubSite1" />
Step 5: Now you run the application and verify the output. By default the browser will open the Web Role project url.

Step 6: You can access the sub sites by adding the sub site name which is configured in the configuration file (here SubSite1) in the end of the url.

Adding a Sub Application

Step 1: Now we are going to add a new web application into the project. Right click the solution and select Add -> New Project. Provide the application name with the location of the web role.
Step 2: For identifying the site, add a message in the Default.aspx page such like Sub Web Application.
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    Sub Web Application
</asp:Content>
Step 3: We have added a new web application in to the solution, so we required to configure the cloud project to be able to access from the browser. So open the ServiceDefinition.csdef file and add another VirtualApplication node under Site node.
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MultiWebApp" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole" vmsize="Small">
    <Sites>
      <Site name="Web">
        <VirtualApplication name="SubSite1" physicalDirectory="../SubSite1" />
        <VirtualApplication name="SubApplication" physicalDirectory="../SubApplication" />
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
  </WebRole>
</ServiceDefinition>
Step 4: We have completed our configuration. Run the project to test the application.
Step 5: The browser will open the url of the main application of the web role.
Step 6: To access the sub application added newly add the sub application name (configured in the configuration file) in the url.
Step 7: You can try accessing the sub site name also in which we added previously by the url (http://127.0.0.1:82/SubSite1/) as accessed before.

Adding a Sub Site under a Sub Site added

Step 1: We are added a sub site before. Now we are planning to add another sub site for a sub site already added. So we can able to access the sub site as http://127.0.0.1:82/SubSite1/NewSubSite
Step 2: To do that, right click the solution and select Add -> New Web Site. Select the location of the cloud project.
Step 3: As we did before, add a message Sub Site of Sub Site 1 in the Default.aspx page to identify.
Step 4: Now we need to modify the configuration. So open the ServiceDefinition.csdef file add a VirtualApplication node under the VirtualApplication node for the sub site.
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="MultiWebApp" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition">
  <WebRole name="WebRole" vmsize="Small">
    <Sites>
      <Site name="Web">
        <VirtualApplication name="SubSite1" physicalDirectory="../SubSite1" >
          <VirtualApplication name="SubSite2" physicalDirectory="../SubSite2" />
        </VirtualApplication>
        <VirtualApplication name="SubApplication" physicalDirectory="../SubApplication" />
        <Bindings>
          <Binding name="Endpoint1" endpointName="Endpoint1" />
        </Bindings>
      </Site>
    </Sites>
    <Endpoints>
      <InputEndpoint name="Endpoint1" protocol="http" port="80" />
    </Endpoints>
  </WebRole>
</ServiceDefinition>
Step 5: Now we are ready for testing our application. So run the application to open the main web role url in the browser.
Step 6: To access the sub site added newly add the SubSite1/SiteSite2 at the end of the url in the browser.

Now we have developed our application by configuring multiple sub sites. To publish this application to cloud, you can use wither publish method or create the package then upload to Azure or any other methods.

To access the sites, use the same method we use before. That is after the cloud url like xyz.cloudapp.net add the sub site names followed by /. Below are the screen shot I published.




In the next post, we will see how to configure for multiple virtual directories in a single Web Role.

The other links on Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role:
  1. Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role - Part 1
  2. Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role - Part 2
  3. Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role - Part 3


4 Responses to “Publishing Multiple Sites, Sub-Sites & Virtual Directories in a single Azure Web Role - Part 1”

  • Anonymous says:
    2 July 2012 at 21:06

    Thanks for this very clear explanation. Just what I needed.

  • Unknown says:
    28 October 2012 at 02:49

    This seems to work on localhost, but i am not sure how this and part 2 works when publishing to azure. When i create my packaged or publishes - is there a way to tell it that it should packaged both sites?

    (Hope my question makes sense).

  • Thirumalai M says:
    28 October 2012 at 10:19

    Hi, The examples shows here is all works fine with Azure. I had keep screenshot of that also.

    Regarding on packaging, (Hope I understand your question) When packaging the webrole, it will package all together (sub website, sub applications) added in that WebRole. Incase if we need to publish only one at a time, web deploy can be used.

  • Help says:
    19 June 2014 at 16:15

    This is the screen shot my project not working,

    http://s4.postimg.org/agn88wkr1/image.png

    In my project One WCFService,2 ASP.Net webprojects
    I can configured same way but not working
    please help me

Post a Comment