views:

776

answers:

3

What is the fastest way to execute a method on an ASP.NET website?

The scenario is pretty simple: I have a method which should be executed when a web page is hit. Nothing else is happening on the page, the only rendered output is a "done" message. I want the processing to be as fast as possible.

Every single hit is unique, so caching is not an option.

My plan is to use an HttpHandler and configure it in web.config (mypage.ashx) rather than a regular .aspx page. This should reduce the overhead significantly.

So my question is really: Is there a faster way to accomplish this than using HttpHandlers?

+12  A: 

Depending on what you're doing, I wouldn't expect to see a lot of improvement over just using an HttpHandler. I'd start by just writing the HttpHandler and seeing how it performs. If you need it to be faster, try looking more closely at the things you're actually doing while processing the request and seeing what can be optimized. For example, if you're doing any logging to a database, try writing to a local database instead of across a network. If it's still not fast enough, then maybe look into writing something lower level. Until that point though, I'd stick with whatever's easiest for you to write.

For reference, I've written an ad server in ASP.NET (using HttpHandlers) that can serve an ad (including targeting and logging the impression to a local database) in 0-15ms under load. I thought I was doing quite a bit of processing - but that's a pretty good response time IMHO.


Update after several months:

If you clear all the HttpModules that are included by default, this will remove a fair amount of overhead. By default, the following HttpModules are included in every site via the machine-level web.config file:

  • OutputCache
  • Session (for session state)
  • WindowsAuthentication
  • FormsAuthentication
  • PassportAuthentication
  • RoleManager
  • UrlAuthorization
  • FileAuthorization
  • AnonymousIdentification
  • Profile
  • ErrorHandler
  • ServiceModel

Like I said above, my ad server doesn't use any of these, so I've just done this in that app's web.config:

<httpModules>
   <clear />
</httpModules>

If you need some of those, but not all, you can remove the ones you don't need:

<httpModules>
   <remove name="PassportAuthentication" />
   <remove name="Session" />
</httpModules>

ASP.NET MVC Note: ASP.NET MVC requires the session state module unless you do something specific to workaround it. See this question for more information: http://stackoverflow.com/questions/884852/how-can-i-disable-session-state-in-asp-net-mvc

Update for IIS7: Unfortunately, things aren't quite as simple in IIS7. Here is how to clear HTTP Modules in IIS7

Daniel Schaffer
You're dead on: I will be performing some logging to a database. And I planned to create tiny text files rather than going to the db. The files would be processed later. But based on your experience and impressive responsetime you achieved, I'll just go ahead with a simple HttpHandler and db access.
Jakob Gade
What we've done is put an SQL Express instance on the web servers which gets written to, and then there's a job that pulls all the data from the web server instances into our main SQL server.
Daniel Schaffer
Awesome, thanks for the update. Very useful.
Jakob Gade
+1  A: 

I'm not sure what your exact scenario is, but if all your page is doing is processing some data, you don't really need an aspx page or an http handler at all. You could write an ASMX web service or WCF service to do what you need and this would most likely be less overhead. The WCF service doesn't even have to be hosted in ASP.NET. You can host it from a Windows service or console app, and call it in-proc using named pipes. This would probably reduce the overhead for calling the data processing code significantly.

davogones
Good call, it didn't cross my mind that this could be an internal process. I guess we need to know more about what he's doing.
Daniel Schaffer
I'll be doing some AJAX/javascript stuff, where I need to register client-side event on the server. I'm not quite sure how I'd execute WCF service method from js, but I'll do some googling on this, definately. :) For now HttpHandlers is probably the best solution for me. Thank you for your reponses.
Jakob Gade
Calling a WCF service from js is actually easier than you may think, especially if you don't need to deal with a return value (just fire and forget). See http://msdn.microsoft.com/en-us/library/bb514961.aspx for more information.
davogones
A: 

If you really have to use asp.net, you can also just hook into AuthorizeRequest step and intercept the request from there, do your processing and write your Done response directly.