Wednesday, 25 December 2013

Interview Questions on web farm and web garden in Dot Net


Web Garden- Web application deployed on a server with multiple processors.
Web Farm- Web application deployed on multiple server.

Q : What is the difference between a web farm and a web garden?
Ans :Web Farm :-After developing our  web application we host it on IIS Server.  Now one standalone server is sufficient to process ASP.NET Request and response for a small web sites but when the site comes for big organization where there an millions of daily user hits then we need to host the sites on multiple Server. This is called web farms. Where single site hosted on multiple IIS Server and they are  running behind the Load Balancer.
Web Garden :-All IIS Request process by worker process ( w3wp.exe). By default each and every application pool contain single worker process. But An application pool with multiple worker process is called Web Garden.   Many worker processes with same Application Pool can sometimes provide better throughput performance and application response time. And Each Worker Process Should have there own Thread and Own Memory space.

Q : How to configure our web garden?
Right Click on Application Pool > Properties > GoTo Performance Tab 
In bottom Group Section  Increase the Worker Process Count.

Q : What is the restriction in web garden?
There are some Certain Restriction to use Web Garden with your web application. If we use Session Mode to "in proc", our application will not work correctly because session will be handled by different Worker Process. For Avoid this Type of problem we should have to use Session Mode "out proc" and we can use "Session State Server" or "SQL-Server Session State".

Q : What are the implications on Application and Session state variables in a web farm or a web garden?
Ans : In both a Web garden and a Web farm, client requests are directed to the ASP.NET process that is currently least busy. That means that a single client can interact with different CPUs or servers over the course of his or her session. This has the following implications for Application and Session state variables:
Q : Application state variables are unique to each separate instance of the Web application.
Ans :Clients can share information through Application state if the Web application is running on a Web garden or a Web farm.
Q : Session state variables are stored in-process by default.
Ans : To enable Session state in a Web garden or Web farm, you need to specify a Session state provider.
Q : How can you share Application State in a web farm or a web garden?
Ans : To share data across multiple sessions in a Web garden or Web farm, you must save and restore the information using a resource that is available to all the processes. This can be done through an XML file, a database, or some other resource using the standard file or database access methods.
Q : What are the two built-in ways provided by ASP.NET to share Session state information across a Web garden or Web farm?
Ans : ASP.NET provides two built-in ways to share Session state information across a Web garden or Web farm. You can share Session state using:

A state server, as specified by a network location
This technique is simple to implement and doesn’t require you to install Microsoft SQL Server.
A SQL database, as specified by a SQL connection
This technique provides the best performance for storing and retrieving state information.
Q : What are the steps to follow to share Session state information using a state server?
Ans : To share Session state information using a state server, follow these steps:
1. In the Web application’s Web.config file, set the sessionState element’s mode and stateConnectionString attributes.
2. Run the aspnet_state.exe utility on the Session state server. The aspnet_state.exe utility is installed in the \WINDOWS\Microsoft.NET \Framework\version folder when you install Visual Studio .NET Professional or Visual Studio .NET Enterprise Architect editions.
Q : What are the steps to follow to share Session state information using a SQL database?
Ans :To share Session state information using a SQL database, follow these steps:
1. In the Web application’s Web.config file, set the sessionState element’s mode and sqlConnectionString attributes.
2. Run the InstallSqlState.sql utility on the Session state server. This utility installs the SQL database that shares Session state information across processes. The InstallSqlState.sql utility is installed in the \WINDOWS\Microsoft.NET \Framework\version folder when you install Visual Studio .NET Professional, Visual Studio .NET Enterprise Developer, or Visual Studio .NET Enterprise Architect editions.

Q : What does the term Scalability mean?
Ans :scalability is the ability of a system, network, or process to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth.For example, it can refer to the capability of a system to increase total throughput under an increased load when resources (typically hardware) are added. ASP.NET Web applications support this concept through their ability to run in multiple processes and to have those processes distributed across multiple CPUs and/or multiple servers.

Type of session in Dot net

How many types of Session :-


Priviously we discussed  about Session and session interview question We will talk about types of session.
Types of session

  1. In-process session
  2. Out process session / State server session
  3. Sql server session 
  4. Off session mode 
  5. Custom session mode 

In process Session mode

When the session state mode set to inProc :-The session state variable are stored on the web server memory inside the asp.net worker process.This is default session state mode.
It is very helpful small website or where number of user very less.we should avoid InProc in "web garden".

Web Garden- Web application deployed on a server with multiple processors.
Web Farm- Web application deployed on multiple server.
For detail of "Web garden" and "web farm" click here

  1. Off - Disables session state for entire web application 
  2. Inproc - 
      Fig(1)

      Fig(2)

    Fig(3)
Video for Inproc session mode in Dot Net
 Now the behind the web Form 1(Fig -1) coding will (coding for sending the data from one web  form to another web form)

Protected void btnsenddata_Click( Object sender , Event args e)
{
      Session ["name"] = textbox1.text;
      Session ["Email"] = textbox2.text;
Response.redirect("webform2.aspx");

}

Now the behind the web Form 1(Fig -1)
Protected void Page_Load( Object sender , Event args e)
{    
     if(Session ["name"] ! = null)
{
      lable1.text = Session ["name"].ToString();
}
  
     if(Session ["Email"] ! = null)
 {
     lable2.text = Session ["Email"] .ToString();

}
}

After this we have to set in web Config file
<Configuration>
<system.web>
<sessionState mode = "Inproc" timeout = "20"
</sessionstate>

For More Detail Watch Above video...

Note - If we set session state mode as "Off" then Disables session state for entire web application.



Advantages Disadvantages
Easy to implement, all time it require is, to set, The session state mod=inproc
in web config file.
Session state data is lost when the worker process or application pool is recycled 
It is best because the session state memory rept on the web server with in the Asp.net worker process.Not suitable for webform & webgarden.
Suitable for web application hosted on a single server.,,
Object can be added without serialization.
Scalability could be on an issue. 

Out process/ State server Session mode



  • State server uses a stand alone window services which is independent of IIS and can also be run on separate server.
  • This session state is totally managed by Asp.net-state.exe.
  • This server may run on the same system, But its outside of the main application domain where your web application is running. This means if you restart your application process your session data will be alive.
  • This has many disadvantages due the overhead of the serialization & De-serialization involved. it also increase the cost of the data access because every time the user retrieves session data.  



 Now the behind the web Form 1(Fig -1) coding will (coding for sending the data from one web  form to another web form)

Protected void btnsenddata_Click( Object sender , Event args e)
{
      Session ["name"] = textbox1.text;
      Session ["Email"] = textbox2.text;
Response.redirect("webform2.aspx");

}

Now the behind the web Form 1(Fig -1)
Protected void Page_Load( Object sender , Event args e)
{    
     if(Session ["name"] ! = null)
{
      lable1.text = Session ["name"].ToString();
}
  
     if(Session ["Email"] ! = null)
 {
     lable2.text = Session ["Email"] .ToString();

}
}

After this we have to set in web Config file
<Configuration>
<system.web>
<sessionState mode = "StateServer"  StateconnectionString =" tcpip = localhost = ipaddress"
  timeout = "20"
</sessionstate>

For More Detail Watch Above video...


Sql server session 

When Should be used SQL Server session mode :-
  • SQL Server session mode is a more reliable & secure session state management.
  • It keeps data in a centralized location (database)
  • we should use SQL Server session mode when we need to implement session with more security.
  • If there happens to be frequent server restarts, This is an ideal choice.
  • This is the perfect mode for web form and web garden security.
  • We can use SQL Server session mode when we need to share session  b/w two different application.





Protected void btnsenddata_Click( Object sender , Event args e)
{
      Session ["name"] = textbox1.text;
      Session ["Email"] = textbox2.text;
Response.redirect("webform2.aspx");

}

Now the behind the web Form 1(Fig -1)
Protected void Page_Load( Object sender , Event args e)
{    
     if(Session ["name"] ! = null)
{
      lable1.text = Session ["name"].ToString();
}
  
     if(Session ["Email"] ! = null)
 {
     lable2.text = Session ["Email"] .ToString();

}
}

After this we have to set in web Config file
<Configuration>
<system.web>
<sessionState mode = "SqlServer"  SqlConnectionString =" DataSource = ;integrated security  =SSPI"
  timeout = "20"
</sessionstate>

For More Detail Watch Above video...





Partial class in C#

Partial class is a new feature added to C# 2.0 and Visual Studio 2005. It is supported in .NET Framework 2.0. If you are working with .NET 1.0 or 1.1, partial classes may not work. 
It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

Benefit of partial classes:

1) More than one developer can simultaneously write the code for the class.

2) You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.

There are a few things that you should be careful about when writing code for partial classes: 
  • All the partial definitions must proceeded with the key word "Partial".
  • All the partial types meant to be the part of same type must be defined within a same assembly and module.
  • Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially).
  • The partial types must have the same accessibility.
  • If any part is sealed, the entire class is sealed.
  • If any part is abstract, the entire class is abstract.
  • Inheritance at any partial type applies to the entire class.
I have attached code of the partial classes along with this article. You can open the project and understand the functionality.

Hope the article would have helped you in understanding what partial classes are. Waiting for your feedback.
Example
//partial class


public partial class Student

{
public virtual void GetRollNo();
}

public partial class Student
{
public virtual void GetStudentName();
}

//Derived class

public class School : Student
{
public void getStudentDetails()
{
GetRollNo();
getStudentDetails(); 
}
}

Tuesday, 24 December 2013

Delegate in C#

                                                                                                                                            Previous.....
                                                                                                                                                      Next....       Delegates

A delegate is a typesafe function pointer. delgate is pointing to function.
A Delegate is similar to a class. You can create an instance of it, and when you do so, you pass in the function name as a parameter to the delegate constructor, and it is to this function the delegate will point to.

A delegate is a function pointer delegate is a type safe function pointer. Actually a delegates points to the function when we invoke the delegate the function will be invoked.

Use of Delegate: Reason is because of the flexibilty we will get in this approach(Delegate)
30. What is the main use of delegates in C#?
Delegates are mainly used to define call back methods.

syntax of delegate is similar to the function.

Delegate can be used to point to a function which is similar signature. This delegate can point to any function that has got void return type & string parameter

How to make delegate point to a function to do that u have to create a instance of the delegate this is where a delegate is similar to a class.

And to the constructor of this delegate you pass in the name of the function to which you want this delegate to point.
the parameter must be a method name that method should have a void return type and string paramer.
Delegate is a type safe function pointer.That is, they hold reference(Pointer) to a function. 

The signature of the delegate must match the signature of the function, the delegate points to, otherwise you get a compiler error. This is the reason delegates are called as type safe function pointers.


Tip to remember delegate syntax: Delegates syntax look very much similar to a method with a delegate keyword.
-----------------------------------------------------------------------------------------

Some of the advantages of Delagates are:-

1.It encapsulates the method call
2.It increases the performance of the application
3.We can call a method Asynchronously using a delegate.We have multicast delegates as well..

There is a lot confusion around in developer community about the exact use of delegates. Here is what delegates are.

Delegate is a way to encapulate a method call as an object. This is very important to understand that when you create a delegate you are in essense creating an object that encapsulates a method call. Since you are wrapping a method call as an object, you can do things with delegates that are only possible with objects. you can store them, you can pass them as method parameters, you can invoke them (so that they invoke target methods) when timings and conditions are right.

just consider following statement

string str = obj.SomeMethod();

when CLR comes accross this statement, it will execute the targer method(obj.SomeMethod()). But what if I want to store this call to a method and invoke only if some conditions are met?? can i do

List<method> myMethods = new List<method>(); to store them?? NO!!!!

but this is possible with delegates.

here is a code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate1
{
    public delegate void HellowFunctionDelegate(string message);

    class Program
    {
        static void Main(string[] args)
        {
            HellowFunctionDelegate del = new HellowFunctionDelegate(Hellow);
            del("Hellow from Delegate");
            Console.ReadLine();
        }

        public static void Hellow(string message)
        {
            Console.WriteLine(message);
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Delegate1
{
    public delegate void HellowFunctionDelegate(string message);

    class Program
    {
        static void Main(string[] args)
        {
            List<Employee> emplist = new List<Employee>();
            emplist.Add(new Employee() { ID = 1, Name = "Sachin", Salary = 50000, Experience = 3 });
            emplist.Add(new Employee() { ID = 2, Name = "Yuvraj", Salary = 30000, Experience = 2 });
            emplist.Add(new Employee() { ID = 3, Name = "Rahul", Salary = 80000, Experience = 7 });
            emplist.Add(new Employee() { ID = 4, Name = "Kiran", Salary = 70000, Experience = 5 });


            IsPromotable isPromotable = new IsPromotable(Pramote);
            Employee.PromotEmployee(emplist, isPromotable);
Employee.PromotEmployee(emplist, emp => emp.Experience >= 5);        }
        public static bool Pramote(Employee e)
        {
            if (e.Experience >= 5)
            {
                return true;
            }
            else
            {
                return false;                    
            }
        }
    }

    delegate bool IsPromotable(Employee emp);

    class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public int Salary { get; set; }
        public int Experience { get; set; }

        public static void PromotEmployee(List<Employee> employeeList, IsPromotable IsEligibleToPramote)
        {
            foreach (Employee e in employeeList)
            {
                if (IsEligibleToPramote(e))
                {
                    Console.WriteLine(e.Name + " Promoted");
                }
            }

        }

    }
}


  


                                                                                                                                            Previous.....                                                                                                                                                                                                                                                                                                                        Next....       




C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...