Disclaimer: This is not a tutorial on how to take an existing project and make it ‘just work’ in Azure. Depending on your existing project it may either be as easy as described below, or so painful that it’s not worth it. The goal of this post is simply to show how to make projects compatible with the Azure development fabric.
So you’ve got an existing project and are experimenting with making it run on the Azure platform. Do you.
A) Create new Azure projects and meticulously copy your code over?
B) Throw your hands up in defeat and tell your boss it’s too much work?
C) Add a few snippets of code to your existing projects and be a hero?
As it turns out, C is the correct answer! In the case of either a web application or a class library, you can easily turn them into web roles and worker roles, respectively. In order to do this, simply unload the project and add the appropriate XML to the project.
To make a web application project (including ASP.NET MVC) able to run as a web role, add the following.
|
1 2 3 4 |
<span class="kwrd"><</span><span class="html">PropertyGroup</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">RoleType</span><span class="kwrd">></span>Web<span class="kwrd"></</span><span class="html">RoleType</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">ServiceHostingSDKInstallDir</span> <span class="attr">Condition</span><span class="kwrd">=" '$(ServiceHostingSDKInstallDir)' == '' "</span><span class="kwrd">></span>$(Registry:HKEY_LOCAL_MACHINESOFTWAREMicrosoftMicrosoft SDKsServiceHostingv1.0@InstallPath)<span class="kwrd"></</span><span class="html">ServiceHostingSDKInstallDir</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">PropertyGroup</span><span class="kwrd">></span> |
To make a class library project able to run as a worker role add the following.
|
1 2 3 4 |
<span class="kwrd"><</span><span class="html">PropertyGroup</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">RoleType</span><span class="kwrd">></span>Worker<span class="kwrd"></</span><span class="html">RoleType</span><span class="kwrd">></span> <span class="kwrd"><</span><span class="html">ServiceHostingSDKInstallDir</span> <span class="attr">Condition</span><span class="kwrd">=" '$(ServiceHostingSDKInstallDir)' == '' "</span><span class="kwrd">></span>$(Registry:HKEY_LOCAL_MACHINESOFTWAREMicrosoftMicrosoft SDKsServiceHostingv1.0@InstallPath)<span class="kwrd"></</span><span class="html">ServiceHostingSDKInstallDir</span><span class="kwrd">></span> <span class="kwrd"></</span><span class="html">PropertyGroup</span><span class="kwrd">></span> |
Once you’ve made these changes and reloaded the project, you should be able to associate your cloud service project with an existing web or worker role!
Unload Project |
Edit Project |
Add XML |
Reload Project |
Associate Project |
A Few More Tricks
As I was working on this post I came across a few more tricks and undocumented nuances that cost me an unfortunate amount of time trying to figure out.
- In order to run an ASP.NET MVC application under Azure, you’ll need to copy the System.Web.Mvc assembly local. This can easily be accomplished from the properties pane.
- In web applications, you can not work with the RoleManager within Application_Start (see the remarks after the jump).
- While you can make any class library a worker role, the development fabric will crash & burn if you do not have exactly one class in that assembly the derives from RoleEntryPoint.