Я настроил свой API таким образом:
program.cs < /p>
Код: Выделить всё
using MassTransit;
using Microsoft.EntityFrameworkCore;
using TransactionalOutboxApi.Data;
using TransactionalOutboxApi.Models;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddMassTransit(cfg =>
{
cfg.SetKebabCaseEndpointNameFormatter();
cfg.AddEntityFrameworkOutbox(options =>
{
options.UseSqlServer();
options.UseBusOutbox(o => o.DisableDeliveryService());
});
cfg.UsingRabbitMq((context, cfg) =>
{
cfg.Host("localhost", h =>
{
h.Username("guest");
h.Password("guest");
});
cfg.ConfigureEndpoints(context);
});
});
builder.Services.AddDbContext(options =>
options.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=MassTransit;Integrated Security=True;Connect Timeout=60;"));
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.MapPost("/events", async (ApiRequest dto, AppDbContext dbContext, IPublishEndpoint publish) =>
{
var message = new OutboxMessage { Id = Guid.NewGuid(), Message = dto.Message, CreatedAt = DateTime.UtcNow };
await publish.Publish(message); // Scrive nella Outbox, non invia subito
await dbContext.SaveChangesAsync(); // Commit della transazione
return Results.Ok(new { Message = "Event saved in Outbox" });
});
app.Run();
< /code>
Что мне не ясно, так это почему, чтобы запустить приложение, мне нужно использовать что -то вроде Rabbitmq, хотя мне не нужно отправлять сообщения в Rabbitmq из проекта API. Если я пропущу это, я получаю ошибку, такую как следующая: < /p>
System.AggregateException:
'Some services are not able to be constructed
(Error while validating the service descriptor 'ServiceType:
MassTransit.DependencyInjection
.IScopedBusContextProvider`1[MassTransit.IBus] Lifetime:
Scoped ImplementationType:
MassTransit.EntityFrameworkCoreIntegration
.EntityFrameworkScopedBusContextProvider`2[MassTransit.IBus,
TransactionalOutboxApi.Data.AppDbContext]':
Unable to resolve service for type
'MassTransit.IBus' while attempting to activate 'MassTransit.EntityFrameworkCoreIntegration.
EntityFrameworkScopedBusContextProvider`2[MassTransit.IBus,
TransactionalOutboxApi.Data.AppDbContext]'.)'
Подробнее здесь: https://stackoverflow.com/questions/794 ... nal-outbox