implement message subscribing and worker

This commit is contained in:
2026-02-14 19:17:05 +01:00
parent 4cdfa1e096
commit 0dadaf1280
19 changed files with 376 additions and 11 deletions

View File

@@ -0,0 +1,48 @@
using System.Reflection;
using AipsCore.Application.Abstract.MessageBroking;
namespace AipsWorker.Utilities;
public class SubscribeMethodUtility
{
private readonly IMessageSubscriber _subscriber;
public SubscribeMethodUtility(IMessageSubscriber subscriber)
{
_subscriber = subscriber;
}
public async Task SubscribeToMessageTypeAsync(
Type messageType,
object targetInstance,
MethodInfo handlerMethod)
{
var subscribeMethod = GetGenericSubscribeMethod(messageType);
var handlerDelegate = CreateHandlerDelegate(messageType, targetInstance, handlerMethod);
var task = (Task)subscribeMethod.Invoke(
_subscriber,
new object[] { handlerDelegate })!;
await task;
}
private MethodInfo GetGenericSubscribeMethod(Type messageType)
{
var method = typeof(IMessageSubscriber)
.GetMethod(nameof(IMessageSubscriber.SubscribeAsync))!;
return method.MakeGenericMethod(messageType);
}
private Delegate CreateHandlerDelegate(
Type messageType,
object targetInstance,
MethodInfo handlerMethod)
{
var delegateType = typeof(Func<,,>)
.MakeGenericType(messageType, typeof(CancellationToken), typeof(Task));
return Delegate.CreateDelegate(delegateType, targetInstance, handlerMethod);
}
}