Fix your application and increase performance
You can fix your application and increase performance with minimal amount of work by address 3 common issues could make a big difference in the performance of your ASP.NET application.Handled exceptions & Response. Redirect
Modify your code to avoid try/catch instead of figuring out why it is throwing an exception in the first place. This can be a bad idea for many reasons, one of them being excessive CPU usage due to exception handling.Detect it
To determine whether your application is suffering from too many exceptions being thrown, monitor the “.NET CLR Exceptions# of Exceps Thrown / sec” performance counter. If the value is high (50+) on a consistent basis, you may have a problem.Fix it
Here are some common areas that can cause excessive exceptions:- ASP.NET MVC rethrows exceptions from controller’s actions multiple times before handling them with the [HandleError] attribute or returning an error. It also does this during view compilation and view resolution when using partial view names like:
return View("MyView");
To fix it:
Don’t use HandleError for control purposes, only for legitimate error reporting. Use fully qualified view paths in View():return View("~/Views/MyView.cshtml");
If you have frequently hit URLs that report errors, restructure your app to write HTTP errors via HttpResponse.StatusCode instead of via exceptions.
- HttpWebRequest throws a WebException for 404s, 401s, and other non-200 responses that are correctly handled by your code.
- HttpResponse.Redirect(). This is the worst offender, because it is incredibly common and because it throws a ThreadAbortException. Besides the usual exception overhead, this also causes the thread on which it is thrown to exit, requiring the CLR threadpool to allocate another thread later. This alone can have a huge CPU impact on a busy application.
return Redirect(newUrl);
2. LINQ to SQL & non-compiled queries
For web apps that use LINQ to SQL or Entity Framework as their data backend, compiling per-request LINQ queries often has the highest CPU overhead. Every time a LINQ query is executed, it is compiled into a SQL query by the LINQ to SQL provider. This is very CPU intensive, and gets progressively worse with more complicated LINQ expressions.Detect it
While there is no easy way to monitor for this externally, you can assume you have this problem if your application uses LINQ to SQL or EF, and does not explicitly implement query compilation for per-request queries (ask your developer).Fix it
To fix it, compile your LINQ to SQL queries:// create compiled query public static Func> CustomersByCity = CompiledQuery.Compile( (Northwnd db, string city) => from c in db.Customers where c.City == city select c ); // invoke compiled query var myDb = GetNorthwind(); return Queries.CustomersByCity(myDb, city);
You can also compile your Entity Framework queries. Starting with Entity Framework 4.5, queries can be compiled automatically by the framework.
There has been a lot of discussion of negative performance benefits of compiled queries. The bottom line is if a query is going to be called in the context of a request, compiling it once and reusing it should always be a net win. Especially when you consider the overhead of per-request query compilation on application CPU usage, and not just the execution speed of the query itself.

