Recently, i did a webcast on this topic and we are running WebCamps on WebMatrix as well which will be covering this part. Hence the premise of this post is to provide steps to deploy the application you developed on WebMatrix as a Windows Azure Hosted service.

Before i detail the steps, here are the links i referred to and those that will add more value

To run the demo source code and set up your environment for WebMatrix to Azure migration, you will need

Below are the steps you can follow for migration

STEP 1 : Create your WebMatrix application as you normally do

1. Fire up WebMatrix and Site From Template and lets choose Photo Gallery for this demo

               image

2. This will generate the code stubs that gives a readymade sample ready to execute. Click on Run and choose browser of your choice.You could login /register and add a photo gallery of your choice and modify this template however you choose. You can also check the Files tab on the bottom left and take a look at the code. For reference on code using Razor syntax and creating an WebMatrix application, please read from here : http://bit.ly/pIgcno 

               image

3. You can click on Top left option “Publish” and below wizard will pop up.Here you can ftp or web deploy to any server of your choice. There is option “import publish settings” in case you want have a file available by your service hoster. But publishing to Azure Platform is not as straight forward as below.

              image

STEP2 : Now for migrating this WebMatrix application as an hosted service in Azure

Windows Azure will take 2 files to make sense of the application

  • ServiceDefinition file – Which will give the physical file path of the WebRole or application and provide some settings
  • ServiceConfiguration file -  Provides configuration settings like “number of instances to scale”, “connecting string for Azure storage” etc. Like web.config for the cloud you could say)

1.  Scrubbing the WebMatrix application

Go to physical folder location & Add assemblies

  • Open the folder location of the WebMatrix application (PhotoGallery) & go to bin directory
  • Ensure you have the following dlls here, if you don’t, please add a copy of them here
    • Microsoft.Web.Helpers
    • Microsoft.Web.Infrastructure.dll
    • NuGet.Core.dll
    • System.Data.SqlServerCe.dll
    • System.Web.Helpers.dll
    • System.Web.Razor.dll
    • System.Web.WebPages.Administration.dll
    • System.Web.WebPages.Deployment.dll
    • System.Web.WebPages.dll
    • System.Web.WebPages.Razor.dll
    • WebMatrix.Data.dll
    • WebMatrix.WebData.dll
  • Open the Web.config of the WebMatrix application and ensure you add the below setting . This is because, in the Azure VM, by default,”HTTPRedirection” module has not been enabled, hence any extensions other than aspx will not be recognised by the IIS. Also add the default page.

image 

2. Targeting the .NET framework 4.0 in Windows Azure

Azure that the .NET runtime which is being targeted is 4.0 since WebMatrix runs under 4.0 context. Create a properties.txt file with single line of code “TargetFrameWorkVersion=v4.0” and place it outside the PhotoGallery folder(i.e -  outside the base WebMatrix folder).

3. Creating Service Definition File

For Windows Azure to run WebMatrix, it will look for the ServiceDefinition File, which will tell which folder( called Role ) that it needs to Web Deploy. In this example, we will deploy the application as a Web role and say that IIS should listen at port 80 for the same.

Hence, create “ServiceDefinition.csdef” and put in the below code:

image

 

4. Creating the Configuration file

Open the Windows Azure Command prompt and we can package the application to run under cloud simulation. While doing this we can generate the the ServiceConfiguration file as well using “generateConfigurationFile” helper. Take a look at the syntax below. We are calling the “cspack” command to package the application and passing the following

  • ServiceDefinition to get the file path and other details.
  • /role: <The name of the role ( as mentioned in the Definition file)> ; < WebMatrix project name>
  • /rolePropertiesFile: <<The name of the role ( as mentioned in the Definition file)> ; <properties.txt which we create in Step 2 and placed along with Definition File>
  • /copyOnly  - To run again development simulator , this will not create the cspkg , zipped version.
  • /out: <any name.csx>  -  This will be the output package
  • /generateConfigurationFile:  The ServiceConfiguration file will be generated with 1 instance count as default

image

 

5.  Deploying the application on the Development Emulator

Step 4 needs to be done to get the output unzipped package

Next we need to run the package like “csrun HelloRazor.csx ServiceConfiguration.cscfg /launchBrowser” as shown below and the browser will be launched as shown.

image

 

6.Create the Deployable package to Azure Data Centre as a hosted Service

Finally, once you have tested locally and ready to push it onto the cloud, you can run the cspack command with below parameters

  • ServiceDefinition.csdef
  • /role: <The name of the role ( as mentioned in the Definition file)> ; < WebMatrix project name>
  • /rolePropertiesFile: <<The name of the role ( as mentioned in the Definition file)> ; <properties.txt which we create in Step 2 and placed along with Definition File>
  • /out: <your name.cspkg>

 

image

You will get a cspkg file generated at the same folder.

7. Upload the Deployable package to Azure Data Centre as a hosted Service

  • open http://windows.azure.com and sign in with your passport id and open the Hosted Services from bottom left
  • Click on new hosted service on top left and fill in details, choose deploy production and upload the package file and service configuration file.

image

Once the status of the role become ready you can browse to the URL and you will see your WebMatrix application running as an hosted Service on Azure virtual machine!

Hope this post was useful and some of the key downloads are below: