将DbContext创建到消息处理程序中

Lor*_*nzo 5 c# simple-injector rebus

我有一个暴露类似的类型的DLL

public class MyDbContext {
    [...]
}
Run Code Online (Sandbox Code Playgroud)

在这个库中我还有一个IPackage实现,它MyDbContext在容器中注册就像

public void RegisterServices( Container container ) {
    container.Register<MyDbContext>( Lifestyle.Scoped );
}
Run Code Online (Sandbox Code Playgroud)

然后从两种不同类型的应用程序引用该程序集: - web api项目 - asp.net mvc应用程序

这是web api项目的初始化

var container = new Container();
container.Options.DefaultScopedLifestyle = new AsyncScopedLifestyle();
InitializeContainer( container );
container.RegisterWebApiControllers( GlobalConfiguration.Configuration );
container.Verify();
Run Code Online (Sandbox Code Playgroud)

这是mvc应用程序的初始化

var container = new Container();
container.Options.DefaultScopedLifestyle = new WebRequestLifestyle();
InitializeContainer( container );
container.RegisterMvcControllers( Assembly.GetExecutingAssembly() );
container.Verify();
Run Code Online (Sandbox Code Playgroud)

当我从Rebus队列(在mvc应用程序中)收到消息时,容器尝试实例化这样的消息处理程序

public class MyHandler : BaseMessageHandler, IHandleMessages<MyMessage>, IHandleMessages<IFailed<MyMessage>>
{
    public MyHandler( ILog logger, MyDbContext context ) {
        _logger = logger;
        _context = context;
    }
}
Run Code Online (Sandbox Code Playgroud)

但我收到一个错误说

Rebus.Retry.ErrorTracking.InMemErrorTracker - 处理ID为85feff07-01a6-4195-8deb-7c8f1b62ecc3的消息时出现未处理的异常1:SimpleInjector.ActivationException:MyDbContext注册为"Web Request"生活方式,但实例是在上下文之外请求的活动(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]()
Run Code Online (Sandbox Code Playgroud)

我还尝试在mvc应用程序中设置Async Scoped Lifestyle,但错误基本相同.

Ste*_*ven 4

Rebus 在后台线程上运行处理程序,而不是在 Web 请求线程上。这意味着不可能将其用作WebRequestLifestyleRebus 管道的一部分。

您应该确保在执行处理程序之前显式启动异步作用域。您可以使用装饰器/代理来做到这一点。

最重要的是,您应该为 MVC 应用程序使用混合生活方式,因为 MVC 使用WebRequestLifestyle而不是“AsyncScopedLifestyle”。

您可以在 MVC 应用程序中应用混合生活方式,如下所示:

container.Options.DefaultScopedLifestyle = Lifestyle.CreateHybrid(
    defaultLifestyle: new AsyncScopedLifestyle(),
    fallbackLifestyle: new WebRequestLifestyle());
Run Code Online (Sandbox Code Playgroud)

你的装饰器应该如下所示:

public sealed class AsyncScopedMessageHandler<T> : IHandleMessages<T>
{
    private readonly Container container;
    private readonly Func<IHandleMessages<T>> decorateeFactory;
    public AsyncScopedMessageHandler(Container container, Func<IHandleMessages<T>> factory)
    {
        this.container = container;
        this.decorateeFactory = factory;
    }
    public async Task Handle(T message) {
        using (AsyncScopedLifestyle.BeginScope(this.container)) {
            var decoratee = this.decorateeFactory.Invoke();
            await decoratee.Handle(message);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

您可以按如下方式注册您的装饰器:

container.RegisterDecorator(
    typeof(IHandleMessages<>),
    typeof(AsyncScopedMessageHandler<>),
    Lifestyle.Singleton);
Run Code Online (Sandbox Code Playgroud)

您应该在 MVC 和 Web API 项目中注册此装饰器。

  • 嗨@Lorenzo,您不必对消息处理程序或注册进行任何更改;这就是 Simple Injector 与 Rebus 提供的精心设计的抽象相结合的美妙之处。您只需将装饰器注册为最后一个装饰器(如果您有更多装饰器),它就应该开始工作。 (2认同)