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?