I'm using Steeltoe to implement Event Messaging, with RabbitMQ, between microservices, but I'm having an issue when I register my Listener service and it not recognizing other DI services.
In my Startup.cs
file I have my services being registered as follows:
public void ConfigureServices(IServiceCollection servcies)
{
...
// Add my custom service as a scoped service
services.AddScoped<IMyService>(provider => new MyService());
var rabbitSection = configuration.GetSection(RabbitOptions.PREFIX);
services.Configure<RabbitOptions>(rabbitSection);
services.AddRabbitServices();
services.AddRabbitAdmin();
services.AddRabbitTemplate();
// Add Rabbit Listener Service
services.AddSingleton<MyRabbitListenerService>();
services.AddRabbitListeners<MyRabbitListenerService>();
...
}
... then in my MyRabbitListenerService.cs
class:
public class MyRabbitListenerService
{
private readonly IMyService _myService;
public MyRabbitListenerService(IMyService myService)
{
_myService = myService;
}
[RabbitListener("MyQueue")]
public async Task MessageListener(byte[] message)
{
// Do stuff ...
}
}
When I run this, I'm getting an error indicating the IMyService
couldn't be injected into the Listener service as it wasn't registered. I can't work out why this isn't working. Is it because I'm trying to inject a scoped service into a singleton service?
UPDATE
I did some testing and changing IMyService from a scoped service to a singleton made it work. Now I need to figure out how to get around this because in my situation it doesn't make sense to register IMyService as a singleton.