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)
-
0
-
0
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"
-
0
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.
-
0
I have sent the project to you on Mail
-
0
-
0
I think remote help is a very good idea – how do we set this up?