Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message Handlers and the Http Module?

In ASP.NET Web API, HTTP requests and responses are processed in a pipeline on the server.

If I want to add some logic or global behaviour at the very early stages of the pipeline , I should do it in the Message Handler. ( for example : authentication)

But what about the Http module pipeline ? where it fits in this whole story ?

Looking at this numbered stages of web api life cycle :

http://i.stack.imgur.com/jkQe8.jpg

enter image description here

But looking at the Http module general events ( contains more but...) enter image description here

Question :

— How those 2 systems combine and where ? I mean if there was 1 picture which contains web api and http module , how would the numbers be ? (I added numbers in the images for easy referencing)

— I always hear that If i want to do things earlier at the pipeline I should use message handlers , but what about HttpModule's BeginRequest for example ? I know that there are objects which are null at this stage but still , later phases at the httpmodule does inflate HttpContetxt's objects - and yet , webapi's guys says : use MessageHandlers....(is it relate to the fact of selfhoster environment) ?

like image 279
Royi Namir Avatar asked Oct 24 '14 18:10

Royi Namir


People also ask

What are HTTP handlers and HTTP modules?

HTTP modules and HTTP handlers are an integral part of the ASP.NET architecture. While a request is being processed, each request is processed by multiple HTTP modules (for example, the authentication module and the session module) and is then processed by a single HTTP handler.

What is an HTTP message handler?

A message handler is a class that receives an HTTP request and returns an HTTP response. Message handlers derive from the abstract HttpMessageHandler class. Typically, a series of message handlers are chained together.

What is HTTP module in Web API?

An HTTP module is an assembly that is called on every request to an application. It's a part of the HTTP request and response pipeline. Even the ASP.NET engine uses HttpModule to perform certain operations (which are outside our interests here).

What is HTTP module in asp net core?

HTTP Modules are a concept of the classic ASP.NET / IIS application model. ASP.NET Core does not use IIS' event system, the requests that IIS received are forwarded to the ASP.NET Core app, processed by this application and returned. If you are using an http module inside a web.


1 Answers

To combine the figure below into the top one, imagine IHttpHandler box in the top figure corresponds to ASPX in the below image so that you put the down image to the bottom left of the top one. So, 8 and 9 are part of the IIS ASP.NET pipeline. IIS pipeline runs modules, etc and terminates when a handler handles the request. With web API, that handler happens to be HttpControllerHandler and this is where Web API pipeline starts. If you look at HttpControllerHandler, the request and response are ASP.NET specific to its left and to its right it becomes HttpRequestMesssage which is Web API specific.

To your second question, the earliest you could do in Web API pipeline will be a message handler. HttpModule will be even more earlier but is not part of Web API but the hosting. Trade-off is that if you have an HttpModule, you can use it only in IIS whereas a message handler can run in any host, since it is Web API specific and host-specific. I keep referring to my MSDN article in my recent SO replies but then it so happens that the article is relevant to the questions being asked. So, I have no other go but to again link it. Here you go. I have comparison of different options in that article.

like image 178
Badri Avatar answered Oct 24 '22 06:10

Badri