Getting Started with EricksonLopez.Outbox
Install EricksonLopez.Outbox along with a storage backend (such as EricksonLopez.Outbox.Storage.PostgreSql) and broker adapter (3.0.0 on NuGet).
dotnet add package EricksonLopez.Outbox --version 3.0.0<PackageReference Include="EricksonLopez.Outbox" Version="3.0.0" /><PackageVersion Include="EricksonLopez.Outbox" Version="3.0.0" />dotnet add package EricksonLopez.Outbox.Storage.PostgreSql --version 3.0.0dotnet add package EricksonLopez.Outbox.Brokers.RabbitMQ --version 3.0.0dotnet add package EricksonLopez.Outbox.SourceGenerators --version 3.0.0Publication Note:
EricksonLopez.Inbox.PostgreSqlandEricksonLopez.Outbox.OpenTelemetryexist only in local4.2.0HEAD and are not published on NuGet.org.
Quick Start: AddOutbox() with OutboxStartupValidator
Section titled “Quick Start: AddOutbox() with OutboxStartupValidator”AddOutbox() lives in namespace EricksonLopez.Outbox.Hosting; and registers OutboxStartupValidator, which requires ILogger<OutboxStartupValidator>, an IOutboxSerializer, an IOutboxMessageTypeResolver, and an IOutboxRepository:
using System;using System.Text.Json.Serialization;using System.Threading.Tasks;using EricksonLopez.Outbox;using EricksonLopez.Outbox.Contracts;using EricksonLopez.Outbox.Hosting;using EricksonLopez.Outbox.Serialization;using EricksonLopez.Outbox.Storage.PostgreSql;using Microsoft.Extensions.DependencyInjection;
namespace Example.OutboxQuickStart;
[OutboxMessage("orders.placed")]public sealed record OrderPlacedEvent(Guid OrderId, decimal Total);
[JsonSerializable(typeof(OrderPlacedEvent))]public sealed partial class OutboxJsonContext : JsonSerializerContext;
public sealed class ConsoleBrokerPublisher : IBrokerPublisher{ public ValueTask<DispatchResult> PublishRawAsync( OutboxMessage message, OutboxMessageMetadata metadata, DispatchContext context) => ValueTask.FromResult(DispatchResult.Ok());}
public static class OutboxUsage{ public static void Register() { var services = new ServiceCollection(); services.AddLogging(); services.AddOutbox(options => { options.UsePostgreSql("Host=localhost;Database=app;Username=postgres;Password=postgres"); options.UseSerializer(new NativeAotJsonSerializer(OutboxJsonContext.Default)); options.UseTypeResolver(new InMemoryMessageTypeResolver( [ ("orders.placed", typeof(OrderPlacedEvent)), ])); options.UseBroker<ConsoleBrokerPublisher>(); });
using var provider = services.BuildServiceProvider( new ServiceProviderOptions { ValidateOnBuild = true, ValidateScopes = true });
using var scope = provider.CreateScope(); var outbox = scope.ServiceProvider.GetRequiredService<IOutbox>(); if (outbox is null) { throw new InvalidOperationException("IOutbox was not resolved."); } }}