September 7, 2012 Leave a comment
I was very happy with the news that recently Windows Azure was available in my region which I was waiting for. So this would be my first cloud solution which I would deploy in the cloud. To start with I will make a simple MVC 4 application and deploy in the cloud using VS 2012.
The Windows Azure SDK for .NET can be downloaded from here for VS2012/VS2010. After installing the SDK you would get the Windows Azure tools and Emulator to develop and test cloud solutions locally. After installing the SDK you can see the cloud template under C# as shown below. You can see the cloud service by selecting framework 4.0 and then selecting “Windows Azure Cloud Service”.
In the dialog box below you can select the different roles that are required for the cloud solution. In our scenario since we are making a simple MVC 4 application so a MVC 4 web role would be enough but to do more with the solution in the future I am adding a worker role. A web role is the front end of the azure service which can be assumed that it will be on a web server (IIS) in the cloud whereas a worker role can be assumed as a Windows Service which can be used for background processing.
The web and worker roles will be renamed from the default and click OK. After that the template for MVC 4 application will appear and we will choose the internet application.
As you can see in the solution there are three more projects along with the MVC 4 project. One is the Unit test project for the MVC. In the MVC project you can see that most of the files are the same but with one more additional file WebRole.cs. If you open this file you can see the entry point. This is the entry point which is going to start everything before the web application starts executing.
If you go to the worker project and open the WorkerRole.cs you can see the Run method and the OnStart method. The Run method will have the implementation of your code in which you can fire some events like call WCF service, monitor for storage etc.
The third Windows Azure project you can see a Web Role and a Worker Role configuration file. If you open the HelloWorldMvcWebRole file you can see the below configurations. In this configuration files we can specify how many web role instances we want in the cloud and the size of the VM. The VM size has different billing cost so we will specify the extra small VM since we don’t need much memory and processing. I will specify two instances for high availability and load balancing the web role.
In the settings tab I will add another configuration called “Message” in which I will specify a string value “Hello Windows Azure”. The advantage of this configuration is after deploying the solution in the cloud we can change the value and it would reflect in the application at runtime. Otherwise without configuration these changes would require redeployment in the cloud. So keeping these kind of settings in the configuration is an important decision while developing Azure solutions.
If you open the ServiceConfiguration.Cloud.csfg file you can see all the settings in the XML format which we specified above in the interface.
Now lets open the HelloWorldWorkerRole file and you will find the same GUI and configurations. Right now we are not doing any background processing so I will keep the instance count 1 and the VM size as Extra Small.
Now lets change some code in the Home Controller in the MVC Web role project as in the below screen and put a break point. We are just reading the “Message” configuration key from the Windows Azure project as shown in the code below. Lets hit F5 and run the project. Make sure that the “Compute Emulator” and the “Storage Emulator” which was installed with the SDK is started and the status is in the Running State in the task bar.
The output can be seen on the browser and note the URL that it is running locally on the PC with the Windows Azure Emulators.
You can open the Windows Azure Compute Emulator from the taskbar and see the deployment. You can see the two instances of the Web Role and one instance of the Worker Role as shown below. The Emulator runs on the local PC emulating the cloud and using the local storage. You can open the local store as shown below in the screen.