中 | EN
Example:
Install-Package MASA.Contrib.Dispatcher.IntegrationEvents.Dapr //Send cross-process messages
Install-Package MASA.Contrib.Dispatcher.IntegrationEvents.EventLogs.EF //Record cross-process message logs
Install-Package MASA.Contrib.Data.UoW.EF //Use UnitOfWork
- Add IIntegrationEventBus
builder.Services
.AddDaprEventBus<IntegrationEventLogService>(options=>
{
options.UseUoW<CatalogDbContext>(dbOptions => dbOptions.UseSqlServer("server=localhost;uid=sa;pwd=P@ssw0rd;database=identity"))
.UseEventLog<CatalogDbContext>();
)
});
CustomerDbContext needs to inherit IntegrationEventLogContext
- Custom IntegrationEvent
public class DemoIntegrationEvent : IntegrationEvent
{
public override string Topic { get; set; } = nameof(DemoIntegrationEvent);//dapr topic name
//todo Custom attribute parameters
}
- Custom CustomDbContext
public class CustomDbContext : IntegrationEventLogContext
{
public DbSet<User> Users { get; set; } = null!;
public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options)
{
}
}
- Send Event
IIntegrationEventBus eventBus;//Get IIntegrationEventBus through DI
await eventBus.PublishAsync(new DemoIntegrationEvent());//Send cross-process events
- Subscribe to events
[Topic("pubsub", nameof(DomeIntegrationEvent))]
public async Task DomeIntegrationEventHandleAsync(DomeIntegrationEvent @event)
{
//todo
}