Thursday, February 9, 2012

Hosting multiple virtual applications in Azure Web Role

Web role can be used for hosting different virtual application. The web role supports running a virtual application within a site.
Same web site project added in solution can be used to host as a multiple virtual application within web role. Service definition file in Azure Cloud service project is used for this purpose. The <sites> element in ServiceDefinition.csdef file helps us to host multiple virtual applications within a web role application. Following is the description how you can achieve it.
Open VS2010 and create Azure Cloud Service Project. Add a simple web role in it. Once done, right click on solution and click on “Add” to add another new project in the same solution. So the final solution structure in VS2010 will be as shown below. Here I have added another web application within my cloud service solution. You can add a simple web site also instead of web application.

Now when you run web role in development environment; web site gets created in IIS as shown below. This means I need to add virtual directory within the web site that gets created in IIS for a web role.

To create a virtual application under web role, we need to add <virtualApplication> tag under <sites> tag in ServiceDefinition.csdef file as shown below –

Also we need to know, which application we are running therefore I added following code in master page file of VirtualApp project under <h1> tag. Here I am trying to create 3 virtual application in web role application.

Now I run my project by pressing F5. I making my web role project run on port 80. Therefore the web role URL I got here is - http://127.0.0.1/Default.aspx
If you are running on port 81 the URL for your web role will be - http://127.0.0.1:81/Default.aspx. Similarly URL will change depending on port being used.
Now I can access virtual application 1 by running URL - http://127.0.0.1/virtualapp/
Similarly other virtual applications can be accessed as - http://127.0.0.1/virtualapp2/ and http://127.0.0.1/virtualapp3/ . Screenshot below shows VirtualApp2

IIS structure will be as follows -


Now if you deploy this project on Azure management portal then the URL of virtual applications will be as follows –
Hope this helps.
Cheers…
Happy Virtualization!!!

No comments:

Post a Comment