首页 > 解决方案 > 什么时候做注入依赖集成事件,控制器不调用c#

问题描述

我正在尝试使用事件总线在微服务之间进行通信,当我使用依赖注入时,我的控制器无法再被调用。

我有我的Controller

  public class CustomersController : ControllerBase
    {
        private readonly ICustomerRepository _customerRepository;
        private readonly IIdentityService _identityService;
        private readonly ICustomerIntegrationEventService _customerIntegrationEventService; 

        public CustomersController(ICustomerRepository customerRepository, IIdentityService identityService, ICustomerIntegrationEventService customerIntegrationEventService)
        {
            _customerRepository = customerRepository ?? throw new ArgumentNullException(nameof(customerRepository));
            _identityService = identityService ?? throw new ArgumentNullException(nameof(identityService));
            _customerIntegrationEventService = customerIntegrationEventService ?? throw new ArgumentNullException(nameof(customerIntegrationEventService));
        }

        }

在此Controller,我有一个名为Add. 它基本上添加了一个客户端。添加客户端后,我想通知其他微服务并将数据发送到服务总线。到目前为止,我正在使用集成事件。但是在控制器中完成依赖注入的那一刻。前面不能再碰到控制器,返回错误500

    public async Task<IActionResult> Add(Customer value)
        {
            var idAdded = await _customerRepository.Add(value).ConfigureAwait(false);
            if (!idAdded.HasValue)
                return BadRequest();
            var integrationEvent = new CustomerIntegrationEvent(idAdded.Value, value);
            await _customerIntegrationEventService.AddAndSaveEventAsync(integrationEvent);
            return CreatedAtAction(nameof(Get), new { id = idAdded.Value }, null);
        }

下面是这个类如何构建 _customerIntegrationEventService

客户集成事件服务

  public class CustomerIntegrationEventService : ICustomerIntegrationEventService
    {

        private readonly Func<DbConnection, IIntegrationEventLogService> _integrationEventLogServiceFactory;
        private readonly IEventBus _eventBus;
        private readonly ApplicationDataContext _osDataContext;
        private readonly IntegrationEventLogContext _eventLogContext;
        private readonly IIntegrationEventLogService _eventLogService;

        public CustomerIntegrationEventService(
            IEventBus eventBus,
            ApplicationDataContext hrDataContext,
            IntegrationEventLogContext eventLogContext,
            Func<DbConnection, IIntegrationEventLogService> integrationEventLogServiceFactory)
        {
            _osDataContext = hrDataContext ?? throw new ArgumentNullException(nameof(hrDataContext));
            _eventLogContext = eventLogContext ?? throw new ArgumentNullException(nameof(eventLogContext));
            _integrationEventLogServiceFactory = integrationEventLogServiceFactory ?? throw new ArgumentNullException(nameof(integrationEventLogServiceFactory));
            _eventBus = eventBus ?? throw new ArgumentNullException(nameof(eventBus));
            _eventLogService = _integrationEventLogServiceFactory(hrDataContext.Database.GetDbConnection());
        }

        public async Task PublishEventsThroughEventBusAsync()
        {
            var pendindLogEvents = await _eventLogService.RetrieveEventLogsPendingToPublishAsync();
            foreach (var logEvt in pendindLogEvents)
            {
                try
                {
                    await _eventLogService.MarkEventAsInProgressAsync(logEvt.EventId);
                    _eventBus.Publish(logEvt.IntegrationEvent);
                    await _eventLogService.MarkEventAsPublishedAsync(logEvt.EventId);
                }
                catch (Exception)
                {
                    await _eventLogService.MarkEventAsFailedAsync(logEvt.EventId);
                }
            }
        }

        public async Task AddAndSaveEventAsync(IntegrationEvent evt)
        {
            await _eventLogService.SaveEventAsync(evt, _osDataContext.Database.CurrentTransaction.GetDbTransaction());
        }
    }

所有这些代码均取自示例https://docs.microsoft.com/en-us/dotnet/standard/microservices-architecture/multi-container-microservice-net-applications/subscribe-events

我在启动时进行了依赖注入,但无论如何错误仍然存​​在

     public void AddIntegrationServices(IServiceCollection services, IConfiguration configuration)
        {
            services.AddTransient<Func<DbConnection, IIntegrationEventLogService>>(
              sp => (DbConnection c) => new IntegrationEventLogService(c));

            services.AddTransient<ICustomerIntegrationEventService, CustomerIntegrationEventService>();

        }

我怎么能至少看到事情背后的错误,或者我是如何想出那个解决方案的。此代码基于 microsoft eShopOnContainers

标签: c#microservicesevent-bus

解决方案


推荐阅读