This is a known problem with Simple Injector 2.2.3 for some WCF configurations. Some WCF configurations force a second call to IInstanceProvider.GetInstance
. The SimpleInjectorInstanceProvider.GetInstance
methods call into the internal BeginScope
method, which is limited to one scope.
There are two solutions:
First solution is to upgrade to the latest Simple Injector WCF Integration package. The 2.3.0 release of the WCF integration package fixed this issue.
Second solution is to not use the Per WCF Operation at all, but use the lifetime scope (which works similar as the Per WCF Operation lifestyle). This is especially a good solution in your case, since you use command handlers and query handlers. In other words, your WCF service will probably consist of just two WCF operations. This makes it very easy to wrap those two methods in a lifetime scope, without needing to worry the code has to be added to all other operations in the service (since your service won't get any more operations). Your operation will look like this:
[OperationContract]
public object Execute(dynamic command)
{
Type commandHandlerType = typeof(ICommandHandler<>)
.MakeGenericType(command.GetType());
using (Bootstrapper.BeginLifetimeScope())
{
dynamic commandHandler = Bootstrapper.GetInstance(commandHandlerType);
commandHandler.Handle(command);
}
return command;
}
Of course, all registrations made with Per WCF Operation should be changed to Lifetime scope:
_container.Register<MyDbContext, MyDbContext>(new LifetimeScopeLifestyle());