Open Closed

Injecting repositories in a class that implements IDistributedEventHandler - RabbitMQ #499


User avatar
0
DanielAndreasen created

Im attempting to inject a repository generated with the ABP suite in a class which implements the IDistributedEventHandler interface. RabbitMQ is configured as the distributed event bus provider.

The documentation describes that "You can inject any service and perform any required logic here..." in context of subscribing to events with IDistributedEventHandler. However when I inject repositories or other classes which is registered to dependency injection with the ITransientDependency interface, the eventhandler is never instantiated. It seems like the eventhandler is only configured properly if it contains either a empty constructor or a constructor with IDistributedEventBus as parameter.

The following implementation should illustrate what I wish to accomplish:

using Stella.GGSensorData;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;
using Volo.Abp.EventBus.Distributed;

 public class GGSensorDataHandler : IDistributedEventHandler<GGSensorDataEntity>, ITransientDependency
    {
        private readonly IGGSensorDataRepository _gGsensorDataRepository;
        private readonly RawSensorDataParser _rawSensorDataParser;


        public GGSensorDataHandler(RawSensorDataParser rawSensorDataParser, IGGSensorDataRepository gGSensorDataRepository)
        {
            _rawSensorDataParser = rawSensorDataParser;
            _gGsensorDataRepository = gGSensorDataRepository;
        }

        public async Task HandleEventAsync(GGSensorDataEntity eventData)
        {
            await _rawSensorDataParser.ParseAndInsertRawSensorData(eventData);
            await InsertRawSensorDataStellaDb(eventData);
        }

        private async Task InsertRawSensorDataStellaDb(GGSensorDataEntity sensorData)
        {
            sensorData.Json = JsonSerializer.Serialize(sensorData);

            await _gGsensorDataRepository.InsertAsync(sensorData);
        }

How do I properly inject repositories/services in a class that implements IDistributedEventHandler like above?

I should note that the "GGSensorDataHandler" class is implemented in a project separate to our main ABP project and references the repositories via a project reference dependency:

The intention is simply to implement the project as a microservice.

Unit of work Using Unit of work as suggested here and similarly in the abp documentation does not solve this issue.

Project Details

  • ABP Framework version: v3.2.1
  • Project Type: .Net Core console app

6 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi, it works for me. could you using startup template to reproduce this problem?

  • User Avatar
    0
    DanielAndreasen created

    Perhaps I am missing some dependecies or configuration of dependency injection? Can you show which dependecies is used in the working example you presented?

    I am experiencing the same problem in a project build on the startup template (eventhandler and dependant classes placed in the .Host project)

    Project structure of RabbitMQ consumer implementation in a project based on startup template

    Below is the full structure of our main project and the SensorData "microservice"

    Main method in Program.cs

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    **I am experiencing the same problem in a project build on the startup template **

    Could you send the startup template proejct to me? shiwei.liang@volosoft.com. thanks.

  • User Avatar
    0
    DanielAndreasen created

    I have sent the project to you on Mail

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I used the project you provided and it works fine. I can help you remotely . if you are free, please contract me.

  • User Avatar
    0
    DanielAndreasen created

    I think remote help is a very good idea – how do we set this up?

Made with ❤️ on ABP v8.2.0-preview Updated on March 25, 2024, 15:11