Skip to content

Commit

Permalink
[UPDATE] Configuration of services with providers.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallebelins committed Jan 5, 2024
1 parent 8654ecd commit f044d76
Show file tree
Hide file tree
Showing 15 changed files with 312 additions and 94 deletions.
6 changes: 3 additions & 3 deletions src/Mvp24Hours.Application/Mvp24Hours.Application.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/Mvp24Hours.Core/Mvp24Hours.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,49 @@ public static IServiceCollection AddMvp24HoursRabbitMQ(this IServiceCollection s
throw new ArgumentNullException(nameof(assemblyConsumers));
}

var types = assemblyConsumers.GetExportedTypes()
.Where(t => t.InheritsOrImplements(typeof(IMvpRabbitMQConsumer)))
.ToList();
if (connectionOptions != null)
{
services.Configure(connectionOptions);
}
else
{
services.Configure<RabbitMQConnectionOptions>(connectionOptions => { });
}

foreach (var type in types)
services.AddSingleton<IMvpRabbitMQConnection, MvpRabbitMQConnection>();

if (clientOptions != null)
{
services.Configure(clientOptions);
}
else
{
MvpRabbitMQClient.Register(type);
services.Configure<RabbitMQClientOptions>(options => { });
}

services.AddSingleton(sp =>
{
var client = new MvpRabbitMQClient(sp);

assemblyConsumers.GetExportedTypes()
.Where(t => t.InheritsOrImplements(typeof(IMvpRabbitMQConsumer)))
.ToList()
.ForEach(x => client.Register(x));

return client;
});

return services;
}

/// <summary>
/// Add rabbitmq
/// </summary>
public static IServiceCollection AddMvp24HoursRabbitMQWithConsumers(this IServiceCollection services,
Action<RabbitMQConnectionOptions> connectionOptions,
Action<RabbitMQClientOptions> clientOptions,
params Type[] typeConsumers)
{
if (connectionOptions != null)
{
services.Configure(connectionOptions);
Expand All @@ -57,7 +91,16 @@ public static IServiceCollection AddMvp24HoursRabbitMQ(this IServiceCollection s
services.Configure<RabbitMQClientOptions>(options => { });
}

services.AddSingleton<MvpRabbitMQClient, MvpRabbitMQClient>();
services.AddSingleton(sp =>
{
var client = new MvpRabbitMQClient(sp);

if (typeConsumers.AnySafe())
foreach (var item in typeConsumers)
client.Register(item);

return client;
});

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
110 changes: 60 additions & 50 deletions src/Mvp24Hours.Infrastructure.RabbitMQ/MvpRabbitMQClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,23 @@ public class MvpRabbitMQClient
private readonly RabbitMQClientOptions _options;
private readonly IMvpRabbitMQConnection _connection;
private readonly Dictionary<string, IModel> _channels;
private readonly IServiceProvider _provider;
private readonly List<IMvpRabbitMQConsumer> _consumers;

protected virtual RabbitMQClientOptions Options => _options;
protected virtual IMvpRabbitMQConnection Connection => _connection;
protected virtual Dictionary<string, IModel> Channels => _channels;
#endregion

#region [ Ctors ]
[ActivatorUtilitiesConstructor]
public MvpRabbitMQClient(IOptions<RabbitMQClientOptions> options, IMvpRabbitMQConnection connection)
: this(options?.Value, connection)
{
}

public MvpRabbitMQClient(RabbitMQClientOptions options, IMvpRabbitMQConnection connection)
public MvpRabbitMQClient(IServiceProvider _provider)
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-ctor");
this._options = options ?? throw new ArgumentNullException(nameof(options));
this._connection = connection ?? throw new ArgumentNullException(nameof(connection));
this._provider = _provider;
this._options = _provider.GetService<IOptions<RabbitMQClientOptions>>()?.Value ?? throw new ArgumentNullException(nameof(_provider));
this._connection = _provider.GetService<IMvpRabbitMQConnection>() ?? throw new ArgumentNullException(nameof(_provider));
this._channels = new Dictionary<string, IModel>();
this._consumers = new List<IMvpRabbitMQConsumer>();
}
#endregion

Expand Down Expand Up @@ -130,27 +128,27 @@ public virtual void Consume()
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-consumer-start");

if (!TypeConsumers.AnySafe())
if (!_consumers.AnySafe())
{
throw new ArgumentException("We didn't find consumers registered through the configuration.");
}

if (Connection.Options.DispatchConsumersAsync)
{
if (TypeConsumers.AnySafe(x => x.InheritsOrImplements(typeof(IMvpRabbitMQConsumerSync))))
if (_consumers.AnySafe(x => x.GetType().InheritsOrImplements(typeof(IMvpRabbitMQConsumerSync))))
{
throw new ArgumentException("DispatchConsumersAsync is enabled, so register only classes that implement the IMvpRabbitMQConsumerAsync interface.");
}
}
else
{
if (TypeConsumers.AnySafe(x => x.InheritsOrImplements(typeof(IMvpRabbitMQConsumerAsync))))
if (_consumers.AnySafe(x => x.GetType().InheritsOrImplements(typeof(IMvpRabbitMQConsumerAsync))))
{
throw new ArgumentException("DispatchConsumersAsync is disabled, so register only classes that implement the IMvpRabbitMQConsumerSync interface.");
}
}

foreach (var customer in GetConsumers())
foreach (var customer in _consumers)
{
var channel = CreateConsumerChannel(QueueBind, customer.RoutingKey, customer.QueueName);

Expand Down Expand Up @@ -457,61 +455,73 @@ private IBusinessEvent ExtractBodyToBusinessEvent(BasicDeliverEventArgs e)

#endregion

#region [ Static ]
static List<IMvpRabbitMQConsumer> _consumers;
static List<IMvpRabbitMQConsumer> GetConsumers()
{
if (_consumers == null)
{
var consumersSync = TypeConsumers
.Where(x => x.InheritsOrImplements(typeof(IMvpRabbitMQConsumerSync)))
.Select(t => (IMvpRabbitMQConsumerAsync)Activator.CreateInstance(t))
.ToList();

var consumersAsync = TypeConsumers
.Where(x => x.InheritsOrImplements(typeof(IMvpRabbitMQConsumerAsync)))
.Select(t => (IMvpRabbitMQConsumerAsync)Activator.CreateInstance(t))
.ToList();

_consumers = new List<IMvpRabbitMQConsumer>();
_consumers.AddRange(consumersSync);
_consumers.AddRange(consumersAsync);
}
return _consumers;
}
static readonly List<Type> TypeConsumers = new();

public static void Register<T>() where T : class, IMvpRabbitMQConsumer
#region [ Registrars ]
public void Register<T>() where T : class, IMvpRabbitMQConsumer
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-register");
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-register-generic");
Register(typeof(T));
}
public static void Register(Type consumerType)
public void Register(Type consumerType)
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-register");
if (consumerType == null)
{
throw new ArgumentNullException(nameof(consumerType));
}
TypeConsumers.Add(consumerType);
_consumers = null;
if (consumerType.InheritsOrImplements(typeof(IMvpRabbitMQConsumerSync)))
{
_consumers.Add((IMvpRabbitMQConsumerSync)(_provider.GetService(consumerType) ?? Activator.CreateInstance(consumerType)));
}
else if (consumerType.InheritsOrImplements(typeof(IMvpRabbitMQConsumerAsync)))
{
_consumers.Add((IMvpRabbitMQConsumerAsync)(_provider.GetService(consumerType) ?? Activator.CreateInstance(consumerType)));
}
else if (consumerType.InheritsOrImplements(typeof(IMvpRabbitMQConsumerRecoverySync)))
{
_consumers.Add((IMvpRabbitMQConsumerRecoverySync)(_provider.GetService(consumerType) ?? Activator.CreateInstance(consumerType)));
}
else if (consumerType.InheritsOrImplements(typeof(IMvpRabbitMQConsumerRecoveryAsync)))
{
_consumers.Add((IMvpRabbitMQConsumerRecoveryAsync)(_provider.GetService(consumerType) ?? Activator.CreateInstance(consumerType)));
}
else
{
throw new ArgumentException("Invalid type for consumers.");
}
}
public static void Unregister<T>() where T : class, IMvpRabbitMQConsumer
public void Register(IMvpRabbitMQConsumer consumer)
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-unregister");
TypeConsumers
.FindAll(x => x.InheritsOrImplements(typeof(T)))
?.ForEach(item => TypeConsumers.Remove(item));
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-register");
if (consumer == null)
{
throw new ArgumentNullException(nameof(consumer));
}
_consumers.Add(consumer);
}
public static void Unregister(Type consumerType)
public void Unregister<T>() where T : class, IMvpRabbitMQConsumer
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-unregister-generic");
Unregister(typeof(T));
}
public void Unregister(Type consumerType)
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-unregister");
if (consumerType == null)
{
throw new ArgumentNullException(nameof(consumerType));
}
TypeConsumers.Remove(consumerType);
_consumers = null;
_consumers
.FindAll(x => x.GetType().InheritsOrImplements(consumerType))
.ForEach(item => Unregister(item));
}
public void Unregister(IMvpRabbitMQConsumer consumer)
{
TelemetryHelper.Execute(TelemetryLevels.Verbose, "rabbitmq-client-consumer-unregister");
if (consumer == null)
{
throw new ArgumentNullException(nameof(consumer));
}
_consumers.Remove(consumer);
}
#endregion
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
6 changes: 3 additions & 3 deletions src/Mvp24Hours.WebAPI/Mvp24Hours.WebAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
<Company>Kallebe Lins</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<Version>3.12.262</Version>
<AssemblyVersion>3.12.26.2</AssemblyVersion>
<FileVersion>3.12.26.2</FileVersion>
<Version>4.1.051</Version>
<AssemblyVersion>4.1.05.1</AssemblyVersion>
<FileVersion>4.1.05.1</FileVersion>
<RepositoryUrl>https://github.com/kallebelins/mvp24hours-netcore</RepositoryUrl>
</PropertyGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace Mvp24Hours.Application.RabbitMQ.Test.Support.Consumers
{
public class CustomerConsumer : IMvpRabbitMQConsumerAsync
{
public string RoutingKey => typeof(CustomerEvent).Name;
public string RoutingKey => typeof(CustomerConsumer).Name;

public string QueueName => typeof(CustomerEvent).Name;
public string QueueName => typeof(CustomerConsumer).Name;

public async Task ReceivedAsync(object message, string token)
{
Expand Down
Loading

0 comments on commit f044d76

Please sign in to comment.