Running multiple sites in one Windows Azure Web Role

Since the release of the Windows Azure SDK 1.3 it is possible to host multiple sites in one web role. In this blog post I will show you how to do this.

1. Creating the Azure project

Start by creating a new Azure Cloud project. Add a single WebRole project (call it MultiSitesWebRole) to it:

image

Hit Ok.

Now we just want to make sure we see each different web site, so open the default.aspx page and change the header, for example:

Code Snippet
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
  2.     CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
  3.  
  4. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  5. </asp:Content>
  6. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  7.     <h2>
  8.         This is the main site!
  9.     </h2>
  10.     <p>
  11.         To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
  12.     </p>
  13.     <p>
  14.         You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
  15.             title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
  16.     </p>
  17. </asp:Content>

 

Make sure the cloud project is set as the start project, and then run (F5) your solution.

Your web browser should open and display the site.

image

While this is still running open IIS manager. Open the list of sites, you should see the site for this solution (the name will be different):

image

The Windows Azure Compute emulator actually uses IIS to run your web role by creating a site on your local machine.

Stop your debugging session.

2. Adding the second site

Right-click your solution and add another ASP.NET web project.calling it “TheSecondWebSite”. Update the default.aspx again to show that this is the second site:

Code Snippet
  1. <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
  2.     CodeBehind="Default.aspx.cs" Inherits="WebApplication2._Default" %>
  3.  
  4. <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
  5. </asp:Content>
  6. <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
  7.     <h2>
  8.         The second web site!
  9.     </h2>
  10.     <p>
  11.         To learn more about ASP.NET visit <a href="http://www.asp.net" title="ASP.NET Website">www.asp.net</a>.
  12.     </p>
  13.     <p>
  14.         You can also find <a href="http://go.microsoft.com/fwlink/?LinkID=152368&amp;clcid=0x409"
  15.             title="MSDN ASP.NET Docs">documentation on ASP.NET at MSDN</a>.
  16.     </p>
  17. </asp:Content>

 

Now open the cloud service definition file (ServiceDefinition.csdef). Look at the <Sites> element and its children. Copy the <Site> element to create another one. Modify the site name and add a physicalDirectory element set to the path of the second site. Inside the <Site> element look for the <Binding> element and add another attribute hostHeader set to www.contoso.com. Ok, I’m using www.contoso.com for testing purposes, you can use your own site url if you want. We’ll be changing the host file on your machine so you can test everything, but this won’t work for other people. If you really want to make this work you will need to make the necessary DNS entries to redirect your site url to the cloud url…

ServiceDefinition.csdef

Code Snippet
  1. <Sites>
  2.   <!-- First site -->
  3.   <Site name="Check" physicalDirectory="C:\...\FullIIS_MultipleSites\WebApplication1">
  4.     <Bindings>
  5.       <Binding name="Endpoint1" endpointName="Endpoint1" />
  6.     </Bindings>
  7.   </Site>
  8.   <!-- Second site -->
  9.   <Site name="Encore" physicalDirectory="C:\...\FullIIS_MultipleSites\WebApplication2">
  10.     <Bindings>
  11.       <Binding name="Endpoint1" endpointName="Endpoint1" hostHeader="www.contoso.com" />
  12.     </Bindings>
  13.   </Site>
  14. </Sites>

 

Before you run your solution we need to make sure that the hostHeader www.contoso.com is pointing to the local environment. You can do this by editing the hosts file, which can be found in <Windows>\System32\drivers\etc\hosts. You can edit this file using notepad. Make sure you have the following in this file:

127.0.0.1 www.contoso.com

Run your solution. Your browser should show the first site:

image

Note the port used by the first site (in this screenshot above this is port 81, the compute emulator will take the first available port above port 80).

Now you can open the second site by browsing to www.contoso.com:81 (use your own port here).

image

Before you stop debugging, open the IIS manager again, you should now see two sites (you may need to refresh the sites node):

image

3. Creating a nested virtual application

Add another web project, calling it TheThirdSite.

Change default.aspx again to reflect that this is the third site.

Open the service definition again, and add the VirtualApplication element inside the second site:

Servicedefinition.csdef

Code Snippet
  1. <Sites>
  2.   <!-- First site -->
  3.   <Site name="Check"
  4.         physicalDirectory="C:\...\FullIIS_MultipleSites\WebApplication1">
  5.     <Bindings>
  6.       <Binding name="Endpoint1" endpointName="Endpoint1" />
  7.     </Bindings>
  8.   </Site>
  9.   <!-- Second site -->
  10.   <Site name="Encore"
  11.         physicalDirectory="C:\...\FullIIS_MultipleSites\WebApplication2">
  12.     <!-- Even a nested Virtual application -->
  13.     <VirtualApplication name="More"
  14.                         physicalDirectory="C:\...\FullIIS_MultipleSites\MvcApplication1">
  15.     </VirtualApplication>
  16.     <Bindings>
  17.       <Binding name="Endpoint1" endpointName="Endpoint1" hostHeader="www.contoso.com" />
  18.     </Bindings>
  19.   </Site>
  20. </Sites>

 

Run again and browse to www.contoso.com:81/More. You should see the third site.

4 Deploying and testing in the cloud

Let’s test your project in the cloud.

Deploy your solution:

image

Once deployment is ready click on the link to open the first site.

image

image

Now open a command prompt and use the ping command to retrieve the site’s IP address. Open the hosts file and modify the www.contoso.com to use the new IP address.

Browse to www.contoso.com (no need to specify a port number now). You should see the second site.

image

If not, you might need to clear your DNS cache using the command:

ipconfig /flushdns
net stop dnscache
net start dnscache

Browse to the nested virtual application at www.contoso.com/More:

image

That’s it. You’ve just created your own web role in the cloud, hosting three different sites! To complete everything you now should go to your internet provider to make the necessary DNS entries to redirect your site url to the cloud url!

Remote debugging a Windows Azure Worker Role using Azure Connect, Remote desktop and the remote debugger, part 3

This is the third part of the “Remote debugging a Windows Azure Worker Role using Azure Connect, Remote desktop and the remote debugger”. In this part I will show you how to attach the remote debugger on your worker role and start debugging…

Step 3: Connect to the remove debugger

In the previous blog post we ended up copying the remote debugger to the worker role instance machine.

Open the remote debugger folder (the one you just copied) and start the remote debugger by double-clicking the msvsmon.exe application. The remote debugger should start:

image_thumb16_thumb

Take note of the server name. Minimize the remote desktop session.

Open Visual Studio ( the one you used to publish the worker role project).

Select Debug->Attach to process… The Attach to Process windows should open. Type in the server name you got when you started the remote debugger.

image_thumb1

Be patient while Visual Studio downloads the list of processes from the remote server…

WARNING: if you get authentication errors this means you are not using the same username and password from your local machine.

After a while you should get a list of all processes:

image_thumb3

Select the WaWorkerHost.exe process and click the Attach button.

Step 4: debug!

Now you can add a breakpoint in your code and wait for the worker role to hit it. Again you will have to be patient…

image_thumb5

Happy debugging!

Remote debugging an Azure worker role using Azure Connect, remote desktop and remote debugger, part 2

This is the second part of the “Remote debugging a Windows Azure Worker Role using Azure Connect, Remote desktop and the remote debugger”. In this part I will show you how to get the remote debugger on your worker role.

Step 2: Copy the remote debugger on your worker role instance

Start by creating a shared folder on your system, and copy the remote debugger file to it.

To do this select a folder on your system (maybe first create one) and select Properties

Click on the Sharing tab, and click the Share button.

image_thumb1_thumb

Click the Share button (you should already be in the list), and then click Done, then Close.

Copy the remote debugger files into this shared folder. You should find the remote debugger inside the Visual Studio folder. On my 64 bit machine this is in the C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\Packages\Debugger\X64 folder…

Now we can create the remote desktop connection. Open the Azure portal, and go to your hosted service. Since we published the worker role you should find at least on instance… Select it:

image_thumb3_thumb

This should enable the Connect button:

image_thumb4_thumb

Click it to open the .rdp file. Accept the download by clicking the Open button.

If you get the Remote Desktop Connection warning, click on Connect.

image_thumb6_thumb

Enter your credentials (using your real username/password):

image_thumb8_thumb

Again a warning will popup:

image_thumb10_thumb

Select Yes (unless you don’t trust your own connection Smile)

Your remote desktop session should open. Minimize it because we still need to do something else…

Now we need to create a connection between the worker role instance machine and your machine.

Go back to the Azure portal and select the Virtual Network tab.

You should have the Local Endpoint installed (we did this in step 1) so you should see your computer and the role instances. To complete the connection we need to edit the role connections:

image_thumb11_thumb

This should open the Edit Endpoint Group dialog. Click on the Add button to add your local group (called MyGroup on my configuration).

image_thumb13_thumb

Click on Save.

Right-click the local endpoint (this is a little icon in the notification area of the taskbar) and select Refresh Policy. After a while this should look like this:

image_thumb14_thumb

Now go back to your remote desktop session.

Open a command prompt and ping your own machine. This might need a couple of retries until the DNS gets updated…

Open the shared folder on your machine (on my machine I do this by typing \\vaiopeter\RemoteDebugger in an explorer window).

Create a folder on the remote machine (I call it RemoteDebugger) and copy the contents of the shared folder in it.

Now we are ready to connect the debugger and start debugging…

Remote debugging an Azure Worker role, using Azure Connect, Remote desktop and the remote debugger

So, you have some code running in a Windows Azure Worker role on Azure (not on the Azure Compute Emulator) and you need to debug what is going on. In this blog post I will show you how you can use Azure Connect, the remote desktop and the remote debugger to look inside your worker role.

WARNING: this process can be a strain on your patience, since the debugger will work very slowly! But this is the only way to look inside a running worker role using the debugger…

Step 1: Install your worker role on Azure, enabling remote desktop and Azure Connect

First we need to install the worker role in an Azure instance, not forgetting to enable remote desktop and Azure Connect.

To install Azure Connect, right-click on the worker role and select Properties. The Worker Role configuration window will open. Select the Virtual Network tab:

image_thumb1

Check the “Activate Windows Azure Connect” checkbox and copy-paste your activation token in the textbox. To get the activation token, go to the Windows Azure Portal, select the Virtual Network tab and select your subscription (you may need to request access to Windows Azure Connect because it is still in CTP).

Click on the Get Activation Token button:

image_thumb2

Copy the activation token and paste in the project’s properties.

If you have never done so before, click on the Install Local Endpoint button to get the local software installed on your computer.

image_thumb3

To install Remove Desktop, you need to first install a certificate on your Hosted Service, containing both private and public key pairs. This is easy to do using Visual Studio:

Right-click your Azure project and select Publish… The Deploy Windows Azure project dialog will open.

image_thumb7

Click on the Configure Remote Desktop connection link, which opens the Remote Desktop Configuration dialog:

image_thumb9

Select a certificate from the drop-down, or create a new certificate using the <Create…> entry (the last entry in the dropdown combobox).

To export the certificate (so you can install it using the Azure Portal) click on the View… button.

Click on the Details tab, then on the Copy to File… button. The Certificate Export wizard will open… Click Next.

Select Yes, export the private key option. Press Next.

image_thumb11

Keep the default .pfx option, press Next again. Enter a password and press Next.

Select a file name, press Next then Finish. Now we’re ready to import the certificate in the Azure portal.

Go to your hosted services, select the service of your choice and then select the Certificate folder. Click on the Add Certificate button:

image_thumb13

Open the previously created .pfx file, enter your password and press Create. This should add the certificate to the hosted service.

Go back to Visual Studio (the Remote Desktop Configuration dialog should still be waiting for you). Select the certificate, enter your username and password.

WARNING: You want to use your real username and password, this way you can authenticate to the remote server from Visual Studio. Otherwise you will get authentication errors!

Enter some appropriate expiration date and click Ok to start publishing. Wait for Visual Studio to complete…

image_thumb15

This is step 1. In the next post I will show you how to copy the remote debugger to your worker role instance…

Getting Started Developing on Azure: Creating the Hosted Service and Storage Account

This is the next post on Getting Started developing on Azure with Visual Studio 2010 and Installing the Azure Management Certificates.

Starting to develop on Azure with Visual Studio can be a lot to eat the first time. Getting Visual Studio ready, including installing the Management Certificates and so on is not a simple task (the first time anyway). So that is why I made this little walk-through series on starting to develop on Azure… In the first part you’ll build and run your first project on the Azure Compute Emulator, which is the local test version of the cloud. In the second part you’ll get Visual Studio ready to directly publish your solution in the cloud by installing the management certificates and in this part you will create the Hosted Service and storage account to actually deploy from Visual Studio.

1.3 Creating the Hosted Service

Go back to the Azure Management Portal (windows.azure.com), and now click on the Hosted Services tab. To deploy we also need a hosted service, so click the New Hosted Service button to create one. This opens the Create a New Hosted Service dialog:

image

Enter a name for your service and URL prefix. The prefix should be world-wide unique, so try adding your company name or something unique to you in it.

If the chosen name is already taken you will be notified like this:

image

Now we need to choose in which data-center the service should be deployed. We can also do this using an affinity group. An affinity group is an easy way to keep everything together (services can communicate with one another more efficiently (and less cost) if they are running in the same data center) so choose affinity group and select Create a new affinity group… from the dropdown list.

Enter a name and datacenter; best take one near to your expected customers:

image

North Europe is the datacenter in Amsterdam, which is closest to where I live, so I took that one. Feel free to take another…

Click OK.

We’ll deploy using Visual Studio, so before you click OK, select Do not deploy. Now click OK.

1.1.5 Creating the Storage Account

Finally, before we can deploy we also need to create a Storage Account. Visual Studio will upload the package to storage and then instruct Azure to deploy it from storage.

Go back to the Management Portal (windows.azure.com)l. Now go the the Storage Accounts tab. Click on the New Storage Account button. This will open the Create a New Storage Account dialog:

image

Enter a unique (lowercase only) URL and use the same affinity group you used in the previous step. Hit OK.

1.1.6 Ready to deploy the web site

Now we are ready to deploy!

First we need to remove the local development trace listener from web.config configuration because it is not available in the cloud, but leave the diagnostic monitor listener:

Code Snippet
  1. <listeners>
  2.   <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  3.     name="AzureDiagnostics">
  4.     <filter type="" />
  5.   </add>
  6. </listeners>

 

Go back to Visual Studio, right click your azure project, and select Publish… Now select your hosted service and storage account, and hit OK (you might need to cancel and re-open this window to refresh it).

image

Deployment should start and after a little while you should see the Windows Azure Activity Log:

image

Wait until this is complete, it may take several minutes or longer depending on bandwidth…

image

Click on the WebSite URL, the site should open.

1.1.7 Using Server Explorer

Open Server Explorer. With the Azure SDK tools installed, you can look here at Azure Compute and Azure Storage. Click on the Windows Azure Compute tree item, then select Add Deployment Environment…

image

Select your management certificate to list all hosted services there. Open the tree item and select Staging or Production. Click OK. From now on you can look at this environment from Visual Studio Server Explorer:

image

You can try the same for storage.

Later we will look at how we can use this to debug applications with Intelli-Trace.

The end. My Next post (next Wednesday) is going to be on remote debugging worker roles running in the cloud…

Installing the Azure Management Certificates

This is the next post on Getting Started developing on Azure with Visual Studio 2010.

Starting to develop on Azure with Visual Studio can be a lot to eat the first time. Getting Visual Studio ready, including installing the Management Certificates and so on is not a simple task (the first time anyway). So that is why I made this little walk-through series on starting to develop on Azure… In the first part you’ll build and run your first project on the Azure Compute Emulator, which is the local test version of the cloud. In this part you’ll get Visual Studio ready to directly publish your solution in the cloud by installing the management certificates and in part 3 you will create the Hosted Service and storage account to actually deploy from Visual Studio.

1.2 Deploying your solution to the cloud

Now we’re ready to deploy to the actual cloud environment. Start by logging on to the Azure Management Portal, which is at http://windows.azure.com . Open the Hosted Services tab, and select Management Certificates:

image

If you just started with Azure development you will not have any certificate here. To make Visual Studio integrate better with the management portal (actually with the management API’s) we need to upload a certificate here so your Visual Studio can publish projects to the cloud. This is easily done with Visual Studio.

1.1.3 Installing the Management Certificate:

Go back to Visual Studio with the cloud project open. Right-click the cloud project and select Publish

image

The Deploy Windows Azure project dialog should open:

With this dialog you can publish your project. The first option will create the package, but then you have to deploy it using the Management Portal. The second option will do this for you. In the first drop-down you need to select a Management Certificate, or create a new one.

Select Add… from the Credentials drop-down. The Windows Azure Project Management Authentication dialog opens:

image

Select <Create…> from the first drop-down. Enter a friendly name for your certificate. I call mine AzureManagment.

image

Now click the View button. This will allow use to export the certificate’s public key using the Details… tab:

image

Click on the Copy to File button. This opens the Certificate Export wizard. Click next. Choose “Do not export the private key”:

image

Click Next> twice, then choose a filename for the certificate. Hit Next> then Finish. Your certificate should be exported now.

Go back to the management portal (windows.azure.com), to the Management Certificates. Click on the Add Certificate button and select your previously exported file. Click Ok and wait for the import to complete. Your certificate should be added, and now you need to copy the subscription-id back to Visual Studio. It is right there in the properties window of the certificate:

image

So copy it, go to Visual Studio to where the Windows Azure Project Management Authentication is waiting. Copy the subscription-id and name it:

image

Click OK.

In the next blog post we will add a hosted service and storage account and deploy the solution…

Getting started developing on Azure with Visual Studio 2010

Starting to develop on Azure with Visual Studio can be a lot to eat the first time. Getting Visual Studio ready, including installing the Management Certificates and so on is not a simple task (the first time anyway). So that is why I made this little walk-through series on starting to develop on Azure… In this part you’ll build and run your first project on the Azure Compute Emulator, which is the local test version of the cloud. In part 2 you’ll get Visual Studio ready to directly publish your solution in the cloud by installing the management certificates and in part 3 you will create the Hosted Service and storage account to actually deploy from Visual Studio.

1.1 Azure Lab – Getting started developing

In this walk-through you will learn how to develop with Visual Studio 2010 on Azure. To be able to do this you need to have the latest Azure SDK with Visual Studio tools installed (SDK 1.4 at time of writing), and you should also have a valid Azure account.

1.1.1 Creating the Azure project

Start Visual Studio 2010 and create a new cloud project, calling it MyFirstCloudProject:

image

Click on Ok. The New Windows Azure Project dialog should open:

image

Click on the ASP.NET Web Role and click the > button. Then rename the project by clicking on the rename button:

image

Call it MyFirstWebRole:

image

Click OK.

Add a button to the web form:

image

Implement its click event as follows:

Code Snippet
  1. System.Diagnostics.Trace.WriteLine("Hello from Azure!");

To enable Tracing to the Windows Compute Emulator, add following configuration to your web.config (line 6 to 9 should be added):

Code Snippet
  1. <listeners>
  2. <add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
  3. name="AzureDiagnostics">
  4. <filter type="" />
  5. </add>
  6. <add type="Microsoft.ServiceHosting.Tools.DevelopmentFabric.Runtime.DevelopmentFabricTraceListener, Microsoft.ServiceHosting.Tools.DevelopmentFabric.Runtime, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="DevFabricListener">
  7. <filter type="" >
  8. </filter>
  9. </add>
  10. </listeners>

Open the Solution Explorer and make sure that the Azure project is the start project. Run your solution by pressing F5. Note that Visual Studio will package your code and upload it to the Compute Emulator.

image

If not already running, the compute emulator (the little Azure icon in the system tray) will also be started. Right-click the Azure icon in the system tray and choose “Show Compute Emulator UI”.

After a while your browser will display the web site and the emulator will display loggings:

image

This window shows you output as your web role gets started, etc…

You can add some more by clicking the button. This should write something to the emulator:

image

Try placing a breakpoint on the button’s event handler. Click the button. Visual Studio should stop on the breakpoint. If your code was a little more complicated this would allow you to debug it.

In the next blog we will look at installing the management certificates to run this same code in the cloud, deploying it with Visual Studio 2010.