Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts
What are ADO.net data services?
An ADO.net data service is a combination of the runtime and a web service through which the services are exposed. ADO.NET Data Services exposes data, represented as Entity Data Modal objects, via web services accessed over HTTP. The data can be addressed using URI. The ADO.net data service, when accessed via the HTTP GET method with such a URI, will return the data. The web service can be configured to return the data in either plain, XML, JSON.
What are the different advantages of using generic collection classes over non generic collection classes?
Generics collection classes provide the solution to a limitation in earlier versions of the common language runtime and the C# language in which generalization is accomplished by casting types to and from the universal base type Object. By creating a generic class, you can create a collection that is type-safe at compile-time.
Difference Between Web Farm and Web Garden
Introduction
Web Farms and Web Garden are very common terminology for any production deployment. Though these terms looks same but the things are totally different. Many beginners very confused with these two terms. Here I am giving the basic difference between the Web Farm and Web Garden.
Web Farm
After developing our asp.net 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.
This is the most common scenarios for any web based production environment. Where Client will hit an Virtual IP ( vIP) . Which is the IP address of Load Balancer. When Load balancer received the request based on the server load it will redirect the request to particular Server.
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.
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".
How To Configure Web Garden?
Right Click on Application Pool > Properties > GoTo Performance Tab
In bottom Group Section Increase the Worker Process Count.
Web Farms and Web Garden are very common terminology for any production deployment. Though these terms looks same but the things are totally different. Many beginners very confused with these two terms. Here I am giving the basic difference between the Web Farm and Web Garden.
Web Farm
After developing our asp.net 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.
This is the most common scenarios for any web based production environment. Where Client will hit an Virtual IP ( vIP) . Which is the IP address of Load Balancer. When Load balancer received the request based on the server load it will redirect the request to particular Server.
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.
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".
How To Configure Web Garden?
Right Click on Application Pool > Properties > GoTo Performance Tab
In bottom Group Section Increase the Worker Process Count.
When we will use an Interface and Abstract class?
Use an abstract class
When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
Use an abstract class to define a common base class for a family of types.
Use an abstract class to provide default behavior.
Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
Use an interface to design a polymorphic hierarchy for value types.
Use an interface when an immutable contract is really intended.
A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.
When creating a class library which will be widely distributed or reused—especially to clients, use an abstract class in preference to an interface; because, it simplifies versioning. This is the practice used by the Microsoft team which developed the Base Class Library. ( COM was designed around interfaces.)
Use an abstract class to define a common base class for a family of types.
Use an abstract class to provide default behavior.
Subclass only a base class in a hierarchy to which the class logically belongs.
Use an interface
When creating a standalone project which can be changed at will, use an interface in preference to an abstract class; because, it offers more design flexibility.
Use interfaces to introduce polymorphic behavior without subclassing and to model multiple inheritance—allowing a specific type to support numerous behaviors.
Use an interface to design a polymorphic hierarchy for value types.
Use an interface when an immutable contract is really intended.
A well-designed interface defines a very specific range of functionality. Split up interfaces that contain unrelated functionality.
What is Web Part control in ASP.Net?
ASP.NET Web Parts is an integrated set of controls for creating Web sites that enable end users to modify the content, appearance, and behavior of Web pages directly from a browser. The modifications can be applied to all users on the site or to individual users. When users modify pages and controls, the settings can be saved to retain a user's personal preferences across future browser sessions, a feature called personalization. These Web Parts capabilities mean that developers can empower end users to personalize a Web application dynamically, without developer or administrator intervention.
Using the Web Parts control set, you as a developer can enable end users to:
Personalize page content. Users can add new Web Parts controls to a page, remove them, hide them, or minimize them like ordinary windows.
Personalize page layout: Users can drag a Web Parts control to a different zone on a page, or change its appearance, properties, and behavior.
Export and import controls: Users can import or export Web Parts control settings for use in other pages or sites, retaining the properties, appearance, and even the data in the controls. This reduces data entry and configuration demands on end users.
Create connections: Users can establish connections between controls so that, for example, a chart control could display a graph for the data in a stock ticker control. Users could personalize not only the connection itself, but the appearance and details of how the chart control displays the data.
Manage and personalize site-level settings: Authorized users can configure site-level settings, determine who can access a site or page, set role-based access to controls, and so on. For example, a user in an administrative role could set a Web Parts control to be shared by all users, and prevent users who are not administrators from personalizing the shared control.
Using the Web Parts control set, you as a developer can enable end users to:
Personalize page content. Users can add new Web Parts controls to a page, remove them, hide them, or minimize them like ordinary windows.
Personalize page layout: Users can drag a Web Parts control to a different zone on a page, or change its appearance, properties, and behavior.
Export and import controls: Users can import or export Web Parts control settings for use in other pages or sites, retaining the properties, appearance, and even the data in the controls. This reduces data entry and configuration demands on end users.
Create connections: Users can establish connections between controls so that, for example, a chart control could display a graph for the data in a stock ticker control. Users could personalize not only the connection itself, but the appearance and details of how the chart control displays the data.
Manage and personalize site-level settings: Authorized users can configure site-level settings, determine who can access a site or page, set role-based access to controls, and so on. For example, a user in an administrative role could set a Web Parts control to be shared by all users, and prevent users who are not administrators from personalizing the shared control.
How to create dynamic Gridview in ASP.Net?
Many times we have the requirement where we have to create columns dynamically.
This article describes you about the dynamic loading of data using the DataTable as the datasource.
Create a gridview in the page,
Drag and drop the GridView on to the page Or Manually type GridView definition in the page.
public partial class _Default : System.Web.UI.Page
{
#region constants
const string NAME = "NAME";
const string ID = "ID";
#endregion
protected void Page_Load(object sender, EventArgs e)
{
loadDynamicGrid();
}
private void loadDynamicGrid()
{
#region Code for preparing the DataTable
//Create an instance of DataTable
DataTable dt = new DataTable();
//Create an ID column for adding to the Datatable
DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));
dcol.AutoIncrement = true;
dt.Columns.Add(dcol);
//Create an ID column for adding to the Datatable
dcol = new DataColumn(NAME, typeof(System.String));
dt.Columns.Add(dcol);
//Now add data for dynamic columns
//As the first column is auto-increment, we do not have to add any thing.
//Let's add some data to the second column.
for (int nIndex = 0; nIndex < 10; nIndex++)
{
//Create a new row
DataRow drow = dt.NewRow();
//Initialize the row data.
drow[NAME] = "Row-" + Convert.ToString((nIndex + 1));
//Add the row to the datatable.
dt.Rows.Add(drow);
}
#endregion
//Iterate through the columns of the datatable to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
GrdDynamic.Columns.Add(bfield);
}
//Initialize the DataSource
GrdDynamic.DataSource = dt;
//Bind the datatable with the GridView.
GrdDynamic.DataBind();
}
}
This article describes you about the dynamic loading of data using the DataTable as the datasource.
Create a gridview in the page,
Drag and drop the GridView on to the page Or Manually type GridView definition in the page.
public partial class _Default : System.Web.UI.Page
{
#region constants
const string NAME = "NAME";
const string ID = "ID";
#endregion
protected void Page_Load(object sender, EventArgs e)
{
loadDynamicGrid();
}
private void loadDynamicGrid()
{
#region Code for preparing the DataTable
//Create an instance of DataTable
DataTable dt = new DataTable();
//Create an ID column for adding to the Datatable
DataColumn dcol = new DataColumn(ID ,typeof(System.Int32));
dcol.AutoIncrement = true;
dt.Columns.Add(dcol);
//Create an ID column for adding to the Datatable
dcol = new DataColumn(NAME, typeof(System.String));
dt.Columns.Add(dcol);
//Now add data for dynamic columns
//As the first column is auto-increment, we do not have to add any thing.
//Let's add some data to the second column.
for (int nIndex = 0; nIndex < 10; nIndex++)
{
//Create a new row
DataRow drow = dt.NewRow();
//Initialize the row data.
drow[NAME] = "Row-" + Convert.ToString((nIndex + 1));
//Add the row to the datatable.
dt.Rows.Add(drow);
}
#endregion
//Iterate through the columns of the datatable to set the data bound field dynamically.
foreach (DataColumn col in dt.Columns)
{
//Declare the bound field and allocate memory for the bound field.
BoundField bfield = new BoundField();
//Initalize the DataField value.
bfield.DataField = col.ColumnName;
//Initialize the HeaderText field value.
bfield.HeaderText = col.ColumnName;
//Add the newly created bound field to the GridView.
GrdDynamic.Columns.Add(bfield);
}
//Initialize the DataSource
GrdDynamic.DataSource = dt;
//Bind the datatable with the GridView.
GrdDynamic.DataBind();
}
}
What is Static Class in .Net?
Static classes are used when a class provides functionality that is not specific to any unique instance. Here are the features of static classes in C# 2.0.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
Static classes can not be instantiated.
Static classes are sealed so they can not be inherited.
Only static members are allowed.
Static classes can only have static constructor to initialize static members.
Advantages
Compiler makes sure that no instance of static class is created. In previous version of C#, the constructor has to be marked private to avoid this from happening.
Also compiler makes sure that no instance members are declared within a static class.
How can we use COM Components in .NET?
.NET components communicate with COM using RCW (Runtime Callable Wrapper). Following
are the ways with which you can generate RCW :-
√ Adding reference in Visual Studio.net. Wrapper class is generated and placed in the “BIN” directory.
√ Using Type library import tool. Tlbimp.exe yourname.dll.
√ Using interopservices.System.runtime.Interopservices namespace contains class
TypeLib Converter which provides methods to convert COM classes and interface in
to assembly metadata.
√ Make your custom wrappe rs.If your COM component does not have type library
then the only way to communicate is writing custom wrappers. That means
communicating directly with COM components.
are the ways with which you can generate RCW :-
√ Adding reference in Visual Studio.net. Wrapper class is generated and placed in the “BIN” directory.
√ Using Type library import tool. Tlbimp.exe yourname.dll.
√ Using interopservices.System.runtime.Interopservices namespace contains class
TypeLib Converter which provides methods to convert COM classes and interface in
to assembly metadata.
√ Make your custom wrappe rs.If your COM component does not have type library
then the only way to communicate is writing custom wrappers. That means
communicating directly with COM components.
What is the difference between System exceptions and Application exceptions?
All exception derives from Exception Base class. Exceptions can be generated programmatically or can be generated by system. System Exception serves as the base class for all applicationspecific exception classes. It derives from Exception but does not provide any extended functionality. You should derive your custom application exceptions from Application Exception.
Application exception is used when we want to define user defined exception, while system exception is all which is defined by .NET.
Application exception is used when we want to define user defined exception, while system exception is all which is defined by .NET.
What is the difference between VB.NET and C# ?
Well this is the most debatable issue in .NET community and people treat there languages like religion. Its a subjective matter which language is best. Some like VB.NET’s natural style and some like professional and terse C# syntaxes. Both use the same framework and speed is also very much equivalents. But still let’s list down some major differences between them :-
Advantages VB.NET :-
√ Has support for optional parameters which makes COM interoperability much easy.
√ With Option Strict off late binding is supported.Legacy VB functionalities can be
used by using Microsoft.VisualBasic namespace.
√ Has the WITH construct which is not in C#.
√ The VB.NET part of Visual Studio .NET compiles your code in the background.
While this is considered an advantage for small projects, people creating very large
projects have found that the IDE slows down considerably as the project gets larger.
Advantages of C#
√ XML documentation is generated from source code but this is now been incorporated
in Whidbey.
√ Operator overloading which is not in current VB.NET but is been introduced in
Whidbey.
√ Use of this statement makes unmanaged resource disposal simple.
√ Access to Unsafe code. This allows pointer arithmetic etc, and can improve
performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies).This is the major difference that you can access unmanaged code in C# and not in VB.NET.
Advantages VB.NET :-
√ Has support for optional parameters which makes COM interoperability much easy.
√ With Option Strict off late binding is supported.Legacy VB functionalities can be
used by using Microsoft.VisualBasic namespace.
√ Has the WITH construct which is not in C#.
√ The VB.NET part of Visual Studio .NET compiles your code in the background.
While this is considered an advantage for small projects, people creating very large
projects have found that the IDE slows down considerably as the project gets larger.
Advantages of C#
√ XML documentation is generated from source code but this is now been incorporated
in Whidbey.
√ Operator overloading which is not in current VB.NET but is been introduced in
Whidbey.
√ Use of this statement makes unmanaged resource disposal simple.
√ Access to Unsafe code. This allows pointer arithmetic etc, and can improve
performance in some situations. However, it is not to be used lightly, as a lot of the normal safety of C# is lost (as the name implies).This is the major difference that you can access unmanaged code in C# and not in VB.NET.
What is concept of Boxing and Unboxing ?
Boxing permits any value type to be implicitly converted to type object or to any interface type implemented by value type. Boxing is a process in which object instances are created and copy values in to that instance.
Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.
Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa.
Dim x As Integer
Dim y As Object
x = 10
‘ boxing process
y = x
‘ unboxing process
x = y
Unboxing is vice versa of boxing operation where the value is copied from the instance in to appropriate storage location.
Below is sample code of boxing and unboxing where integer data type is converted in to object and then vice versa.
Dim x As Integer
Dim y As Object
x = 10
‘ boxing process
y = x
‘ unboxing process
x = y
What are Value types and Reference types ?
Value types directly contain their data which are either allocated on the stack or allocated in-line in a structure.
Reference types store a reference to the value's memory address, and are allocated on the heap.
Reference types can be self-describing types, pointer types, or interface types.
Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. All types derive from the System.Object base type.
Reference types store a reference to the value's memory address, and are allocated on the heap.
Reference types can be self-describing types, pointer types, or interface types.
Variables that are value types each have their own copy of the data, and therefore operations on one variable do not affect other variables. Variables that are reference types can refer to the same object; therefore, operations on one variable can affect the same object referred to by another variable. All types derive from the System.Object base type.
What is Delay signing ?
During development process you will need strong name keys to be exposed to developer which is not a good practice from security aspect point of view.In such situations you can assign the key later on and during development you an use delay signing
Following is process to delay sign an assembly:
√ First obtain your string name keys using SN.EXE.
√ Annotate the source code for the assembly with two custom attributes from System.Reflection: AssemblyKeyFileAttribute, which passes the name of the file containing the public key as a parameter to its constructor. AssemblyDelaySignAttribute,which indicates that delay signing, is being used by passing true as a parameter to its constructor. For example as shown below:
[Visual Basic]
Assembly:AssemblyKeyFileAttribute("myKey.snk")
Assembly:AssemblyDelaySignAttribute(true)
[C#]
[assembly:AssemblyKeyFileAttribute("myKey.snk")]
[assembly:AssemblyDelaySignAttribute(true)]
The compiler inserts the public key into the assembly manifest and reserves space in the PE file for the full strong name signature. The real public key must be stored while the assembly is built so that other assemblies that reference this assembly can obtain the key to store in their own assembly reference.
√ Because the assembly does not have a valid strong name signature, the verification of that signature must be turned off. You can do this by using the –Vr option with the Strong Name tool.The following example turns off verification for an assembly called myAssembly.dll.
Sn –Vr myAssembly.dll
√ Just before shipping, you submit the assembly to your organization's signing authority for the actual strong name signing using the –R option with the Strong Name tool.
The following example signs an assembly called myAssembly.dll with a strong name
using the sgKey.snk key pair.
Sn -R myAssembly.dll sgKey.snk
Following is process to delay sign an assembly:
√ First obtain your string name keys using SN.EXE.
√ Annotate the source code for the assembly with two custom attributes from System.Reflection: AssemblyKeyFileAttribute, which passes the name of the file containing the public key as a parameter to its constructor. AssemblyDelaySignAttribute,which indicates that delay signing, is being used by passing true as a parameter to its constructor. For example as shown below:
[Visual Basic]
Assembly:AssemblyKeyFileAttribute("myKey.snk")
Assembly:AssemblyDelaySignAttribute(true)
[C#]
[assembly:AssemblyKeyFileAttribute("myKey.snk")]
[assembly:AssemblyDelaySignAttribute(true)]
The compiler inserts the public key into the assembly manifest and reserves space in the PE file for the full strong name signature. The real public key must be stored while the assembly is built so that other assemblies that reference this assembly can obtain the key to store in their own assembly reference.
√ Because the assembly does not have a valid strong name signature, the verification of that signature must be turned off. You can do this by using the –Vr option with the Strong Name tool.The following example turns off verification for an assembly called myAssembly.dll.
Sn –Vr myAssembly.dll
√ Just before shipping, you submit the assembly to your organization's signing authority for the actual strong name signing using the –R option with the Strong Name tool.
The following example signs an assembly called myAssembly.dll with a strong name
using the sgKey.snk key pair.
Sn -R myAssembly.dll sgKey.snk
What is strong names ?
Strong name is similar to GUID(It is supposed to be unique in space and time) in COM
components.Strong Name is only needed when we need to deploy assembly in GAC. Strong
Names helps GAC to differentiate between two versions. Strong names use public key cryptography (PKC) to ensure that no one can spoof it.PKC use public key and private key concept.
Following are the step to generate a strong name and sign a assembly :-
√ Go to “Visual Studio Command Prompt”.
√ After you are in command prompt type sn.exe -k “c:\test.snk”.
√ After generation of the file you can view the SNK file in a simple notepad.
√ After the SNK file is generated its time to sign the project with this SNK file.
√ Click on project -- properties and the browse the SNK file to the respective
folder and compile the project.
components.Strong Name is only needed when we need to deploy assembly in GAC. Strong
Names helps GAC to differentiate between two versions. Strong names use public key cryptography (PKC) to ensure that no one can spoof it.PKC use public key and private key concept.
Following are the step to generate a strong name and sign a assembly :-
√ Go to “Visual Studio Command Prompt”.
√ After you are in command prompt type sn.exe -k “c:\test.snk”.
√ After generation of the file you can view the SNK file in a simple notepad.
√ After the SNK file is generated its time to sign the project with this SNK file.
√ Click on project -- properties and the browse the SNK file to the respective
folder and compile the project.
What is GAC ?
GAC (Global Assembly Cache) is used where shared .NET assembly reside. GAC is used in the following situations :-
√ If the application has to be shared among several application.
√ If the assembly has some special security requirements like only administrators
can remove the assembly. If the assembly is private then a simple delete of assembly the assembly file will remove the assembly.
√ If the application has to be shared among several application.
√ If the assembly has some special security requirements like only administrators
can remove the assembly. If the assembly is private then a simple delete of assembly the assembly file will remove the assembly.
What is Manifest?
following things (See Figure Manifest View for more details):
√ Version of assembly
√ Security identity
√ Scope of the assembly
√ Resolve references to resources and classes.
√ The assembly manifest can be stored in either a PE file (an .exe or .dll) with
Microsoft intermediate language (MSIL) code or in a stand-alone PE file that
contains only assembly manifest information.
√ Version of assembly
√ Security identity
√ Scope of the assembly
√ Resolve references to resources and classes.
√ The assembly manifest can be stored in either a PE file (an .exe or .dll) with
Microsoft intermediate language (MSIL) code or in a stand-alone PE file that
contains only assembly manifest information.
What is Difference between NameSpace and Assembly?
Following are the differences between namespace and assembly :
√ Assembly is physical grouping of logical units. Namespace logically groups
classes.
√ Namespace can span multiple assembly.
√ Assembly is physical grouping of logical units. Namespace logically groups
classes.
√ Namespace can span multiple assembly.
What is NameSpace?
Namespace has two basic functionality :-
√ In Object Oriented world many times its possible that programmers will use the
same class name.By qualifying NameSpace with classname this collision is able to
be removed.
√ NameSpace Logically group types, example System.Web.UI logically groups
our UI related features.
√ In Object Oriented world many times its possible that programmers will use the
same class name.By qualifying NameSpace with classname this collision is able to
be removed.
√ NameSpace Logically group types, example System.Web.UI logically groups
our UI related features.
What are the different types of Assembly?
There are two types of assembly Private and Public assembly. A private assembly is normally used by a single application, and is stored in the application's directory,
or a sub-directory beneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful,
e.g. Crystal report classes which will be used by all application for Reports.
or a sub-directory beneath. A shared assembly is normally stored in the global assembly cache, which is a repository of assemblies maintained by the .NET runtime. Shared assemblies are usually libraries of code which many applications will find useful,
e.g. Crystal report classes which will be used by all application for Reports.
Subscribe to:
Posts (Atom)