Windows Azure - Compute (Web Role)
As the title says, we are now going to talk about the service which allows the programmer to create applications which will enable the cloud to exhibit its potential on computing.
Azure development is categorized based on the roles and they are widely categorized as below.
- Web Role
The applications you write with the web role will concentrate with the objective which has to deal with the http traffic and need a public communication and a good example will be a web site or portal or web service or WCF service.
- Worker Role
Worker role, as it says by itself is a worker process / background worker which will keep running on the machine and is mostly programmed to run infinitely.
Remember that you cannot contact a worker role by yourself.
- VM Role
As discussed earlier, Microsoft is still working on this and expected anytime soon to come out with this.
We will look into every single role and check how to code with them.
As I already said, this deals with the applications which use the http traffic. Now without wasting much time, let's get into coding.
All our samples throughout the tutorial will be using Visual Studio 2010 Ultimate and .NET 4.0 (C#). Open the Visual Studio as administrator (Right click on the Visual Studio shortcut and click "Open as Administrator"); as running the Cloud applications require Admin rights, we have to do this.
Create an Application
Click on "New Project".
© Create New Azure Project - (Azure Not Installed) - Jebarson
If you are seeing this option in the dialog when you select "Cloud", then it means that you haven't installed Azure tools. Please install the SDK from the below URL http://www.microsoft.com/windowsazure/windowsazure/. Once you have installed, then you will see the screen as below.
© Create New Azure Project - Jebarson
We will name the project as "
CloudServiceTutorial". Now you will see another popup as below.
© Azure New Web Role - Jebarson.
Though it's out of scope we will just talk what about the different roles we are seeing in the above dialog.
- ASP.NET Web Role: As it sounds, this template will allow us to create a web application.
- ASP.NET MVC2 Web Role: This template will create a web application but on a MVC2 pattern.
- WCF Service Web Role: This template will create a WCF application over web role.
- Worker Role: As we already spoke, this will create a worker application which will run more like a service.
- CGI Web Role: Developers who know FastCGI languages can develop web application on cloud.
As we have already selected the web role, we will name as "
Now we have created a different application other than the conventional "Hello World" application, we will try to run the application and lets check how it runs. http://localhost:90/admin/Posts/Add_entry.aspx
For those who are seeing the below dialog, it can be any reason as below.
© Azure+Failed To Initialize - Jebarson
The output window will say as below:
Windows Azure Tools: Failed to initialize Development Storage service. Unable to start Development Storage. Failed to start Development Storage: the SQL Server instance ‘localhost\SQLExpress’ could not be found. Please configure the SQL Server instance for Development Storage using the ‘
DSInit’ utility in the Windows Azure SDK.
- SQL Express is not installed.
- SQL Express service is not running.
- May be you changed the default "
SQLEXPRESS" instance to something else.
If you have changed your "
SQLEXPRESS" instance name, then you have to change the instance using "DSInit.exe" tool. For changing the same, open the "Windows Azure SDK Command Prompt" application and run the
DSInit command using the below syntax which can be got by running "
DSInit [/sqlinstance:<SQL server instance>] [/forceCreate] [/user:<Windows account name>]
If you are wondering why you need SQL for running a web application, it's because we are running on a simulated environment, the Azure simulator will use the SQL as storage for running the application.
Assuming that we have solved the issue we faced, let's move on.
You will be now seeing our application's home page. And isn't it better than "Hello World" application? ;)
Now coming back to our solution, let's walkthrough the differences between the conventional web application and the Azure web role application.
The solution will have two files name "ServiceDefinition.csdef" and "ServiceConfiguration.cscfg" which are used for "Service Configuration Schema" and "Service Configuration Values".
Service definition file
This file deals with the roles, end points, and the configuration settings available for the roles. Always note that you cannot change this file after being deployed.
Service configuration (values) file
This file holds the values of the keys / settings defined in the definition file. This file can be changed after deployment. Do remember that you cannot set a value unless it is defined in the definition file.
On the developer environment, these values can be added / edited using the corresponding role setting file. This file can be found inside the solution folder "Roles". Each and every role added to the solution will have a similar settings file. For more information on the same, please refer http://msdn.microsoft.com/en-us/library/ee758711.aspx.
Role Settings File
As I mentioned earlier, Visual Studio presents the settings file in a well arranged UI similar to the project settings. We will only discuss about the settings available in for the web role.
.NET trust level: By default, "Full trust" is selected which means that the application is fully trusted to work with system resources and libraries both managed and native. However partial trust restricts the access to limited and more information on the details can be found in http://msdn.microsoft.com/en-us/library/dd573355.aspx. Do remember any managed library needs a "Full trust" by default and if it has to use partial trusted applications, then "
AllowPartialTrustedCallers" has to be set.
Instance Count: As the name suggests, it's all about the number of VMs where the application will be deployed into. Now if we want to test the same on our simulation environment, let's change the instance count of our "
CloudTutorialWeb" to run on 2 instances and run the application. Now open the "Development Fabric UI". Now on the left tree view, you will see two instances running "
0" and "
VM Size: Ranging from "Small" to "Extra Large" and you can find the configuration of the same by clicking the information icon next to the drop down or check it at http://msdn.microsoft.com/en-us/library/ee814754.aspx.
Do remember that "Instance Count" and "VM Size" will affect your billing.
Startup Action: The browser can use both HTTP and HTTPS endpoints which can be configured here.
This is very similar to the application settings that we have on a config file. While you enter a value here, as we discussed earlier, the definition moves to ".csdef" file and the value is moved to ".cscfg"file.
When you are defining a connection string, either you can specify to "
UseDevelopmentStorage" which will use the
SQLEXPRESS instance of SQL as we discussed earlier or use the cloud storage. By default, our application will have a "
DiagnosticsConnectionString" which is used for the logging and other diagnostics purpose.
© Azure Connection String Settings - Jebarson
By default our web role will have a HTTP endpoint and if you have SSL to be used, then you can select the "HTTPS" option for the same and provide the port and certificate details.
Apart from the "Development Storage" and "SQL Azure" we briefed in previous part, there is something called "local storage" which is provided for every web / worker role. This is just a temporary file storage which can be mostly used for caching on temporary value storage. They can also be set to be cleaned when there is recycle on role.
Always remember that if there is a VM crash, the files wont be recovered. Also do remember that these files cannot be shared across instances if you running a multiple instances and therefore your code should accommodate considering the fact.
As every other .NET application, Azure application can also take the advantage of the certificates for application signing.
This class inherits the "
RoleEntryPoint" where we define the role "
Start" and "
Stop" methods. These methods are fired on the role start and stop respectively. By default, you will see the
DiagnosticMonitor being initiated. Now if you want any code to be run on these methods, you can add them.
Simulation Environment for Web Role
© Azure+Simulation+Environment Jebarson.png
When you ran your application, you might have seen an icon similar to the above. Right click on the icon and click "Show Development App Fabric UI". You will see a window as below.
© Azure Development Fabric - Jebarson
The command window will show all the logs written by the application. Every role added to the project will show their own role instances similarly. The logging level can be changed the way we want to trace / troubleshoot; this can be done by changing the options in Tools à Logging Level.
Now we have created our first web role application on cloud and learnt few things related to that as well. The web application development is nothing but ASP.NET and it's out of our scope. In our next part, we are going to learn about the "Worker Role" which is again an another part of "Compute" in Windows Azure.