Friday, 17 January 2014

WCF In Dot Net


Introduction of WCF:  WCF stands for Windows Communication Foundation and is part of .NET 3.0. WCF is Microsoft platform for building distributed and interoperable applications.

What is a distributed application or connected systems?
Distributed application, is an application where parts of it run on 2 or more computers.

Why distributed applications is used?
1. An enterprise application may need to use the services provided by other enterprises. For example an ecommerce application may be using Paypal service for payments.
2. For better scalability. An enterprise web application may have Presentation tier, Business tier, and Data Access tier, and each tier may be running on a different machine.

What is an interoperable application?
An application that can communicate with any other application that is built on any platform and using any programming language is called as an interoperable application. Web services are interoperable, where as .NET remoting services are not.

Web services can 
communicate with any application built on any platform, where as a .NET remoting service can be consumed only by a .net application.

What technology choices did we have before WCF to build distributed applications?
Enterprise Services
Dot Net Remoting
Web Services

Why should we use WCF?
We have 2 clients and we need to implement a service a for them. 
1. The first client is using a Java application to interact with our service, so for interoperability this client wants messages to be in XML format and the protocol to beHTTP.
2. The second client uses .NET, so for better performance this client wants messages formatted in binary over TCP protocol.

Without WCF
1.To satisfy the first client requirement we end up implementing an ASMX web service, and 


2. To satisfy the second client requirement we end up implementing a remoting service 



These are 2 different technologies, and have complete different programming models.So the developers have to learn different technologies. 

So to unify and bring all these technologies under one roof Microsoft has come up with a single programming model that is called as WCF - Windows Communication Foundation.

With WCF,
You implement one service and we can configure as many end points as want to support all the client needs. To support the above 2 client requirements, we would configure 2 end points. In the endpoint configuration we can specify the protocols and message formats that we want to use.






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(); 
}
}

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...