OwinMiddleware and dependency injection
|Added at||2016-12-29 12:12|
I'm trying to create an OwinMiddleware object to register in a pipeline to pull out some creds from the incoming request and apply them to a service in our service container. The result of this would be that a service that requires auth that we use in the controllers will arrive pre-authed meaning the controllers no longer have to pull out authorization headers and principals and I can remove a lot of plumbing that passes these values down several layers.
We use DependencyInjection (Microsoft.Extensions.DependencyInjection) and more importantly this authed service is scoped which means it needs to be created and destroyed per request (i.e. otherwise it might accidentally use creds from previous requests). Its set to do so via a reasonably simple implementation of IDependencyResolver that uses a ServiceCollection. The scoping works via the .BeginScope() method that I don't quite understand because I can't find the sources that actually call .BeginScope() via HttpConfiguration.IDependencyResolver, I kinda get what its up to but not entirely. It works well though.
Now the problem with the object I have inheriting from OwinMiddleware is that I can't seem to get it communicating with the relevant scoped object via the IDependencyResolver.
Even if I put the IDependencyResolver into the constructor I still don't get the right instance of my service. I can verify the middleware is executing.
I saw other people talking some time ago about being able to override the Invoke() function of OwinMiddleware with extra params and this solving the scope issue. This seems to make sense if the middleware is only created once as any scoped constructor dependencies would be stale by the second request. Adding the dependency during the pipeline action makes much more sense to me. However with the version of the Owin lib I have (v3) there is only:
defined on the base so as far as I can tell my only point of entry for extra services in the middleware is via the constructor. Do I need a different version? Do I need to invoke something else special to inform the pipeline to construct new instances of my middleware per request?
This blog entry seems to state this will be fixed in aspnet 5 which I believe I'm on. So how and where is it fixed?