Skip to content

Commit

Permalink
MessageConsumer created according to servicelifetime.
Browse files Browse the repository at this point in the history
  • Loading branch information
byerlikaya committed Jun 27, 2024
1 parent e836641 commit 12e72dc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Basic.RabbitMQ/Extensions/ServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,36 @@ private static void CreateServices(
VirtualHost = messageBrokerOptions.VirtualHost,
DispatchConsumersAsync = true,
AutomaticRecoveryEnabled = true,
NetworkRecoveryInterval = TimeSpan.FromSeconds(30)
NetworkRecoveryInterval = TimeSpan.FromSeconds(30),
ClientProvidedName = "Basic.RabbitMQ"
});

services.AddSingleton<RabbitMqClientService>();
services.AddSingleton<IMessageConsumer, MessageConsumer>();

CreateConsumerService(services, messageConsumerServiceLifetime);
CreateConsumerAndProducerService(services, messageConsumerServiceLifetime);
}

private static void CreateConsumerService(
private static void CreateConsumerAndProducerService(
IServiceCollection services,
ServiceLifetime messageConsumerServiceLifetime)
{
switch (messageConsumerServiceLifetime)
{
case ServiceLifetime.Singleton:
services.AddSingleton<IMessageProducer, MessageProducer>();
services.AddSingleton<IMessageConsumer, MessageConsumer>();
break;
case ServiceLifetime.Scoped:
services.AddScoped<IMessageProducer, MessageProducer>();
services.AddScoped<IMessageConsumer, MessageConsumer>();
break;
case ServiceLifetime.Transient:
services.AddTransient<IMessageProducer, MessageProducer>();
services.AddTransient<IMessageConsumer, MessageConsumer>();
break;
default:
services.AddSingleton<IMessageProducer, MessageProducer>();
services.AddSingleton<IMessageConsumer, MessageConsumer>();
break;
}
}
Expand Down

0 comments on commit 12e72dc

Please sign in to comment.