Primeros Pasos con EricksonLopez.Outbox
Instala EricksonLopez.Outbox junto con un motor de almacenamiento (como EricksonLopez.Outbox.Storage.PostgreSql) y un adaptador de broker (3.0.0 en 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.0Nota de Publicación:
EricksonLopez.Inbox.PostgreSqlyEricksonLopez.Outbox.OpenTelemetryexisten únicamente en el workspace local4.2.0y no están publicados en NuGet.org.
Inicio Rápido: AddOutbox() con OutboxStartupValidator
Sección titulada «Inicio Rápido: AddOutbox() con OutboxStartupValidator»AddOutbox() reside en namespace EricksonLopez.Outbox.Hosting; y registra OutboxStartupValidator, el cual exige ILogger<OutboxStartupValidator>, un IOutboxSerializer, un IOutboxMessageTypeResolver y un 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."); } }}