TL;DRDo not call next, and fill in the "owin.ResponseBody" environment key.
WhyUsually one middleware calls the next and that is how the pipeline is executed and how different pieces of middleware gets to each do their thing. At the end of the pipeline is the application where you do whatever application logic to handle the request. There are situations, though, where it makes sense for requests to not even reaching the application logic:
- Requests from unpaying customers could be throttled to some low rate
- Requests from certain IPs could be blocked
- Requests without valid authentication tokens could be turned away with a 401 status
- Some endpoint are not for application use - like monitoring endpoints
Build a Monitor MiddlewareLets take a look at building a middleware that provides a monitoring endpoint. It will listen on the path
"/_monitor"and will respond with a small
jsonobject containing the version of the deployed software.
Once again I implement the middleware using the OWIN Midfunc - which we can think of as a curried function taking first the next piece of middleware in the pipeline and then the OWIN environment dictionary.
To implement the monitoring endpoint I look up the request path in the environment dictionary and decide if it is a monitoring path. If it is I write a simple
jsonstring to the response body, set the content type header and return. On the other hand if it is not a monitoring endpoint I just pass the environment into the next middleware in the pipeline.
Here is the code: