Search code examples
vb.netwcfdependency-injectionmsmqsimple-injector

Self Hosted WCF via MSMQ Simple Injector Lifestyle


I'm using WCF (self-hosted) using MSMQ bindings and wanted to use the Per WCF request lifestyle with SimpleInjector. I setup my code as:

Dim httpLifecycle = New SimpleInjector.Integration.Wcf.WcfOperationLifestyle(True)
container.Register(of ISomeType, SomeType)(httpLifecycle)

Over MSMQ, this doesn't work, as I get the error:

The dependency resolver for the web services host failed to initialize: SimpleInjector.ActivationException: The registered delegate for type ILoader threw an exception. The ISomeType is registered as 'WCF Operation' lifestyle, but the instance is requested outside the context of a WCF Operation.

Is there a way the WCF request lifestyle an work with MSMQ?


Solution

  • I ended up using a per execution scope and defining that scope within the WCF service operation itself. By scoping it that way, it ensured the dependencies opened and closed when I wanted them to.

    I needed to include execution context scoping.