Relationship between Middleware and Application Delegate

Asked

Viewed 151 times

10

I’m studying the OWIN and its implementation Katana by Microsoft. I’ve asked about it here and the answers already help to have a good overview of the subject. Going deeper I found this doubt. In the specification are proposed two pro functioning objects of everything: the Dictionary Environment and the delegate application.

From what I understand the intuition behind this is the following: 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. Thus, the application delegate, from what I understand receives the Dictionary Environment with the request data, and is able to modify the response asynchronously.

So far so good. My doubt is that there are 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 answer. For example the Cookie Authentication Middleware can be plugged into the pipeline and manipulates the response to implement cookie authentication. Similarly, we can plug in the Webapi that is capable of handling 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 doubt, in fact, is the following: by application built on top of the OWIN exists only one application delegate or several working together? In that case how does application delegate relate to middleware?

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

2 answers

1

Middleware are functions that receive the Environment Dictionary as a parameter, and then 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 even reach the MVC control.

0

Your study is correct.

when you use the following code snippet:

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

You’re just adding the function to an internal list. All items in this list will be called, depending on the middleware the answer 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 authentication by Cookie you mentioned).

you can see this in detail here:

http://whereslou.com/2014/05/28/asp-net-moving-parts-ibuilder/

and here:

http://benjamincollins.com/the-katana-pipeline/

Specification of the Owin: http://owin.org/

And here’s the code behind it: https://github.com/aspnet/AspNetKatana/blob/dev/src/Microsoft.Owin/Builder/AppBuilder.cs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.