10 Proven Ways to Optimize Your ASP.NET Application Performance

May 18, 2017 11:24:28 PM


.NET has made building dynamic websites a breeze. A mastery over the controls and understanding of the environment of this open source framework helps developers build scalable applications using fewer codes. ASP.NET is exclusively server-side and is language-independent by allowing the developers to choose from one out of many languages that target ASP.NET framework. So now you have deployed your application with ASP.NET and checked all the boxes before doing so.

But you know it is not always perfect, and that odd slow query and a few other glitches pop up once in a while. With users expecting quick and reliable services at all times coupled with constantly increasing scale, is your performance consistent? Here are 10 ways to optimize your ASP.NET application performance:

  1. Identify Bottlenecks

Identifying problem areas in your ASP.NET application is the first step to optimizing it. Performance issues could stem up from architecture or design issues. Some of the common issues that can affect your ASP.NET application are improperly rendered HTML, complex algorithms and thread pool issues due to long running operations.

  1. Essential ASP.NET Tools

There is a range of development tools that can make managing your ASP.NET website or application a lot easier. Some of the popular ones include .NET Code Profilers, ideal for finding hot paths in code and Windows Performance Counters, which provide metrics related to CPU usage, request queuing, garbage collection, HTTP error rates, and memory usage. Windows Performance Counters are pre-installed on every Windows Server.

To get more accurate representations of performance, up your customization game. Create and track your own custom metrics that focus on the areas that are relevant to your application. Key Performance Indicators will towards the goal of optimization further. Tracking can be done with multiple third party tools or by using stats. You can also use IIS Access Logs to find out top used requests, slowest requests and others that will help identify areas of optimization.

  1. Remove Unused Modules

Requests get processed after they have passed through the various HTTP modules of the ASP.NET pipeline and finally reach the handler which serves the request. Run a code to check your list of active modules and those which are not being used. Shorten this pipeline journey of the request by removing modules that are unused, thereby adding some efficiency and speed to your application. For removing a module, you need to update web.config

  1. Disable View State

Another tried and tested technique to optimize ASP.NET application performance is by disabling View State. ASP.NET uses the View State method as a state management technique to keep page and control values during round trips.  There are certain situations where View State is not necessary, so you can disable it to free up your application more. View State is not required when the control is input only and changes only from user actions. You also do not need View State if the control is populated again after every postback. View State can be disabled at control, page, application and machine level, with different coding solutions for each level.

  1. Utilize Caching

Optimize ASP.NET applications by prudently utilizing caching. The output cache is useful in helping quick page rendering by storing a copy of the ASP.NET page in memory. Of course, for dynamic pages, out cache needs to be given more features to enable customization, ensure that your users do not suffer from a badly utilized output cache.  Work with are the VaryBy parameters and SqlDependency to ensure that your output cache increases speed without affecting performance. You can also explore methods to configure the output cache in runtime, such as SetExpires and SetCacheability.

  1. Process configuration optimization and Parallel Programming

ASP.NET provides the flexibility to configure the process model by defining various process level properties like number of threads in execution, timeout for threads, number of requests to keep waiting for IO work. Developer can tweak these settings defined in Machine.config file. As nowadays hardware is pretty cheap and multi core servers are available, developers can fetch maximum benefits by tweaking the process model configuration. Even the use of parallel programing in development gives upper hand to developers for designing superfast web apps.

  1. NET web optimization framework

The ASP.NET Web Optimization Framework was introduced by Microsoft for optimizing the ASP.NET web application performance. In order to optimize web application this works on 2 principle. First by reducing the number of round trips and secondly by reducing the size of requested resources. This framework provides services

  • Bundling - This feature was introduced in ASP.NET 4.5 and reduces the number of requests to server. It means combine multiple resources like script, CSS files, etc. to single resources so that there will be fewer requests by browser. It improves page load performance
  • Minification – This feature minified the size of requested resources by trimming and shortening the variable names, whitespaces etc
  1. Choosing correct pipeline mode hosting on IIS

At application pool level, there are two pipeline modes available: classic and integrated. Classic mode was there to support applications migrated from older version of IIS. IIS provides many features which are implemented as modules and in similar way many feature are implemented as HTTP Module which are part of ASP.NET Pipeline. In case of classic mode, each request goes through first IIS pipeline and then ASP.NET pipeline before getting served whereas in case of Integrated mode both these pipelines are merged in one and all the modules of IIS and ASP.NET are invoked in one event which significantly reduces the redundancy and thereby improving the application performance.

  1. Http Compression

One of the very promising method to optimize the performance is Http Compression. Http compression works by compressing the content mostly in GZip format before transmitting the data on wire which provides great improvement by reducing the transmission time. IIS supports 2 types of compression: Static and Dynamic.

  • Static compression works by compressing the cache static content. Initial request is compressed and all the followed requests use this compressed version. One should only compress the static content which does not change, not the dynamic one.
  • Dynamic Compression unlike the static content, works on dynamic content which often changes. It supports compression without adding into the cache, it only compresses the content.
  1. Load balancers

Applications that are meant to get regular hits are benefitted by load balancer. Load balancer will act as one point of receiving all the client requests and by use of various algorithms of load balancing it forwards the request to a server which will less loaded and most time effective based on state of server. In case of a web garden scenario where multiple servers are there behind the load balancer, even if one of the server stops or crashes, the application with continue working since other servers are still working. Only point at which application goes down is when load balancer goes off. ASP.NET provides full support to load balancer implementation and allows multiple ways in term of state management like outproc or state server is used to maintain the state of process in web farm.

ASP.NET applications offer a universe of possibilities for developers. At the same time, there are many areas where they can be optimized. Optimization is not a one-shot process but an iterative relationship between developer and application. Understanding problems is the key, as uncertain steps can create unwanted ripples in different areas of the application. Be ready to repeat processes and dedicate time to analyze as you get closer and closer to the goal of an optimized .NET application.

Sachin Mehndiratta

Written by Sachin Mehndiratta

Sachin is a Technical Lead at Daffodil Software. He is a firm believer that technology plays a big role in interconnecting the world. Being a part of this hi-tech landscape, he sees himself contributing back to society with his astute technical skills and mind blowing applications he works on.