Monday, 21 May 2012

Tips for improving the performance of Asp.Net web application


Tips for improving the performance of Asp.Net web application
Many a times we face performance problems in our Asp.Net web application. Sometimes the page hangs or the server takes too much time in processing a page or when many users hit the application the application hangs or some pages take more time than others even though they are roughly same in size etc. Not only these but many more issues are there which one encounter with Asp.Net application in regard to performance.

There are many reasons for it but if we keep some key points in mind while designing and developing the web application we can easily over-come these performance issues. For this I have made out a list of 20 tips which if one keeps in mind and implement it there would be no performance issue for the Asp.Net web application. The list of 20 tips is as follows:

1) Turn off Tracing unless until required

Tracing is one of the wonderful features which enable us to track the application's trace and the sequences. However, again it is useful only for developers and you can set this to "false" unless you require to monitor the trace logging. Enabling tracing adds performance overhead and might expose private information, so it should be enabled only while an application is being actively analyzed.

When not needed, tracing can be turned off using:
<trace enabled="false" localonly="”true”"
pageoutput="”false”" requestlimit="”10”"
tracemode="”SortByTime”">

2) Check if client is connected before performing any large operation

Take advantage of HttpResponse.IsClientConnected before performing a large operation:

if (Response.IsClientConnected)
{
// If still connected, redirect to another page.
Response.Redirect("Page2CS.aspx", false);
}


3) Disable View State of a Page if possible

View state is a fancy name for ASP.NET storing some state data in a hidden input field inside the generated page. When the page is posted back to the server, the server can parse, validate, and apply this view state data back to the page's tree of controls.
View state is a very powerful capability since it allows state to be persisted with the client and it requires no cookies or server memory to save this state. Many ASP.NET server controls use view state to persist settings made during interactions with elements on the page, for example, saving the current page that is being displayed when paging through data.

There are a number of drawbacks to the use of view state, however. It increases the total payload of the page both when served and when requested. There is also an additional overhead incurred when serializing or deserializing view state data that is posted back to the server.
View state increases the memory allocations on the server. Several server controls, the most well known of which is the DataGrid, tend to make excessive use of view state, even in cases where it is not needed.

Pages that do not have any server postback events can have the view state turned off.
The default behavior of the ViewState property is enabled, but if you don't need it, you can turn it off at the control or page level. Within a control, simply set the EnableViewState property to false, or set it globally within the page using this setting:

<%@ Page EnableViewState="false" %>

If you turn view state off for a page or control, make sure you thoroughly test your pages to verify that they continue to function correctly.


4) Turn off Session State, if not required

One extremely powerful feature of ASP.NET is its ability to store session state for users, such as a shopping cart on an e-commerce site or a browser history.

Since ASP.NET Manages session state by default, you pay the cost in memory even if you don't use it. I.e. whether you store your data in in-process or on state server or in a Sql Database, session state requires memory and it's also time consuming when you store or retrieve data from it.

You may not require session state when your pages are static or when you do not need to store information captured in the page. In such cases where you need not use session state, disable it on your web form using the directive

<@%Page EnableSessionState="false"%>

In case you use the session state only to retrieve data from it and not to update it, make the session state read only by using the directive,

<@%Page EnableSessionState ="ReadOnly"%>


5) Set debug=false in web.config

When you create the application, by default this attribute is set to "true" which is very useful while developing. However, when you are deploying your application, always set it to "false".

Setting it to "true" requires the pdb information to be inserted into the file and this results in a comparatively larger file and hence processing will be slow. Therefore, always set debug="false" before deployment.


6) Use the String builder to concatenate string

String is Evil when you want to append and concatenate text to your string. All the activities you do to the string are stored in the memory as separate references and it must be avoided as much as possible, i.e. When a string is modified, the run time will create a new string and return it, leaving the original to be garbage collected. Most of the time this is a fast and simple way to do it, but when a string is being modified repeatedly it begins to be a burden on performance: all of those allocations eventually get expensive.

Use String Builder when ever string concatenation is needed so that it only stores the value in the original string and no additional reference is created.


7) Use gzip compression on IIS

While not necessarily a server performance tip (since you might see CPU utilization go up), using gzip compression can decrease the number of bytes sent by your server. This gives the perception of faster pages and also cuts down on bandwidth usage. Depending on the data sent, how well it can be compressed, and whether the client browsers support it (IIS will only send gzip compressed content to clients that support gzip compression, such as Internet Explorer 6.0 and Firefox), your server can serve more requests per second. In fact, just about any time you can decrease the amount of data returned, you will increase requests per second.
The good news is that gzip compression is built into IIS 6.0 and is much better than the gzip compression used in IIS 5.0. Unfortunately, when attempting to turn on gzip compression in IIS 6.0, you may not be able to locate the setting on the properties dialog in IIS. The IIS team built awesome gzip capabilities into the server, but neglected to include an administrative UI for enabling it. To enable gzip compression, you have to spelunk into the innards of the XML configuration settings of IIS 6.0.


8) Page Output Caching and Proxy Servers

ASP.NET is your presentation layer (or should be); it consists of pages, user controls, server controls (HttpHandlers and HttpModules), and the content that they generate. If you have an ASP.NET page that generates output, whether HTML, XML, images, or any other data, and you run this code on each request and it generates the same output, you have a great candidate for page output caching.
By simply adding this line to the top of your page:

<%@ Page OutputCache VaryByParams="none" Duration="60" %>

You can effectively generate the output for this page once and reuse it multiple times for up to 60 seconds, at which point the page will re-execute and the output will once be again added to the ASP.NET Cache. This behavior can also be accomplished using some lower-level programmatic APIs, too. There are several configurable settings for output caching, such as the VaryByParams attribute just described. VaryByParams just happens to be required, but allows you to specify the HTTP GET or HTTP POST parameters to vary the cache entries. For example, default.aspx?Report=1 or default.aspx?Report=2 could be output-cached by simply setting VaryByParam="Report". Additional parameters can be named by specifying a semicolon-separated list.

Many people don't realize that when the Output Cache is used, the ASP.NET page also generates a set of HTTP headers that downstream caching servers, such as those used by the Microsoft Internet Security and Acceleration Server or by Akamai. When HTTP Cache headers are set, the documents can be cached on these network resources, and client requests can be satisfied without having to go back to the origin server.

Using page output caching, then, does not make your application more efficient, but it can potentially reduce the load on your server as downstream caching technology caches documents. Of course, this can only be anonymous content; once it's downstream, you won't see the requests anymore and can't perform authentication to prevent access to it.


9) Per - Request Caching

Whereas the Cache API is designed to cache data for a long period or until some condition is met, per-request caching simply means caching the data for the duration of the request. A particular code path is accessed frequently on each request but the data only needs to be fetched, applied, modified, or updated once. This sounds fairly theoretical, so let's consider a concrete example.

In the Forums application of Community Server, each server control used on a page requires personalization data to determine which skin to use, the style sheet to use, as well as other personalization data. Some of this data can be cached for a long period of time, but some data, such as the skin to use for the controls, is fetched once on each request and reused multiple times during the execution of the request.

To accomplish per-request caching, use the ASP.NET HttpContext. An instance of HttpContext is created with every request and is accessible anywhere during that request from the HttpContext.Current property. The HttpContext class has a special Items collection property; objects and data added to this Items collection are cached only for the duration of the request. Just as you can use the Cache to store frequently accessed data, you can use HttpContext.Items to store data that you'll use only on a per-request basis. The logic behind this is simple: data is added to the HttpContext.Items collection when it doesn't exist, and on subsequent lookups the data found in HttpContext.Items is simply returned.


10) Connection Pooling

Setting up the TCP connection between your Web application and SQL Server™ can be an expensive operation. Developers at Microsoft have been able to take advantage of connection pooling for some time now, allowing them to reuse connections to the database. Rather than setting up a new TCP connection on each request, a new connection is set up only when one is not available in the connection pool. When the connection is closed, it is returned to the pool where it remains connected to the database, as opposed to completely tearing down that TCP connection.

Of course you need to watch out for leaking connections. Always close your connections when you're finished with them. I repeat: no matter what anyone says about garbage collection within the Microsoft® .NET Framework, always call Close or Dispose explicitly on your connection when you are finished with it. Do not trust the common language runtime (CLR) to clean up and close your connection for you at a predetermined time. The CLR will eventually destroy the class and force the connection closed, but you have no guarantee when the garbage collection on the object will actually happen.

To use connection pooling optimally, there are a couple of rules to live by. First, open the connection, do the work, and then close the connection. It's okay to open and close the connection multiple times on each request if you have to (optimally you apply Tip 1) rather than keeping the connection open and passing it around through different methods. Second, use the same connection string (and the same thread identity if you're using integrated authentication). If you don't use the same connection string, for example customizing the connection string based on the logged-in user, you won't get the same optimization value provided by connection pooling. And if you use integrated authentication while impersonating a large set of users, your pooling will also be much less effective. The .NET CLR data performance counters can be very useful when attempting to track down any performance issues that are related to connection pooling.

Whenever your application is connecting to a resource, such as a database, running in another process, you should optimize by focusing on the time spent connecting to the resource, the time spent sending or retrieving data, and the number of round-trips. Optimizing any kind of process hop in your application is the first place to start to achieve better performance. The application tier contains the logic that connects to your data layer and transforms data into meaningful class instances and business processes. For example, in Community Server, this is where you populate a Forums or Threads collection, and apply business rules such as permissions; most importantly it is where the Caching logic is performed.


11) Return Multiple ResultSets

Review your database code to see if you have request paths that go to the database more than once. Each of those round-trips decreases the number of requests per second your application can serve. By returning multiple resultsets in a single database request, you can cut the total time spent communicating with the database. You'll be making your system more scalable, too, as you'll cut down on the work the database server is doing managing requests.

While you can return multiple resultsets using dynamic SQL, I prefer to use stored procedures. It's arguable whether business logic should reside in a stored procedure, but I think that if logic in a stored procedure can constrain the data returned (reduce the size of the dataset, time spent on the network, and not having to filter the data in the logic tier), it's a good thing. Using a SqlCommand instance and its ExecuteReader method to populate strongly typed business classes, you can move the resultset pointer forward by calling NextResult.


12) Avoid throwing exceptions

Exceptions are probably one of the heaviest resource hogs and causes of slowdowns you will ever see in web applications, as well as windows applications. You can use as many try/catch blocks as you want. Using exceptions gratuitously is where you lose performance. For example, you should stay away from things like using exceptions for control flow.


13) Use Finally Method to kill resources
The finally method gets executed independent of the outcome of the Block. Always use the finally block to kill resources like closing database connection, closing files and other resources such that they get executed independent of whether the code worked in Try or went to Catch.


14) Avoid unnecessary round trips to the server - Where-ever feasible use AJAX

Round trips significantly affect performance. They are subject to network latency and to downstream server latency. Many data-driven Web sites heavily access the database for every user request. While connection pooling helps, the increased network traffic and processing load on the database server can adversely affect performance.

Keep round trips to an absolute minimum. Implement Ajax UI whenever possible. The idea is to avoid full page refresh and only update the portion of the page that needs to be changed


15) Use Page.ISPostBack

Make sure you don't execute code needlessly. Use Page.ISPostBack property to ensure that you only perform page initialization logic when a page is first time loaded and not in response to client postbacks.


16) Always check Page.IsValid when using Validator Controls

So you’ve dropped on some validator controls, and you think your good to go because ASP.net does everything for you! Right? Wrong! All that happens if bad data is received is the IsValid flag is set to false. So make sure you check Page.IsValid before processing your forms.


17) Use low cost authentication

Authentication can also have an impact over the performance of your application. For example passport authentication is slower than form-base authentication which in here turn is slower than Windows authentication.


18) Make JavaScript and CSS External

Using external files generally produces faster pages because the JavaScript and CSS files are cached by the browser. Inline JavaScript and CSS increases the HTML document size but reduces the number of HTTP requests. With cached external files, the size of the HTML is kept small without increasing the number of HTTP requests thus improving the performance.


19) Avoid Server-Side Validation

Try to avoid server-side validation, use client-side instead. Server-Side will just consume valuable resources on your servers, and cause more chat back and forth. Only implement server-side validation on important fields.


20) Minimize the number of web server controls

The use of web server controls increases the response time of your application because they need time to be processed on the server side before they are rendered on the client side. One way to minimize the number of web server controls is to taking into consideration, the usage of HTML elements where they are suited, for example if you want to display static text.


The list is not exhaustive, it just discusses the 20 top performance tips. If you have any other tip do share it.

Sunday, 20 May 2012

Caching in Asp.Net and its Types


Define Caching in ASP.NET. 
Caching technique allows to store/cache page output or application data on the client. The cached information is used to serve subsequent requests that avoid the overhead of recreating the same information. This enhances performance when same information is requested many times by the user. 

Advantages of Caching 

It increases performance of the application by serving user with cached output.
It decreases server round trips for fetching data from database by persisting data in the memory.
It greatly reduces overhead from server resources. 

What are the types of Caching in ASP.NET? 

Caching in ASP.NET can be of the following types
Page Output Caching
Page Fragment Caching
Data Caching 


What are the types of Caching in ASP.NET? 

Caching in ASP.NET can be of the following types
Page Output Caching
Page Fragment Caching
Data Caching 

Explain in brief each kind of caching in ASP.NET. 

Page Output Caching
This type of caching is implemented by placing OutputCache directive at the top of the .aspx page at design time.
For example:
<%@OutputCache Duration= "30" VaryByParam= "DepartmentId"%>

The duration parameter specifies for how long the page would be in cache and the VaryByParam parameter is used to cache different version of the page.
The VaryByParam parameter is useful when we require caching a page based on certain criteria.



Data Caching
Data Caching is implemented by using Cache object to store and quick retrieval of application data.
Cache object is just like application object which can be access anywhere in the application.
The lifetime of the cache is equivalent to the lifetime of the application. .
Let Us Assume we can create One Xm File and this Could be acted as database
Right Click on Solution ExploreràAddNewItemàXMLFile and Save it as Sample.xml
Write the following lines of Code under it
<?xml version="1.0" encoding="utf-8" ?>
<UserInfo>
  <User>
    <Name>Mohan</Name>
    <Location>Chennai</Location>
    <Age>25</Age>
  </User>
  <User>
    <Name>Venkat</Name>
    <Location>Hyderabad</Location>
    <Age>26</Age>
  </User>
</UserInfo>
The Following steps  we need to follow for displaying the xml file data into GridView
Step 1:
      Place One Label ,button and Grid view on to the design page
Step 2:
  Write the following Code under Button_Click
  if (Cache["data"] == null)
        {
            ds = new DataSet();
            ds.ReadXml(MapPath("Sample.xml"));
            Cache.Insert("data",ds );
            Label3.Text = "From Data Source";

        }
        else
        {
            ds = (DataSet)Cache["data"];
            Label3.Text = "From Cache";
        }
        GridView1.DataSource = ds;
        GridView1.DataBind();
Step 3:
Now you can execute the application, for the first time we are sending the request from our client end and the data should be displayed into Gridview and also we can see the message in label  i.e,from Data Source
For second time you are sending the same request, this time you can get it from Cache Memory
This is the way we can improve the performance of our application
Page Fragment Caching
This technique is used to store part of a Web form response in memory by caching a user control.
   Tips :Output caching can be implemented using  <%@OutputCache Duration =”60” varybyparam=”none”%> where as Data Caching can be implemented by using Concept
Fragment Caching means only particular portion of page is cached and This concept can be Implemented by using UserControl
What is Fragment Caching in ASP.NET? 
Fragment caching does not cache a WebForm, rather it allows for caching of individual user controls within a Web Form, where each control can have different cache duration and behavior.
E.g.: If you have a User Control, then add the following line in Aspx page
<%@ OutputCache Duration="20" VaryByParam="none"%>
Similarly you could have another usercontrol with duration set to 10 and have both these controls on a single Web Form 

Tuesday, 15 May 2012

Need of Delegates in C#.Net


Delegate

A delegate in C# is similar to a function pointer in C or C++. Using a delegate allows the programmer to encapsulate a reference to a method inside a delegate object. The delegate object can then be passed to code which can call the referenced method, without having to know at compile time which method will be invoked.

Delegate Signatures
A delegate type is specified by a distinct signature, which includes a unique identifier, parameter list, and return type. The following code shows how to declare a delegate:
public delegate double UnitConversion(double from);
The example above contains a full definition of a delegate. Just like any other namespace element, delegates are declared as either public or internal, with a default of internal accessibility if the modifier is not specified. Delegates may also be declared as nested types, with accessibility modifiers allowable for their containing class or struct.
The C# keyword delegate identifies this as a delegate declaration. The return type of this delegate is double, its identifier is "UnitConversion," and it has a single parameter of type double. Looking at both the delegate identifier and parameter identifier, you can get an idea of the purpose of this delegate, which is to serve as a definition of methods that perform a conversion of units from one type to another.
A delegate declaration is appended with a semi-colon. Delegates do not have implementation. Instead, they are a skeleton defining the proper signature of delegate handler methods to which they may refer. The handler methods contain implementation that is executed when its referring delegate is invoked.
Example
using System;
namespace Delegates
{
    public delegate int DelegateToMethod(int x, int y);

    public class Math
    {
        public static int Add(int first, int second)
        {
            return first + second;
        }
        public static int Multiply(int first, int second)
        {
            return first * second;
        }

        public static int Divide(int first, int second)
        {
            return first / second;
        }
    }
    public class DelegateApp
    {
        public static void Main()
        {
            DelegateToMethod aDelegate = new DelegateToMethod(Math.Add);
            DelegateToMethod mDelegate = new DelegateToMethod(Math.Multiply);
            DelegateToMethod dDelegate = new DelegateToMethod(Math.Divide);
            Console.WriteLine("Calling the method Math.Add() through the aDelegate object");
            Console.WriteLine(aDelegate(5, 5));
            Console.WriteLine("Calling the method Math.Multiply() through the mDelegate object");
            Console.WriteLine(mDelegate(5, 5));
            Console.WriteLine("Calling the method Math.Divide() through the dDelegate object");
            Console.WriteLine(dDelegate(5, 5));
            Console.ReadLine();
        }
}
}
}


OUTPUT
Calling the method Math.Add() through the aDelegate object
10
Calling the method Math.Multiply() through the mDelegate object
25
Calling the method Math.Divide() through the dDelegate object
1


Inverview Questions for Csharp.Net


1. 
Which of the following statements are TRUE about the .NET CLR?
  1. It provides a language-neutral development & execution environment.
  2. It ensures that an application would not be able to access memory that it is not authorized to access.
  3. It provides services to run "managed" applications.
  4. The resources are garbage collected.
  5. It provides services to run "unmanaged" applications.
Only 1 and 2
Only 1, 2 and 4
1, 2, 3, 4
Only 4 and 5
Only 3 and 4


2. 
Which of the following are valid .NET CLR JIT performance counters?
  1. Total memory used for JIT compilation
  2. Average memory used for JIT compilation
  3. Number of methods that failed to compile with the standard JIT
  4. Percentage of processor time spent performing JIT compilation
  5. Percentage of memory currently dedicated for JIT compilation
1, 5
3, 4
1, 2
4, 5

3. 
Which of the following statements is correct about Managed Code?
Managed code is the code that is compiled by the JIT compilers.
Managed code is the code where resources are Garbage Collected.
Managed code is the code that runs on top of Windows.
Managed code is the code that is written to target the services of the CLR.
Managed code is the code that can run on top of Linux.

4. 
Which of the following utilities can be used to compile managed assemblies into processor-specific native code?
gacutil
ngen
sn
dumpbin
ildasm



5. 
Which of the following are NOT true about .NET Framework?
  1. It provides a consistent object-oriented programming environment whether object code is stored and executed locally, executed locally but Internet-distributed, or executed remotely.
  2. It provides a code-execution environment that minimizes software deployment and versioning conflicts.
  3. It provides a code-execution environment that promotes safe execution of code, including code created by an unknown or semi-trusted third party.
  4. It provides different programming models for Windows-based applications and Web-based applications.
  5. It provides an event driven programming model for building Windows Device Drivers.
1, 2
2, 4
4, 5
1, 2, 4
6. 
Which of the following components of the .NET framework provide an extensible set of classes that can be used by any .NET compliant programming language?
.NET class libraries
Common Language Runtime
Common Language Infrastructure
Component Object Model
Common Type System

7. 
Which of the following jobs are NOT performed by Garbage Collector?
  1. Freeing memory on the stack.
  2. Avoiding memory leaks.
  3. Freeing memory occupied by unreferenced objects.
  4. Closing unclosed database collections.
  5. Closing unclosed files.
1, 2, 3
3, 5
1, 4, 5
3, 4

8. 
Which of the following .NET components can be used to remove unused references from the managed heap?
Common Language Infrastructure
CLR
Garbage Collector
Class Loader
CTS

9. 
Which of the following statements correctly define .NET Framework?
It is an environment for developing, building, deploying and executing Desktop Applications, Web Applications and Web Services.
It is an environment for developing, building, deploying and executing only Web Applications.
It is an environment for developing, building, deploying and executing Distributed Applications.
It is an environment for developing, building, deploying and executing Web Services.
It is an environment for development and execution of Windows applications.

10. 
Which of the following constitutes the .NET Framework?
  1. ASP.NET Applications
  2. CLR
  3. Framework Class Library
  4. WinForm Applications
  5. Windows Services
1, 2
2, 3
3, 4
2, 5
11. 
Which of the following assemblies can be stored in Global Assembly Cache?
Private Assemblies
Friend Assemblies
Shared Assemblies
Public Assemblies
Protected Assemblies
12. 
Code that targets the Common Language Runtime is known as
Unmanaged
Distributed
Legacy
Managed Code
Native Code

13. 
Which of the following statements is correct about the .NET Framework?
.NET Framework uses DCOM for achieving language interoperability.
.NET Framework is built on the DCOM technology.
.NET Framework uses DCOM for making transition between managed and unmanaged code.
.NET Framework uses DCOM for creating unmanaged applications.
.NET Framework uses COM+ services while creating Distributed Applications.

14. 
Which of the following is the root of the .NET type hierarchy?
System.Object
System.Type
System.Base
System.Parent
System.Root

15. 
Which of the following benefits do we get on running managed code under CLR?
  1. Type safety of the code running under CLR is assured.
  2. It is ensured that an application would not access the memory that it is not authorized to access.
  3. It launches separate process for every application running under it.
  4. The resources are Garbage collected.
Only 1 and 2
Only 2, 3 and 4
Only 1, 2 and 4
Only 4
All of the above

16. 
Which of the following security features can .NET applications avail?
  1. PIN Security
  2. Code Access Security
  3. Role Based Security
  4. Authentication Security
  5. Biorhythm Security
1, 4, 5
2, 5
2, 3
3, 4
17. 
Which of the following jobs are done by Common Language Runtime?
  1. It provides core services such as memory management, thread management, and remoting.
  2. It enforces strict type safety.
  3. It provides Code Access Security.
  4. It provides Garbage Collection Services.
Only 1 and 2
Only 3, 4
Only 1, 3 and 4
Only 2, 3 and 4
All of the above

18. 
Which of the following statements are correct about a .NET Assembly?
  1. It is the smallest deployable unit.
  2. Each assembly has only one entry point - Main(), WinMain() or DLLMain().
  3. An assembly can be a Shared assembly or a Private assembly.
  4. An assembly can contain only code and data.
  5. An assembly is always in the form of an EXE file.
1, 2, 3
2, 4, 5
1, 3, 5
1, 2

19. 
Which of the following statements are correct about JIT?
  1. JIT compiler compiles instructions into machine code at run time.
  2. The code compiler by the JIT compiler runs under CLR.
  3. The instructions compiled by JIT compilers are written in native code.
  4. The instructions compiled by JIT compilers are written in Intermediate Language (IL) code.
  5. The method is JIT compiled even if it is not called
1, 2, 3
2, 4
3, 4, 5
1, 2

20. 
Which of the following are parts of the .NET Framework?
  1. The Common Language Runtime (CLR)
  2. The Framework Class Libraries (FCL)
  3. Microsoft Published Web Services
  4. Applications deployed on IIS
  5. Mobile Applications
Only 1, 2, 3
Only 1, 2
Only 1, 2, 4
Only 4, 5
All of the above