DKNet

Consume published events

Part of DKNet.SlimBus.Extensions. Fluents.EventsConsumers.IHandler<TEvent> is a thin alias over SlimMessageBus’s IConsumer<TEvent> for reacting to a message on the bus. Use the console-project setup in the Quick Start, then replace Program.cs with this example:

using DKNet.SlimBus.Extensions;
using Microsoft.Extensions.DependencyInjection;
using SlimMessageBus;
using SlimMessageBus.Host;
using SlimMessageBus.Host.Memory;

var services = new ServiceCollection();
services.AddLogging();
services.AddSlimMessageBus(mbb => mbb
    .AddServicesFromAssembly(typeof(Program).Assembly)
    .AddChildBus("Memory", m => m.WithProviderMemory().AutoDeclareFrom(typeof(Program).Assembly)));

await using var provider = services.BuildServiceProvider();
var bus = provider.GetRequiredService<IMessageBus>();

await bus.Publish(new ProductCreatedEvent(Guid.NewGuid()));
Console.WriteLine($"ProductCreatedHandler received: {ProductCreatedHandler.LastReceived}");

public sealed record ProductCreatedEvent(Guid ProductId);

public class ProductCreatedHandler : Fluents.EventsConsumers.IHandler<ProductCreatedEvent>
{
    public static Guid? LastReceived;

    public Task OnHandle(ProductCreatedEvent message, CancellationToken cancellationToken)
    {
        LastReceived = message.ProductId;
        return Task.CompletedTask;
    }
}

Run with dotnet run. Prints ProductCreatedHandler received: <guid>.

Publish with IMessageBus.Publish(...) from anywhere that holds an IMessageBus — a handler, a background job, or SlimBusEventPublisher forwarding an EF Core domain event automatically after a save.

![IMessageBus.Publish routes the event by type to the registered Fluents.EventsConsumers.IHandler, whose OnHandle runs and returns no response.](/DKNet/DKNet.SlimBus.Extensions/diagrams/consume-events.svg)

🧱 Where it fits

Publish domain events onto the bus is the usual source of events this handler consumes when the host uses EF Core — SlimBusEventPublisher calls the same IMessageBus.Publish shown above.

⚠️ Gotchas & limits