Monday, December 26, 2011

Virtual Machine Role in Windows Azure


Virtual Machine Role in Windows Azure

Windows Azure compute provides facility to run an application in the cloud. For this azure provides three types of roles:
1)      Web role: used for web application programming and supported by IIS7
2)      Worker role: used for asynchronous, long running tasks, without any user interruption.
3)      VM role: used for migrating windows server applications to Windows azure in an easy way.
In this article we will see Virtual Machine (VM) roles. VM role fits for the situations where:
·         Long running application installation is required
·         Error prone application installation
·         Application installation needs manual interaction
·         Migrating existing applications
VM role is a special role, which gives user freedom to control the operating system. While web role and worker role runs on a virtual machine, but VM is a virtual machine. But this comes with the responsibility of making sure that the OS image is up to date through the creation of difference disks. So it increases the cost of development, testing and maintenance.
Creation of VM role in azure needs:
I will blog each step in detail in my upcoming articles.

Friday, October 14, 2011

Difference between ASP.NET HttpHandler and HttpModule

In this blog we will see what are http handlers and modules. And in which condition which one is suitable. For that first we need to understand when user request application resource to web server, what happened? The user requests for a resource on web server. The web server examines the file name extension of the requested file, and determines which ISAPI extension should handle the request. Then the request is passed to the appropriate ISAPI extension. For example when an .aspx page is requested it is passed to ASP.Net page handler. Then Application domain is created and after that different ASP.Net objects like Httpcontext, HttpRequest, HttpResponse are created. Then instance of HttpApplication is created and also instance of any configured modules. One can register different events of HttpApplication class like BeginRequest, AuthenticateRequest, AuthorizeRequest, ProcessRequest etc.
HTTP Handler
HTTP Handler is the process which runs in response to a HTTP request. So whenever user requests a file it is processed by the handler based on the extension. So, custom http handlers are created when you need to special handling based on the file name extension. Let’s consider an example to create RSS for a site. So, create a handler that generates RSS-formatted XML. Now bind the .rss extension to the custom handler.
HTTP Modules
HTTP Modules are plugged into the life cycle of a request. So when a request is processed it is passed through all the modules in the pipeline of the request. So generally http modules are used for:
Security: For authenticating a request before the request is handled.
Statistics and Logging: Since modules are called for every request they can be used for gathering statistics and for logging information.
Custom header:  Since response can be modified, one can add custom header information to the response.

Thursday, October 13, 2011

Logging Application Block

First we need to know what application blocks are?
Application Blocks are reusable software components and are part of Microsoft’s Enterprise Library. Enterprise library provides configurable features to manage crosscutting concerns like validation, logging, exception management etc. Here I elaborate on one of the feature of Enterprise Library- Logging Application Block. One can use the Logging Application Block to log information to:
1)      Event logs
2)      A text file
3)      An email message
4)      A database
5)      A message queue
6)      Windows Management Instrumentation (WMI) event
7)      Customize the logging location
In this article we will see how to configure the logging. One need to install the Enterprise Library.
Create a windows form application. Now add a reference of Enterprise Library Logging Application Block to the solution.

Now add a configuration file for configuring the log. Now edit it with Enterprise Library Configuration.


It opens a window. In that select Blocks -> Add Logging Settings


It creates a default logging settings as follows:


It contains a category named “General”, which is using an “Event Log Listener” with a “Text Formatter”. For now we can leave Special Categories. So, what all these are doing?

Let’s understand it with an example. There may be some logs which you need to write in event log and a text file, and for some log you need to mail also. So you create one category which writes all logs to event log and text file, and another category for sending mail of only error logs. Then you have to create 3 listeners, for logging in event log, text file and mailing. You can also define different formatters for different type of listeners. For example you need a detailed log for logging in events log and precise log for mailing. All these settings are defined in the configuration. All you need to do is when you write a log just defines the category of the log and rest is done by the application block.

Categories:
Category can filter the log entry and route it to different listeners. The filter can be based on the severity, i.e., Critical, Error, Warning or Information.

Logging Target Listeners:
Different listeners can be defined here. We only need to do configuration and these listeners can write to event logs, a flat file, xml file, to database, send mail etc.

Log Message formatters:
This is used to define a format in which log is written. Text formatter is the default formatter, which writes all the information. For customizing the formatter, define a template.
A sample project is uploaded. This contains a general category which logs to event logs and a flat file using text formatter. And mail category which sends mail using mail formatter. And depending on the conditions it logs to general category, or sends mail, or can do both.

So now you can see a configuration section “loggingConfiguration” in the app.config file.
This configuration section contains listeners, formatters, categorysources and specialSources. One can configure listener, formatter, categories here.

To write a log you just need to create a LogEntry and specify the category. Of category is not specified, log will be written to default category.
So, now you can see logging so easy using Application blocks.

Monday, September 19, 2011

Managed Extensibility Framework (MEF)

MEF is a component of .net framework 4.0, to create lightweight, extensible applications. It avoid hard dependencies and lets application developer to discover and use extensions without any configuration required.

Why MEF?

Imagine a situation where an application is using several smaller components. And application is responsible for creating and running those components.
One possible solution could be including all the components as source code in the application. But you cannot add new components without modifying the source code.
Another solution could be to provide an interface for decoupling between application and components. So the component can implement the interface and interact with application. But this approach too has drawback. As application cannot discover components by itself, it must be explicitly told which components are required and loaded.
Here MEF comes into the picture. MEF provides a way to discover components via composition. A MEF component specifies both its dependencies (known as imports) and what capabilities (known as exports) it makes available.
Let’s understand it with the help of an example. Suppose you are having a simple calculator application which currently supports addition and subtraction.

Creating composition container and catalogue

Composition container keeps track of which components are available for composition and what are their dependencies. It provides a way by which application can get the instance of components to be composed. We need to include System.ComponentModel.Composition in reference.
   
   
    //An aggregate catalog that combines multiple catalogs
    var catalog = new AggregateCatalog();
    //Adds all the parts found in the same assembly as the current class
    catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly));
 
    //If parts are placed at some other location then adds that directory path
    //catalog.Catalogs.Add(new DirectoryCatalog(componentsDirectoryPath));
 
 
    //Create the CompositionContainer with the parts in the catalog
    CompositionContainer _container = new CompositionContainer(catalog);
 
    //Fill the imports of this object
    try
    {
        this._container.ComposeParts(this);
    }
    catch (CompositionException compositionException)
    {
        Console.WriteLine(compositionException.ToString());
   }

catalog.Catalogs.Add(new AssemblyCatalog(typeof(this).Assembly));
This adds the components from the current assembly. To add the components from some specified folder location, add a directory catalogue like:
catalog.Catalogs.Add(new DirectoryCatalog(componentssDirectoryPath));
Where componentsDirectoryPath is the path of directory where components can be found.

Import and Exports

Define an interface like it.
[Import(typeof(IOperation))]
public interface IOperation
{
    string Operate(int leftOperand, int rightOperand);
}

This interface has an attribute Import. This ImportAttribute defines that the type IOperation needs to be imported. So we implement the IOperation class, and above the class we use ExportAttribute indicating that it has the capabilities of IOperation. Also it contains ExportMetadata attribute indicating that depending on the metadata Symbol the operation is performed.
[Export(typeof(IOperation))]
[ExportMetadata("Symbol", '+')]
class Add : IOperation
{
//Implementation of IOperation
}

Now we will do lazy initialization for getting objects like this:
[ImportMany]
IEnumerable<Lazy<IOperation, IMetadata>> operations;

Lazy initialization is used so that only operations that are needed are initialized. Based on the metadata it initializes what operation is to be performed. It contains ImportMany attribute because IOperation can be filled by many exports like add, subtract etc.

Lazy Initialization

As we see we declare an IEnumerable for lazy initialization. Now how does it work? When we run application, it initializes the catalogue and creates a container. Based on the Import attribute it finds the components which can be filled for it. Here we have only one class Add to fill it. So operations will contains only single Lazy object, and that object will be initialized when it will be accessed first time. So based on the operation we will call Operate function.
foreach (Lazy i in operations)
    {
        if (i.Metadata.Symbol.Equals(operation)) 
                 result= i.Value.Operate(left, right).ToString();
    }

I had a sample project to illustrate it. It contains 2 operations Add and Subtract in the assembly and Multiplication in a separate dll, which can be found in “\Extensions” folder.







Thursday, July 21, 2011

Extension Methods in .Net

In this article we will see what extension methods are, why we need them and how to create them.
Extension methods are methods which allow you to extend a type, even if you don’t have the source code for that type. Let’s understand it with a simple example: Suppose you want to add IsNumeric method to string class to check whether a string is numeric or not. What you will do?
1) Either you have to create a new class inheriting from string class and then add the IsNumeric method in it. Now this solution has 2 problems. First, what if the class you want to inherit, in this case string class, is sealed. You can’t inherit from it. Second, if any how you inherit the string class then you need to make sure everyone else is using your class not the string class
2) Or create a static StringHelper class and create a static IsNumeric function in it. But this solution also has a problem that user need to know a Helper class exist.
Prior to .Net Framework 3.5 second solution was the most popular solution. But in .Net Framework concept of extension methods is introduced. Now let’s see how to create extension methods. We need to create a static class StringHelper and a static method IsNumeric as we do in the second solution.
public static class StringHelper
{
public static bool IsNumeric(this string str)
{
double val;
return double.TryParse(str, out val);
}
}
The only difference from second solution is the “this” keyword in the IsNumeric parameter. By using this keyword we are indication that it is the extension of type next to the “this” keyword. Here we have written “this string” it means IsNumeric is the extension of string type.
To use it:
string nonNumericString = "123abc";
bool Result = nonNumericString.IsNumeric();
Another benefit of using extension method is that you can easily convert helper class into extension methods without breaking the existing functionality, i.e., following code still works:
bool Result = StringHelper.IsNumeric(nonNumericString);
So now you realize it is easy to create extension methods. J

Saturday, May 7, 2011

Initializing Windows Azure Diagnostic – Part 2

As you have read in my previous article how to initialize Windows Azure diagnostic monitor. Now we will see how to collect different diagnostic data. I am defining a connection string for the role. For that right click on the role and click on properties. Now from the connection tab you can add configuration string.
clip_image002

1) Collecting data from event logs
Open the Role entry point class. Add following code to the OnStart() function
var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
config.WindowsEventLog.DataSources.Add("System!*");
config.WindowsEventLog.ScheduledTransferLogLevelFilter = LogLevel.Warning;
config.WindowsEventLog.ScheduledTransferPeriod = TimeSpan.FromMinutes(1.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", config);

So, first you need to get DiagnosticMonitorConfiguration object. Then you can add channel to the datasource. You can add multiple channels. Windows Azure cannot log Security channel. You can define log level filter, so that only those logs are transferred. You have to define schedule transfer period, so that after that time span logs are transferred to the account you specified in the configuration. Now start the diagnostic monitor with the connection and new configuration.

2) Collect data from Performance Counters
Open the Role entry point class. Add following code to the OnStart() function

var config = DiagnosticMonitor.GetDefaultInitialConfiguration();
config.PerformanceCounters.DataSources.Add(new PerformanceCounterConfiguration()
{
CounterSpecifier = @"\Processor(_Total)\% Processor Time",
SampleRate = TimeSpan.FromMinutes(1.0),
});
config.PerformanceCounters.ScheduledTransferPeriod = TimeSpan.FromMinutes(5.0);
DiagnosticMonitor.Start("DiagnosticsConnectionString", config);

Here I have defined a performance counter which takes sample for processor time in every minute. You can add multiple performance counters in the data souce. And then transfer the log to azure account defined in the connection string after every 5 minutes.

3) Collect data from IIS logs
IIS logs are collected only for web role because worker and VM roles don’t run under IIS.
To enable IIS logging add following code to the web.config.

<tracing>
<traceFailedRequests>
<add path="*">
<traceAreas>
<add provider="ASP" verbosity="Verbose" />
<add provider="ASPNET" areas="Infrastructure, Module, Page, AppServices" verbosity="Verbose" />
<add provider="ISAPI Extension" verbosity="Verbose" />
<add provider="WWW Server"
areas="Authentication, Security, Filter, StaticFile, CGI, Compression, Cache, RequestNotifications, Module"
verbosity="Verbose" />
</traceAreas>
<failureDefinitions statusCodes="400-599" />
</add>
</traceFailedRequests>
</tracing>
After adding this code IIS logs are automatically logged. No additional API calls are required.

4) Collect data from crash dumps
In the role entry point class add the following code;

Microsoft.WindowsAzure.Diagnostics.CrashDumps.EnableCollection(true);

Pass true for enabling complete crash dump data, or false for partial crash dump data.

Initializing Windows Azure Diagnostics- Part 1

As we have seen in my previous blog that some of diagnostics data is stored in table, while some in blob. For collecting diagnostics data, we must initialize Windows Azure diagnostic monitor. Windows Azure diagnostic monitor runs in Windows Azure and in the compute emulator and collects diagnostic data for a role instance. Only Windows Azure Logs, Windows Azure Diagnostic Infrastructure Logs and IIS logs are configured in the diagnostic monitor by default. We can collect other diagnostic data by importing diagnostic module. A role instance that imports the Diagnostic module starts, it starts the diagnostic monitor.
To start diagnostic monitor for a role open the role entry point class and make sure that you are using Microsoft.WindowsAzure.Diagnostics namespace in the class. Now on the Azure service project right click on the role and click Properties. Here I am configuring a web role.
clip_image002
Now in the configuration section check enable diagnostics. And specify the account credentials
clip_image004
You can either use development storage or can give your account credentials. Just click on “…” and will ask for the credentials.
clip_image006
Now when you open your .cscfg file it has a new setting for the webrole like
<ConfigurationSettings>
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=myaccountName;AccountKey=myAccountKey" />
</ConfigurationSettings>

And in the .csdef file it import the module like:
<Imports>
<Import moduleName="Diagnostics" />
</Imports>

If you are using a web role or worker role template for creating roles then in the web.config or app.config following code is added. Or you can just inherit a class from RoleEntryPoint and add the following code in the config file.


<system.diagnostics>
<trace>
<listeners>
<add type="Microsoft.WindowsAzure.Diagnostics.DiagnosticMonitorTraceListener, Microsoft.WindowsAzure.Diagnostics, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" name="AzureDiagnostics">
<filter type="" />
</add>
</listeners>
</trace>
</system.diagnostics>


 If you want to create a web role just override OnStart() method, and if you want to create a workerrole override OnStart() and Run() method. We will see it in my next article.