Relationship between Middleware and Application Delegate

10

I'm studying OWIN and its Katana implementation by Microsoft. I've already asked about this here and the answers will help you get a good overview of the subject. Going deeper I found this doubt. In the specification two pro-operating objects are proposed: the environment dictionary and the application delegate.

From what I understood the intuition behind it is this: the environment dictionary has all the HTTP request and response data. The application delegate, on the other hand, is a pointer to a function that receives the environment dictionary and returns a Task. In this way, the application delegate, so I understand it receives the environment dictionary with the request data, and is able to modify the response asynchronously.

So far so good. My question is the existence of middleware. From what I understand the middleware are basically components that we can plug into the pipeline and that are able to interfere with the request by modifying the response. For example, Cookie Authentication Middleware can be plugged into the pipeline and handles the response in order to implement cookie authentication. Similarly, we can plug in the WebAPI that is able to handle the request and provide the functionality of a RESTful service.

Basically, there seems to be a very close relationship between middleware and application delegate. My question, in fact, is this: per application built on top of OWIN is there only one application delegate or several that work together? In this case how does the application delegate relate to the middleware?

I imagine it is as follows: there is an application delegate per application and when we use the Use extension method to add a middleware basically what happens is that the Invoke of the middleware is added to the delegate. In this way, the application delegate can execute all the tasks in sequence, one from each middleware, allowing each middleware to handle the request one after the other in the registration sequence. Is this the way it works?

    
asked by anonymous 27.05.2014 / 00:47

2 answers

1

The middlewares are functions that receive the environment dictionary as a parameter, and shortly after they call the next middleware, which is also a function that receives the environment dictionary. So if any middleware does not call the next middleware, it completely interrupts the pipeline and for example, the request will not reach the MVC control either.

    
01.01.2017 / 07:07
0

Your study is correct.

when you use the following code snippet:

// vNext
app.Use((context, next) =>
{
    // algum funcionalidade
}

You are just adding the function to an internal list. All items in this list will be called, depending on the middleware the response can be generated or passed to the next registered middleware (note that this influences a greater control of each request, for example WebSockets where the connection and responses have a different behavior and even the cookie authentication you mentioned).

You can see this in detail here:

  

link

and here:

  

link

Owin Specification: link

And here's the code behind it: link

    
26.01.2015 / 20:59