如何将依赖项注入 NServiceBus 管道行为?

How to inject dependency into NServiceBus pipeline behavior?

我一直在关注 NServiceBus 示例,特别是如何使用与 Sql 持久性集成的 entity framework(核心)DbContext,以便我可以保存 dbcontext 状态更改以及发件箱消息.这是样本:https://docs.particular.net/samples/entity-framework-core/

我稍微修改了工作单元代码以支持创建 aspnet 核心 DI 范围的 DbContext。相关代码如下:

    public class UnitOfWork<TDbContext>
        where TDbContext : DbContext
    {
        private Func<SynchronizedStorageSession, IServiceProvider, TDbContext> _contextFactory;
        private TDbContext _context;
        private IServiceProvider _serviceProvider;

        public UnitOfWork(Func<SynchronizedStorageSession, IServiceProvider, TDbContext> contextFactory, IServiceProvider serviceProvider)
        {
            _contextFactory = contextFactory;
            _serviceProvider = serviceProvider;
        }

        public TDbContext GetDataContext(SynchronizedStorageSession storageSession)
        {
            if (_context == null)
            {
                _context = _contextFactory(storageSession, _serviceProvider);
            }
            return _context;
        }
    }

    public class UnitOfWorkSetupBehavior<TDbContext> : Behavior<IIncomingLogicalMessageContext>
        where TDbContext : DbContext
    {
        private readonly Func<SynchronizedStorageSession, IServiceProvider, TDbContext> _contextFactory;
        private readonly IServiceScopeFactory _serviceScopeFactory;

        public UnitOfWorkSetupBehavior(Func<SynchronizedStorageSession, IServiceProvider, TDbContext> contextFactory, IServiceScopeFactory serviceScopeFactory)
        {
            _contextFactory = contextFactory;
            _serviceScopeFactory = serviceScopeFactory;
        }

        public override async Task Invoke(IIncomingLogicalMessageContext context, Func<Task> next)
        {
            using (var scope = _serviceScopeFactory.CreateScope())
            {
                var uow = new UnitOfWork<TDbContext>(_contextFactory, scope.ServiceProvider);
                context.Extensions.Set(uow);

                await next().ConfigureAwait(false);

                context.Extensions.Remove<UnitOfWork<TDbContext>>();
            }
        }
    }

    public static class EndpointConfigurationExtensions
    {
        public static void RegisterUnitOfWork<TDbContext>(this EndpointConfiguration endpointConfiguration, IServiceScopeFactory serviceScopeFactory)
            where TDbContext : DbContext
        {
            var pipeline = endpointConfiguration.Pipeline;
            pipeline.Register(new UnitOfWorkSetupBehavior<TDbContext>((storageSession, serviceProvider) =>
            {
                var dbConnection = storageSession.SqlPersistenceSession().Connection;
                var dbContextFactory = serviceProvider.GetService<IDbContextConnectionFactory<TDbContext>>();
                var dbContext = dbContextFactory.GetDbContext(dbConnection);

                //Use the same underlying ADO.NET transaction
                dbContext.Database.UseTransaction(storageSession.SqlPersistenceSession().Transaction);

                //Call SaveChanges before completing storage session
                storageSession.SqlPersistenceSession().OnSaveChanges(x => dbContext.SaveChangesAsync());

                return dbContext;
            }, serviceScopeFactory), "Sets up unit of work for the message");
        }
    }

    public static class UnitOfWorkContextExtensions
    {
        public static TDbContext DataContext<TDbContext>(this IMessageHandlerContext context)
            where TDbContext : DbContext
        {
            var uow = context.Extensions.Get<UnitOfWork<TDbContext>>();
            return uow.GetDataContext(context.SynchronizedStorageSession);
        }
    }

为此行为需要注入 IServiceScopeFactory。

现在我能够找到的所有行为注册示例仅显示手动实例化并传递到端点配置管道的类型。

有没有一种方法可以通过行为的 Invoke 方法访问 IServiceScopeFactory(也许通过某些扩展通过上下文?),或者是否可以注册行为本身,以便我可以用服务构造它由 DI 容器创建?

仅供参考,我看了 问答,这让我想到了注入 IServiceScopeFactory。不幸的是,答案并未显示如何实际获取接口实例。

您可以在 Invoke 方法中使用 context.builder.Build<T>(); 来解析任何对象,例如 IServiceScopeFactory.

确保 IServiceScopeFactory 已在 DI 容器中注册。例如,在端点初始化期间:

endpointConfiguration.RegisterComponents(registration: x =>
    {
        x.ConfigureComponent<IServiceScopeFactory>(yourServiceScopeFactory);
    });

您也可以通过创建一个功能来做到这一点