Skip to content

Latest commit

 

History

History
67 lines (50 loc) · 1.53 KB

File metadata and controls

67 lines (50 loc) · 1.53 KB

| EN

IntegrationEventBus

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
  1. 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

  1. Custom IntegrationEvent
public class DemoIntegrationEvent : IntegrationEvent
{
    public override string Topic { get; set; } = nameof(DemoIntegrationEvent);//dapr topic name

    //todo Custom attribute parameters
}
  1. Custom CustomDbContext
public class CustomDbContext : IntegrationEventLogContext
{
    public DbSet<User> Users { get; set; } = null!;

    public CustomDbContext(MasaDbContextOptions<CustomDbContext> options) : base(options)
    {

    }
}
  1. Send Event
IIntegrationEventBus eventBus;//Get IIntegrationEventBus through DI
await eventBus.PublishAsync(new DemoIntegrationEvent());//Send cross-process events
  1. Subscribe to events
[Topic("pubsub", nameof(DomeIntegrationEvent))]
public async Task DomeIntegrationEventHandleAsync(DomeIntegrationEvent @event)
{
    //todo
}