I am trying to play around a little with MediatR and MassTransit, in fact I’m trying to make a WebAPI developed as a Modular Monolith using a slightly modified version of Clean Architecture (Applicaiton, Domain, Infrastructure, Persistance), my core idea is to use MediatR.INotification as a DomainEvent (in-domain comunication) and MassTransit for Integration events (module to module comunication)
My Setup regarding masstransit and mediatr:
services.AddMassTransit(configure =>
{
configure.SetKebabCaseEndpointNameFormatter();
configure.UsingInMemory((context, cfg) => cfg.ConfigureEndpoints(context));
});
// This correctly registres the service INotificationHandler<AddedRegistryDomainEvent>
services.AddMediatR(cfg =>
{
cfg.RegisterServicesFromAssembly(typeof(Modules.Registry.Application.AssemblyReference).Assembly);
});
//Verified by breakpointing in setup before app.Run()
var x = app.Services.GetServices<INotificationHandler<AddedRegistryDomainEvent>>().ToList();
This is my ICommandHandler.Handle metho
public async Task<RegistryDTO> Handle(AddRegistryCommand request, CancellationToken cancellationToken)
{
string denomination = string.Empty;
if (request.Registry.IsPerson)
{
denomination = $"{request.Registry.FirstName} {request.Registry.LastName}";
}
if (request.Registry.IsCompany)
{
denomination = $"{request.Registry.CompanyName} {request.Registry.LegalName}";
}
await _repository.BeginTransactionAsync(cancellationToken);
var entity = new RegistryEntity()
{
FirstName = request.Registry.FirstName,
LastName = request.Registry.LastName,
CompanyName = request.Registry.CompanyName,
LegalName = request.Registry.LegalName,
Denomination = denomination,
IsPerson = request.Registry.IsPerson,
IsCompany = request.Registry.IsCompany,
IsCustomer = request.Registry.IsCustomer,
IsSupplier = request.Registry.IsSupplier,
IsNetwork = request.Registry.IsNetwork
};
await _repository.AddAsync(entity, cancellationToken);
await _repository.SaveChangesAsync(cancellationToken);
await _repository.CommitAsync(cancellationToken);
var dto = new RegistryDTO(entity.Id, entity.FirstName, entity.LastName, entity.CompanyName, entity.LegalName, entity.Denomination, entity.IsPerson, entity.IsCompany, entity.IsCustomer, entity.IsSupplier, entity.IsNetwork);
await _mediatR.Publish(new AddedRegistryDomainEvent() { Id = dto.Id.Value, Denomination = dto.Denomination }, CancellationToken.None);
return dto;
}
And this is my event handler
public class AddedRegistryEventHandler : INotificationHandler<AddedRegistryDomainEvent>
{
public async Task Handle(AddedRegistryDomainEvent notification, CancellationToken cancellationToken)
{
_log.LogInformation("This is the domain event with GUID: {EventId}", notification.EventId);
_log.LogInformation("Registry entity successfully added with Id: {Id}", notification.Id);
var @event = new AddedRegistryIntegrationEvent()
{
Id = notification.Id,
Denomination = notification.Denomination
};
await _bus.Send(@event, cancellationToken);
}
}
When i go to breakpoint inside it is never hit and it actually never logs anything from inside the eventhandler the only log i get is when it sends the command to add a Registry