Reputation: 3515
What is the difference between HttpApplication class and IHttpModule? Are they both same or different?
I see articles that mention the same events in both the classes.
Upvotes: 3
Views: 2699
Reputation: 105081
HttpApplication
is the web application instance that has multiple IHttpModule
instances registered in it. That's why every IHttpModule
instance handles a certain part of application execution thus can usually be reused on many applications (think of a Session
handling module or authentication module).
In terms of application/request handling there are many similarities. HttpApplication
has access to application-level events like OnStart, OnEnd etc as well as request-level events like OnBeginRequest, OnEndRequest etc. IHttpModule
on the other hand only has access to request-level events.
Additional note: It's possible to handle application start and end events even using an IHttpModule, but this is a non-documented feature and you have to take some special precautions. Check these two blog posts that explain it all into great depth:
Writing a customIHttpModule
that handlesApplication_OnStart
event
How to correctly useIHttpModule
to handleApplication_OnStart
event
I suggest you read a bit about application life cycle execution pipeline:
Upvotes: 4
Reputation: 64943
IHttpModule is a managed request handler, which means that implements "catches" for the request life-cycle.
HttpApplication represents the Web server (IIS) ASP.NET application. Right, it has the events and other members that you need in the context of an HTTP Module.
You may develop a HTTP module if you need to implement a customized way of handling some input data given by some HTTP request. Obviously, that may require you to access the HTTP application, which is the one exposing the request life-cycle events.
The summary is HTTP module is an observer of HTTP application that may implement logic in order to process an HTTP request in the way the HTTP application signals every step of ASP.NET infraestructure life-cycle.
Upvotes: 0
Reputation: 6903
http://aspalliance.com/442_Introducing_HTTPModules.all
Good article that should help your understanding of how HttpModules and HttpApplications work together.
Upvotes: 0
Reputation: 9513
There isn't any great difference other than HttpModules allow you to remove or separate the handling of an event from the rest of your application. This can help with code reuse, particularly across applications.
Furthermore, using this technique you can add in HttpModules to your application without having to change any existing source code (e.g. Elmah) to provide extra functionality.
Upvotes: 0