【发布时间】:2017-04-20 11:31:04
【问题描述】:
我有一个 dll 暴露了一个像
这样的类型public class MyDbContext {
[...]
}
在这个库中,我还有一个IPackage 实现,它在容器中注册MyDbContext,如
public void RegisterServices( Container container ) {
container.Register<MyDbContext>( Lifestyle.Scoped );
}
然后从两种不同类型的应用程序引用此程序集: - 一个网络 API 项目 - 一个 asp.net mvc 应用程序
这是web api项目的初始化
var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
InitializeContainer( container );
container.RegisterWebApiControllers( GlobalConfiguration.Configuration );
container.Verify();
这是mvc应用程序的初始化
var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer( container );
container.RegisterMvcControllers( Assembly.GetExecutingAssembly() );
container.Verify();
当我从 Rebus 队列(在 mvc 应用程序中)收到消息时,容器会尝试像这样实例化消息处理程序
public class MyHandler : BaseMessageHandler, IHandleMessages<MyMessage>, IHandleMessages<IFailed<MyMessage>>
{
public MyHandler( ILog logger, MyDbContext context ) {
_logger = logger;
_context = context;
}
}
但我收到一条错误提示
Rebus.Retry.ErrorTracking.InMemErrorTracker - 处理 ID 为 85feff07-01a6-4195-8deb-7c8f1b62ecc3 的消息时未处理的异常 1:SimpleInjector.ActivationException:MyDbContext 已注册为“Web 请求”生活方式,但在外部请求实例活动(Web 请求)范围的上下文。
使用以下堆栈跟踪
at SimpleInjector.Scope.GetScopelessInstance[TImplementation](ScopedRegistration`1 registration)
at SimpleInjector.Scope.GetInstance[TImplementation](ScopedRegistration`1 registration, Scope scope)
at SimpleInjector.Advanced.Internal.LazyScopedRegistration`1.GetInstance(Scope scope)
at lambda_method(Closure )
at SimpleInjector.InstanceProducer.GetInstance()
at SimpleInjector.Container.GetInstance[TService]()
我也尝试在 mvc 应用程序中设置 Async Scoped Lifestyle,但错误基本相同。
【问题讨论】:
标签: c# simple-injector rebus