November 3, 2011

Web Garden - ASP.Net

When an application is hosted by multiple processes on the same server it is said to be a web garden environment.All IIS Requests process by worker process. By default each and every application pool contain single worker process. The worker process is a small Win32 shell of code that hosts the common language runtime (CLR) and runs managed code. It takes care of servicing requests for ASPX, ASMX, and ASHX resources. There is generally only one instance of this process on a given machine. All currently active ASP.NET applications run inside of it, each in a separate AppDomain.

Identical copies of the process run on all of the CPUs with affinity to the process, known as Web Garden.

Think of a web garden as a web farm, but all in the context of a single machine.

What are the Limitations?
You’d stuck with an application that assumes everything lives within the same worker process.

No InProc session state:
If you’re using session state in your application, then you need to use an out-of-process session state, such as the ASP.NET State Service or sessions stored in SQL Server. InProc session management won’t work because each worker process will be maintaining its own session state. If you use an out-of-process session state, then you can be sure that all worker processes are consulting the same single resource as the place to store and retrieve session data.

No built-in caching mechanism:
If you’re using in-proc Caching, you need to remember that each individual worker process is going to maintain its own cache. This can make an operation such as clearing the cache difficult.
A better solution would be to move to an out-of-process cache like memcached. Then you only have one service that you need to clear the cache for, and that cache clearing action will be observed by all worker processes.

Advantages:
  • Robust processing of requests. When a worker process in an application pool is tied up (for example, when a script engine stops responding), other worker processes can accept and process requests for the application pool.
  • There is also a reduced contention for resources. When a Web garden reaches a steady state, each new TCP/IP connection is assigned, according to a round robin scheme, to a worker process in the Web garden. This helps smooth out workloads and reduce contention for resources that are bound to a worker process.
  • Web gardening helps enable scalability on multiprocessor computers by distributing the work to several processes. This improves performance and eliminates cross processor lock contentions.
  • Application availability, Processes in a Web garden share the requests that arrive for that particular application pool. If one worker process fails, another worker process can continue to process requests.
  • Optimum utilization of processes running on multiple processors located in a single server
  • Binding the application to processor with the help of CPU masks (Processor affinity), applications can be swapped out and restarted on the fly.
  • Could be said as a provisional solution for faulty applications.
Disadvantages :
  • Many applications assume dedicated access to some system level resource.  when this kind of application starts, it takes exclusive access to a resource; so when a second version of the application starts, it fails to get this exclusive access.
  • Prevent the use of session state in the process. So in statefull application you have another performance penalty to serialize the state to an external store.
  • Low Performance for large volumes of data.

Scenerios Where could Use :
  • Application need to share resources and hence can cause contention of resources.
  • The application is not very CPU intensive.
  • There exists multiple instances of an application, each of which can be allocated a worker process.

October 29, 2011

Difference between a.Equals(b) and a == b?

Simply we could say,
a==b  checks equality of values
a.Equals(b))  checks the equality of references(objects)

Value Types :
When a & b are of same data type
int a = 1;
int b = 1;
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
When a & b are of different data types
int a = 1;
long b = 1;
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns false
It shows that a.Equals(b) not only checks for content, but also their data types.

Reference Types :
Compare StringBuilder objects.
StringBuilder a = new StringBuilder("testing");
StringBuilder b = new StringBuilder("testing");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
a==b returns false, because these are two different objects refenreces
a.Equals(b) returns true, because there content/values are equal


Lets consider a custom class Person objects.
public class Person
{
 string Name;

 public Person(string name)
 {
  Name = name;
 }
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns false
a==b returns false, similaly like StringBuilder example, as two different objects refenreces
a.Equals(b) returns false,  Unlike StringBuilder example, because here content/values are not checking by-default, in order to check the internal contents of objects we have to put our custom logic, as in next example.


Lets add our own Equals() method override to put logic to compare internal content of objects. We are checking the Name of both objects.
So now Person class will look like:
public class Person
{
 string Name;

 public Person(string name)
 {
  Name = name;
 }

 public override bool Equals(object obj)
 {
  if (obj.GetType() != this.GetType())
  {
   return false;
  }

  Person p = obj as Person;
  return Name.Equals(p.Name);
 }

 public override int GetHashCode()
 {
  return base.GetHashCode();
 }
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns false
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
a.Equals(b) now returns true, because now its checking Name of both objects for equality.

Moreover if you want to put cusomt checking for == operator, you even can do this by overloading ==operator.

In next example we add two overloads for ==operator and !=operator in Person class :
public static bool operator ==(Person a, Person b)
{
 // If both are null, or both are same instance, return true.
 if (System.Object.ReferenceEquals(a, b))
 {
  return true;
 }

 // If one is null, but not both, return false.
 if (((object)a == null) || ((object)b == null))
 {
  return false;
 }

 return a.Name.Equals(b.Name);
}

public static bool operator !=(Person a, Person b)
{
 return !(a == b);
}

Person a = new Person("Person");
Person b = new Person("Person");
Console.WriteLine("a==b > {0}", a == b);// Returns true
Console.WriteLine("a.Equals(b) > {0}", a.Equals(b));// Returns true
Now both techniques will return true, because both are checking the same data.

For further guidelines about overloading Equals () or operator overloading, please visit MSDN:
http://msdn.microsoft.com/en-us/library/ms173147%28v=vs.80%29.aspx

October 9, 2011

Exe, Dll and Ocx files

EXE :
  • Exe has only one main entry point (it contains a startup function etc).
  • Exe is an out of process server, when the system launches new exe, a new process is created.
  • When exe is lanuched, it occupised its own memory space.
  • The exe’s entry thread is called in context of main thread of that process.
  • The exe can process requests on an independent thread of execution, notifying the client of task completion using events or asynchronous call-backs. This frees the client to respond to the user.
  • If an error occurs the client processes can continue to operate.
  • Generally slower than an Dll alternative.

DLL :
  • A Dll runs is an in process server running in the same memory space as the client process.
  • DLLs are loaded into an exe application (a Dll can't run by itlsef). If tried to run it directly , it will display an error about a missing entry point.
  • In-process component shares its client’s address space, so property and method calls don’t have to be marshaled. This results in much faster performance.
  • If an unhandled error occurs it will cause the client process to stop operating.
  • That in most cases DLLs have an export section where symbols are exported.
  • DLL can be reused and versioned. It reduces storage space as different programs/files can use the same dll.
  • DLL does not have a main entry point. Binding occurs at runtime, that’s why it is called "Dynamic Link" library.
  • The system loads a DLL into the context of an existing thread.

OCX :
  • An OCX is used where a visual interface is required for the function , and in the 3rd party controls you use.
  • Ocx's are interfaces that you can place on a form. Like a textbox or a picturebox.
  • Very often, you could think of an OCX as an extension of the VB IDE controls.
  • They need to be driven by the form's thread and can not exist without a parent form.
  • An OCX is a file that can hold one or more ActiveX controls. These files do not need to have the .ocx extension (some are .dll files) and thus should not be referred to as "OCXs".