Tips and Tricks for Using Visual Studio 2010 to Build Applications that Run on Windows Azure

Thanks to everyone who attended my session at PDC today.  I really hope that you took something useful away from it.

As mentioned in the session, I’m posting the set of tips that were covered. 

You can now view the session online at: https://microsoftpdc.com/Sessions/SVC53

If you missed my session, I went through a "green field" new Windows Azure Cloud Service project developer scenario as well as a "brown field" moving an existing ASP.NET web application to run on Windows Azure developer scenario. 

Through the context of those two walkthroughs, I covered the following Tips and Tricks.

Tips for Getting Started

1. The Web Platform Installer automates a number of the steps to install the Windows Azure Tools for VS 2008 or to install IIS prior to installing the Windows Azure Tools for VS 2010.

2. Get the patches - https://msdn.microsoft.com/en-us/azure/cc974146.aspx

3. Name your projects before creating them.  When you add roles to your cloud service solution, it is much easier to rename them at that stage, then after the solution and projects have been created.

clip_image002

The reason is that even though you can use the Solution Explorer to rename the projects after creation, the folders where those projects reside will not be renamed.

4. Include the Static Content module and register the Silverlight mime type in IIS – I actually didn’t mention this one (on purpose) in my session as it fell below the cut line but the development fabric uses IIS under the hood and every so often we run into folks that get unexpected results when they run their apps on IIS because they are used to running them on the ASP.NET Development Server.

To enable the static content module: See tip #1 above (use WebPI) or see this article https://technet.microsoft.com/en-us/library/cc732612(WS.10).aspx

To register the xap mime type (typically only needed for older OSs):

  1. Open Internet Information Services (IIS) Configuration Manager and select the server to manage (usually the top-level item on the left side)
  2. Double-click MIME Types on the right side
  3. Add an entry to map the .xap extension to the application/x-silverlight-app MIME type

If you are using WCF, don’t forget to install the WCF Activation Windows feature – Control Panel | Programs | Turn Windows features on or off | Microsoft .NET Framework 3.5.1 | Windows Communication foundation HTTP Activation

Tips During Development

5. Always keep the Cloud Service project as the StartUp project to get the desired Run/Debug on the development fabric behavior.

5. Know the 3 differences between web roles and web application projects.  The 3 differences are:

  • References to the Windows Azure specific assemblies: Microsoft.WindowsAzure.Diagnostics, Microsoft.WindowsAzure.ServiceRuntime, and Microsoft.WindowsAzure.StorageClient
  • Bootstrap code in the WebRole.cs/vb file that starts the DiagnosticMonitor as well as defines a default behavior of recycling the role when a configuration setting change occurs.
  • The addition of a trace listener in the web.config file: Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener.

6. In general, WCF works correctly on Windows Azure.  There is a problem using "add service reference" or svcutil but we have a patch to workaround the problem.  The patch is installed in the cloud, and more information about this is here: https://code.msdn.microsoft.com/wcfazure/Wiki/View.aspx?title=KnownIssues (not that a web.config change is also required)

7. Use the tools to configure the UI.  Click on a role under the “Roles” node in Solution Explorer and select “Properties”.

image

This will bring up a UI over the Service Definition and Service Configuration file sections for that selected Role:

image 

8. Each role instance is run in a process, all role process are attached to the debugger

  • Web Roles run in WaWebHost.exe, one process per instance
  • Worker Roles run in WaWorkerHost.exe, one process per instance

image

When debugging, Visual Studio will attach to all of those processes as well as your web browser to enable debugging across roles and across instances. 

You can correlate the role instance you are debugging to the instance in the development fabric by looking at the log messages and matching the PID.  You can also right click and attach to the debugger from the development fabric and the subsequent dialog also shows the PID.

These processes in the Cloud will be 64-bit processes running on 64 bit Windows.  Keep that in mind especially if you are calling native methods through pinvoke.

9. Project settings for debugging in role projects are read when running on the development fabric – Although we don’t read all of the values, we do pay attention to the project properties debuggers settings.  For example on a web application project | project properties | Web tab, you can configure whether you want to to Native Code debugging, or Silverlight debugging.

image

10. Add multiple cloud service projects to the solution - you can use different cloud service projects to manage different configuration/definition files and different role configurations.

Tips for Migration

The NerdDinner sample code can be found at: https://nerddinner.codeplex.com/

10. Use an existing web application project as a web role

clip_image002[5]

Right click on the Roles node in the Cloud Service project and select Add | Web Role Project in Solution…

clip_image004

The select the project you just added.

clip_image006

Then refer to tip #5 above (know the differences between an ASP.NET Web App project and a Web Role project) and selectively add references, trace listener and startup code as desired.

11. ASP.NET Provider scripts for SQL Azure

To use the ASP.NET providers with SQL Azure, you can use these scripts: https://support.microsoft.com/default.aspx/kb/2006191 to setup the database.

12. Know what Visual Studio tools work with SQL Azure. 

Use SQL Server Management Studio 2008 R2 CTP3 (November) to connect to SQL Azure. (SSMS 2005 is not compatible and SSMS 2008 only support SQL Azure from the Query window)

Tips for Deployment

13. Test your app on the local development fabric with cloud storage.  There are two reasons to do this:

  • Deploying your application to the cloud in stages makes it much easier to diagnose any issues you encounter.  Being able to debug the app locally while you move the data to the cloud will help you flush out a number of issues
  • When your application is running in the cloud, run additional roles locally to debug issues that are related to production data that would otherwise be difficult to reproduce locally or hard to debug in the cloud.

14. Crack the Service Package using the _CSPACK_FORCE_NOENCRYPT_ environment variable. See this post for more information.

15. Use the Service Management APIs and Powershell cmdlets to automate deployment.  See this post for more information.

The script I used to deploy the service was:

$cert = Get-Item cert:\CurrentUser\My\<enter cert>
$sub = "<enter subscription ID>"
$servicename = <enter service name>'
$package = "<enter url to service package in blob storage>"
$config = "<enter path to service configuration file>"

Add-PSSnapin AzureManagementToolsSnapIn

Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub |
    New-Deployment Staging $package $config -Label 'PDC09Staging' |
    Get-OperationStatus –WaitToComplete

Get-HostedService $servicename -Certificate $cert -SubscriptionId $sub |
    Get-Deployment -Slot Staging |
    Set-DeploymentStatus 'Running' |
    Get-OperationStatus -WaitToComplete

csmanage (tool that exercises the Service Management APIs) and other samples that aren't included in the Windows Azure SDK can be found here: https://code.msdn.microsoft.com/windowsazuresamples