implement message subscribing and worker
This commit is contained in:
@@ -20,7 +20,15 @@ public static class AipsRegistrationExtensions
|
||||
|
||||
services.AddSingleton<IRabbitMqConnection, RabbitMqConnection>();
|
||||
services.AddSingleton<IMessagePublisher, RabbitMqPublisher>();
|
||||
services.AddSingleton<IMessageSubscriber, RabbitMqSubscriber>();
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddAipsMessageHandlers(this IServiceCollection services)
|
||||
{
|
||||
services.AddMessageHandlersFromAssembly(typeof(IMessage).Assembly);
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
using System.Reflection;
|
||||
using AipsCore.Application.Abstract.MessageBroking;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace AipsCore.Infrastructure.DI;
|
||||
|
||||
public static class MessageHandlerRegistrationExtensions
|
||||
{
|
||||
public static IServiceCollection AddMessageHandlersFromAssembly(this IServiceCollection services, Assembly assembly)
|
||||
{
|
||||
var handlerInterface = typeof(IMessageHandler<>);
|
||||
|
||||
var types = assembly.GetTypes()
|
||||
.Where(t => t is { IsAbstract: false, IsInterface: false });
|
||||
|
||||
foreach (var type in types)
|
||||
{
|
||||
var interfaces = type.GetInterfaces();
|
||||
|
||||
foreach (var @interface in interfaces)
|
||||
{
|
||||
if (!@interface.IsGenericType)
|
||||
continue;
|
||||
|
||||
var genericDef = @interface.GetGenericTypeDefinition();
|
||||
|
||||
if (handlerInterface != genericDef)
|
||||
continue;
|
||||
|
||||
services.AddTransient(@interface, type);
|
||||
}
|
||||
}
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user