Wednesday, September 30, 2009

Web Farm and ASP.NET

This is an abstract of the Web Farming in ASP.NET. Just like “Capsule”. Before discussing about Web Farm, we need to get some basic knowledge about Server Farming.

What is a Cluster Computer or Server Farm?

Cluster Computer (Server Farm) is virtually a SINGLE computer system, but logically it includes multiple computers. That means, there are multiple computers, but it works as a single unit. These computers are usually connected by Local Area Network. The Load Balancing logic decides how/which computer in the Cluster to do the task.

Load Balancer

Load Balancer can be Software or a dedicated Hardware, but its function is same to forward the request to actual Server. The Load Balancer will decide which server should be used for getting optimal performance. The decision will be based on the configured scheduling algorithm. Example for scheduling algorithm: - Round Robin, Random, Weighted Round Robin etc..

What is Web Farm?

A major use of Server Farm is for hosting web sites. In this case we will call the server farm as Web Farm. By using web farm for hosting web site we can make sure the availability and performance of the web site.

How it works?

The web farm is a collection of web servers, but all are hosted with same websites. If a user requests an HTML page in the website (Eg:- http://testserver.com/home.html), this will come to the Load Balancer. Then it will forward this request to anyone of the web server. That means all the requests to the website are routed through the Load Balancer. So we cannot say the user will be served by the same web server for all his requests.

Web Farm & ASP.NET

Simply a website is stateless. That means it will forget all about the previous page request. For example, User A requests home.html, then Aboutus.html. Here the web site cannot identify both requests are from the same user. Each request will be treated as an independent request.

Microsoft ASP.NET has an advance feature, it provides Session State Management. That means it keeps memory of the page requests. So it can identify the requests from the same user.

So the problem arises in the Web Farm.

Assume, we are browsing a shopping web site for purchasing a Laptop. The Products.html page contains all the models of Laptop and “Purchase” button against each Laptop. While clicking on the “Purchase” button the users will be redirected to CreditCarddetails.html page and collects the details. Here the website keeps the Product selected in the first page in his memory for later processing. If this happens in a web farm, there will be a problem. For instance, the first request will be to Webserver1 and second will be to Webserver2. So Webserver2 will not get the information collected in the first request.

Solutions For this Problem

There are too many solutions for this problem. These are the commonly used solutions

1. Use SQL Server or State Server for keeping state information.
Here the state information will be kept in a shared Database or a shared service. So all the web servers can access this and can recollect its memory even though the previous request was to another web server.

2. Persistence/Stickiness.
This is usually done through client side page redirecting. The first request will be through the load balancer, while processing the first request the URL will be replaced with new URL. This new URL is the URL of an individual web server not the Load Balancer. The main problem is, the underlying web servers are visible to the network.

Tuesday, February 12, 2008

FileSystemWatcher: Monitor for File changes using C#.Net

This example will show messages if there is any file change in the specified folder. But i added a file filter condition for text files(*.txt). So it will monintor only for txt files. The changes like create new file, rename a file, accessing files etc.. will be notified by a message Box.

The following code snippet will create a file watcher and initialize its properties, then enable it for monitoring.

FileSystemWatcher watcher = new FileSystemWatcher(); // Declares the FileSystemWatcher objectwatcher.Path = @"D:\MyDownloads"; // We have to specify the path which has to monitor
watcher.NotifyFilter = NotifyFilters.LastAccess NotifyFilters.LastWrite NotifyFilters.FileName notifyFilters.DirectoryName; // This property specifies which are the events to be monitoredwatcher.Filter = "*.txt"; // Only watch text files.// Add event handlers.
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
// Begin watching.
watcher.EnableRaisingEvents = true;


The following code for event handlers:

// Define the event handlers.
private static void OnChanged(object source, FileSystemEventArgs e)
{
// Specify what is done when a file is changed, created, or deleted.
MessageBox.Show("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
// Specify what is done when a file is renamed.
MessageBox.Show("File: "+ e.OldFullPath+" renamed to "+ e.FullPath);
}

Path class - VB.Net, C#.Net

The Path class provides methods for manipulating a file system path.
Path Methods

ChangeExtension: Takes a path and returns a new path with the file name's extension changed. (Note that only the path string changes, not the actual file name extension.)
Combine: Combines two compatible path strings.
GetDirectoryName: Returns the name of the directory in the specified path.
GetExtension: Returns the name of the file extension in the specified path.
GetFileName: Returns the name of the file in the specified path.
GetFileNameWithoutExtension: Returns the file name without the extension in the
specified path.
GetFullPath: Returns a full path to the specified path. This method can be used to resolve relative paths.
GetPathRoot: Returns the root directory name in the specified path.
GetRandomFileName: Generates a random file name.
GetTempFileName: Generates a temporary file in the file system and returns the full path to the file.
GetTempPath: Returns the path to the temporary file directory for the current user or system.
HasExtension: Indicates whether a specified path's file name has an extension.
IsPathRooted: Indicates whether the specified path includes a root directory.

DriveInfo - Listing Drives in C#.Net

Example:
DriveInfo[] mydrives = DriveInfo.GetDrives();

foreach (DriveInfo mydrive in mydrives)
{
Console.WriteLine("Drive: {0}", mydrive.Name);
Console.WriteLine("Type: {0}", mydrive.DriveType);
}

Note:
- GetDrives() is a static( shared in VB.Net) method.

- DriveType enumeration has the following values:
CDRom - An optical drive. It can be CD-ROM, DVD, and so on.
Fixed - A fixed disk.
Network - A network mapped drive.
NoRootDirectory - A drive that does not have a root directory.
Ram - A RAM drive.
Removable - A drive that has removable media.
Unknown - The drive could not be determined

DirectoryInfo class: Listing files in the specified directory - VB.Net, C#.Net

The DirectoryInfo class provides the basic functionality to access and manipulate a single directory in the file system.

Properties:
Parent
Root
Methods:
Create
CreateSubDirectory
GetDirectories
GetFiles
GetFileSystemInfo
MoveTo

Example: This exaple will list all files in the specified directory

DirectoryInfo myDir = new DirectoryInfo(@"H:\movies");

Console.WriteLine("Directory: {0}", myDir.FullName);

foreach (FileInfo myfile in myDir.GetFiles())
{
Console.WriteLine("File: {0}", myfile.Name);
}

How to copy, move files in C# .Net

Example:
Use system.IO namespace

FileInfo myfile = new FileInfo(@"H:\myphoto.jpg");

if (myfile.Exists)
{
myfile.CopyTo(@"H:\myphoto.bak",true );
myfile.MoveTo(@"H:\photos\photo.jpg");
}

- If the second parameter is true, the existing file will be replaced.
- If we specify an invalid path in MoveTo() it will throw DirectoryNotFoundException.

How to Get Information about File in c#.Net

Example:

FileInfo myfile = new FileInfo(@"H:\myphoto.jpg");

if (myfile.Exists)
{
Console.WriteLine("File name : {0}", myfile.Name);
Console.WriteLine("Path : {0}", myfile.FullName);
Console.WriteLine("Creation Time: {0}", myfile.CreationTime);
Console.WriteLine("Length : {0}", myfile.Length);
}

Note: "@" this symbol is used in string type to turn off escape processing. i.e If escape processing is turned on compiler will look for valid escape characters, if there is any invalid escape symbol it will throw compilation error.
landscape paintings nature art landscape art sky art illustration art artwork art human art beautiful art landscape prints nature prints sky prints illustration prints artwork prints human prints beautiful prints beautiful special promotions nature special promotions sky special promotions illustration special promotions artwork special promotions beautiful canvas prints beautiful painting canvas prints beautiful framed prints beautiful painting framed prints beautiful posters beautiful painting posters