antonyvorontsov / RabbitMQ.Client.Core.DependencyInjection

.Net Core library-wrapper of RabbitMQ.Client for Dependency Injection.
MIT License
111 stars 36 forks source link

Scope Exception #93

Open gergomiho opened 3 years ago

gergomiho commented 3 years ago

I am trying to inject a Scoped context to a Transient message handler and I am receiving an exception

Example code (removed the config part) :

services.AddRabbitMqClientTransient(new RabbitMqClientOptions{}).AddProductionExchange("events.user", new RabbitMqExchangeOptions{}).AddConsumptionExchange("events.essay", new RabbitMqExchangeOptions{}).AddAsyncNonCyclicMessageHandlerTransient<EssayCreatedMessageHandler>("essay.created", "events.essay");

The exception I receive is :

System.AggregateException: 'Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: RabbitMQ.Client.Core.DependencyInjection.Services.IMessageHandlerContainerBuilder Lifetime: Singleton ImplementationType: RabbitMQ.Client.Core.DependencyInjection.Services.MessageHandlerContainerBuilder': Cannot consume scoped service 'UserAPI.Infrastructure.UserContext' from singleton 'RabbitMQ.Client.Core.DependencyInjection.Services.IMessageHandlerContainerBuilder'.) (Error while validating the service descriptor 'ServiceType: RabbitMQ.Client.Core.DependencyInjection.Services.IMessageHandlingService Lifetime: Singleton ImplementationType: RabbitMQ.Client.Core.DependencyInjection.Services.MessageHandlingService': Cannot consume scoped service 'UserAPI.Infrastructure.UserContext' from singleton 'RabbitMQ.Client.Core.DependencyInjection.Services.IMessageHandlerContainerBuilder'.)'

I think this shouldn't happen because I am registering the handler as Transient, and Transient can consume Scoped. The message doesn't change if I use AddRabbitMqClient. I've fixed it by using the CreateScope function, but this is not how it should behave.

.NET 5 RabbitMQ.Client.Core.DependencyInjection Version 4.3.0