ASP.NET Application Performance Tuning and Scalability Improvement (Part – 1)

Every developers and customer wants his/her application performed well. But it is very tricky to improve performance of a web application. It’s actually depends on different parameter like HTML client, HTTP network, Web server, middle-tier components, database components, resource-management components, TCP/IP networks, and database servers. Sometimes performance of the application increases drastically, if you change a single parameter. Sometimes you have to change multiple parameters. Here are the some performance optimization tips for ASP.NET web applications which may help you to increase performance of ASP.NET Application.

Prepare release with release mode
When prepare release for deployment, select release mode instead of debug mode.

How affect in performance?

  • If you choose debug mode instead of release mode, you are creating pdb (program database – uses for debugging) files which creates extra overhead. For this reason you may face timeout problem.

Best Practice

  • Always prepare release in release mode.

Release mode

In Web.Config, Set debug=”false”
Set debug=”false” in web.config as follows after deploying your application. By default debug=”true” when you create a web application. It is necessary to set debug=”true” in development environment.

  
    
  

How affect in performance?

  • If you set debug = “true”, application requires the pdb information to be inserted into the file and that results in a comparatively larger file and hence processing will be slow.

Best Practice

  • In deployment server, set debug = “false” in web.config

Turn off Tracing unless until required
Sometimes developers need to trace the application to monitor the executions of application or a pages. It requires for application diagnostic purposes.

How affect in performance?

  • When trace is enabled it loaded extra information to the pages which degrades performances.

Best Practice

  • Always set trace enabled = “false” unless or until you required to monitor a page’s executions. Set trace enable = “false” as follows in web.config.

    

Carefully manage session state
Session state is a very useful feature of asp.net. Though, ASP.NET manages session state by default, we must pay attention of session memory management.

  • How affect in performance?
  • When you store your data in in-process or on a state server or in a SQL Database, session state, it requires memory.
  • It is also time consuming when you store or retrieve data in-process or state server or SQL server.

Best Practice

  • If your page is static, it is recommended not to use session state. In such cases where you don’t need to use session state, disable it on your web form using the following directive:
    <@%Page EnableSessionState="false"%>
  • In case you use the session state only to retrieve data and not to update, make the session state read-only using the following directive.
    <@%Page EnableSessionState ="ReadOnly"%>
  • If your application session state is out of process then consider carefully whether there is a need of the state server or SQL Server mode.
  • SQL Server session mode provides lower performance than state server mode.
  • Try to avoid keeping object in session. Since it requires serializing then de-serializing which affected in performance.
  • Use client-side state management than server side.

Disable View State of a page if not required

  • It stores data in the generated HTML using hidden field not on the server.
  • View State provides page level state management
  • As long as the user is on the current page, state is available and the user redirects to the next page and the current page state is lost
  • View State can store any type of data because it is object type but it is preferable not to store a complex type of data due to the need for serialization and deserialization on each post back

How affect in performance?

  • It increases the total payload of a page when submitted and when serving request.
  • Serialization and deserialization of the data is required when submitting data and gets requested data.
  • View state increases the memory allocations on the server.

Best Practice

  • Pages that do not have any server postback events can have the view state turned off.
  • The default behaviour of the View State 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" %>

Use finally block to release resources
We always uses try, catch and finally block for exception handling. Finally block executes whether any exception occurs or not.
How affect in performance?

  • Sometimes application occupy resources where as it doesn’t need it. It is occur due to bad programming.

Best Practice

  • Always use a finally block to release resources like closing database connections, closing files, disposing objects and other resources.

Avoid unnecessary round trips to the server

How affect in performance?

  • Unnecessary round trips significantly effect on web application performance.
  • It increases network latency and 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.

Best Practice

  • Keep round trips as minimum as possible
  • Use Ajax or partial page load instead of full page reload or refresh.

Choose low cost authentication
Authentication is a main factor for a secured applications. You must take decision which authentication will you use? Keep in mind, passport authentication is slower than form-base authentication which is slower than Windows authentication.

Use paging in grid view
In asp.net web application to show data in tabular format, generally we use grid view. Besides this we also uses DataGrid, JQgrid, Telerik grid, Kendo Grid etc. For huge number of data we cannot think general way because it takes huge time to load.

Best Practice

  • To load grid view faster take advantages of paging, it shows small subsets of data at a time. JQGrid is faster than asp.net grid view because it does everything in client side.

Minimizes number of web server control

How affect in performance?

  • The uses of web server controls increase the response time.
  • Web server controls go to the server executes all of its life cycle and then rendered on the client side.

Best Practice

  • Don’t use server control unless until required. Use HTML elements where suited.

bobj is undefined

Problem Statement
I have an application developed with the following tools and technologies.

  • MS .NET Framework 4.5
  • SAP Crystal Report 13.5
  • ASP.NET Web form
  • SQL Server 2008 R2

In deployment server the report is not displayed. Instead I found a javascript error “bobj is undefined”. But it works fine in development server.

Solutions

  • Copy aspnet_client folder (From a running project) from c:\inetpub\wwwroot folder to the new website root folder. For example: c:\inetpub\wwwroot
  • Create a virtual directory called aspnet_client that points to c:\inetpub\wwwroot\aspnet_client inside the new website
  • If this is a 32 bit application running on a 64 bit OS, make sure the application pool is set to 32 bit
  • Set application pool to run under Local System Account
  • Change application pool to Classic mode.

How to add ASP.NET MVC to a Web form application

ASP.NET MVC is very popular now a day. But we have much legacy system of ASP.NET web form. If you want to use ASP.NET MVC with legacy ASP.NET web form application. What should you do? This is not encouraged to create an application with ASP.NET web form and ASP.NET MVC together. If you need, you can use. So, let’s start. In this application I used Visual Studio 2012 and .NET framework 4.5

Step 1: Create a ASP.NET Web Forms Application

Open visual studio, Got to File->New->Project
Select Template -> Visual C# -> Web -> ASP.NET Web Forms application, set location and click OK

Step 2: Add Reference
Click right button on the project -> Add Reference ->Extensions -> System.web.mvc
And Add Reference -> Framework -> System.Web.Routing

addref

Step 3: Configure Default Routing
Modify the Application_Start method of Global.asax file. Write down the following code snippet in the Application_Start method .

System.Web.Routing.RouteTable.Routes.MapRoute("Default", "{controller}/{action}/{id}",
                new { Controller = "Home", action = "index", id = "" });

Here Route name is Default and default routing is {controller}/{action}/{id} . Here, default controller is Home and default action is index and default id is empty.

Step 4: Create Controller directory and Controller

Create a controller directory by right click on solution ->Add -> New folder -> Controllers
Create a controller by right click on Controller folder -> Add -> Class -> MyController (name of the class)
And create an action in MyController – name AboutMe like below.

MyController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebFormMVC.Controllers
{
    public class MyController : Controller
    {
        public JsonResult AboutMe()
        {
            return Json(new { name = "Mahedee Hasan", designation = "Software Architect"}, JsonRequestBehavior.AllowGet);
        }
    }
}

Now run the application, if you set default page of your application is default.aspx. You will see the output of as usual asp.net webform application. Now change the URL like : http://localhost:53598/My/Aboutme you will see a json output like below. This is actually MVC output.

webmvcoutput

*If you don’t clear in any portion or any suggestion, please leave your comment here. To run the application download required packages.