Hybrid solution that uses a single scope, covering OWIN middleware, and either MVC, WebApi or SignalR

Oct 20, 2014 at 4:43 AM
I'm using a solution where I setup the container with ExecutionContextScope in an Owin middleware,
and then use that scope throughout the full request, covering either MVC, WebApi or SignalR.

For now i'm experimenting without using a hybrid scope for MVC.
But seeing there are posts pointing out the scope saved in CallContext might get lost if ASP.NET migrates threads, I might have to use one.
I'm hoping recent advancements might make that unnecessary :)

Already in between the execution of the Owin Middleware, and the execution of MVC (or even WebApi if MVC is also within the same application) I have to restore the Scope into the CallContext, however no other issues experienced so far..

If there's any interest in such a solution I don't mind cleaning it up and sharing it.
Jan 27, 2015 at 8:45 AM
Yes PLEASE! :)

I've had a lot of troubles with OWIN and SimpleInjector and would really like to see your solutions! Please share it, it will be greatly appreciated! :)
Jan 27, 2015 at 8:57 AM
My initial thought is that call context should simply work in ASP.NET. ASP.NET should (or at least from .NET 4.5 and up) use the same call context throughout the complete web request. The only problem I'm currently aware of is that (one of the earlier versions of) Web API disposes the IDependencyScope after the callcontext is removed. This is normally not an issue, but disallows you to do any cleanup in your code that still needs the existence of such callcontext. IMO this is a bug in Web API. I'm not sure if this issue still exists.
Jan 27, 2015 at 9:03 AM
@dot_NET_Junkie I would be very interested to read about how to work with owin and SI. Could you point me in the correct directions. Some examples of what we are struggling with:
  • Injecting IWhatever into a OwinMiddleware
  • In anyway possible, make it so we can inject IOwinContext somewhere later in our app (where context is not normally available)
  • PerWebApiRequest vs PerOwinContext. When and why?
Jan 29, 2015 at 10:41 PM
Hi @abergs, take a look at this thread, it contains a quite elaborate discussion about how to use Owin with Simple Injector. Using the information described here, you should be up and running in no time.
Feb 11, 2015 at 3:02 PM
Hi @dot_NET_Junkie. Please see my comment in that thread - It's day 107 and I still haven't gotten IOwinContext resolution to work.