I have a one way WCF service using the MSMQ Binding which is activated using Windows Activation Service in IIS 7.0.
I'm a big fan on NInject so I've been using the NInject extension for WCF, which for a typical HTTP WCF service would work great.
However, in WAS activate services there is no HTTP pipeline, so I can't use InRequestScope when binding my types because System.Web.HttpContext.Current is null. I'm struggling to find an alternative when using WAS that will give me what I want. AspCompatibility mode attribute doesn't work in this mode either.
I thought InThreadScope might work, but the service is created in a separate thread than what it is executed in.
So basically I need the equivalent of the the HttpContext for WCF+WAS to scope my objects at the request level. Is there some static object in this world that would work the same way or does anyone else have any ideas on something I can hack together?
I implemented my own WCF extensions for Ninject 2.0 before I knew there was an this up on github. My implementation differs slightly, but I did come up with a solution to scoping objects:
For completeness, this is how I use the callback defined above:
The haven't hosted WCF services in WAS, so not sure if you'd use the
WcfWebContext
orWcfContext
defined above, but you can try 'em out and see. IfWebOperationContext
works, then you're all set. Otherwise, I found things are a bit more complicated. You'll note the code snippet above uses aNinjectInstanceContext
class that is attached to theOperationContext
. This is a class I wrote that uses Ninject 2.0's "cache and collect" mechanism that allows objects to be deterministically disposed. Basically, the class is implementsIExtension<InstanceContext>
which is a WCF construct for attaching almost anything to theOperationContext
. This class also implement Ninject'sINotifyWhenDisposed
interface which is what provides support for deterministic disposal. Here's what the class definition looks like:The rest of my WCF extension for Ninject is the same as the one on github. What basically happens is that an instance provider is created which is plugged into the WCF "activation" chain -- I'm not using their specific terminology, just how I understand things. So, the idea is that your instance provider is supposed to supply instances of the WCF service class being requested. So, here's where we use Ninject to produce the service instance. By doing so, we can also activate and inject any dependencies. What the instance provider does in my implementation is wrap up the Ninject kernel in an instance if
NinjectInstanceContext
and attach it to theOperationContext
. The creation of the service is then delegated to this WCF extension. When the instance provider is told to release a service, theNinjectInstanceContext
that was attached to the OperationContext is disposed which by way of implementingINotifyWhenDisposed
causes deterministic disposal of the service (and potentially its dependencies).Hope this discussion helps. I'll see if I can get some more concrete code posted here if you're interested.