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

问题描述 投票:0回答:1

我一直在关注NServiceBus示例,专门针对如何使用与Sql Persistence集成的实体框架(核心)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容器?

仅供参考,我查看了this问答,这给了我注入IServiceScopeFactory的想法。不幸的是,答案没有显示如何实际获取接口的实例。

nservicebus
1个回答
0
投票
您将在https://docs.particular.net/nservicebus/dependency-injection/<T>();方法中使用Invoke来解析IServiceScopeFactory之类的任何对象。

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

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

  • 您也可以通过创建功能来执行此操作

  • © www.soinside.com 2019 - 2024. All rights reserved.