Ir al contenido

Buscar en el Ecosistema EricksonLopez.*

Escribe el nombre de una librería, ID de paquete NuGet, tipo .NET o concepto arquitectónico para buscar en el portal.

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
Ventana de terminal
dotnet add package EricksonLopez.Outbox.Storage.PostgreSql --version 3.0.0
dotnet add package EricksonLopez.Outbox.Brokers.RabbitMQ --version 3.0.0
dotnet add package EricksonLopez.Outbox.SourceGenerators --version 3.0.0

Nota de Publicación: EricksonLopez.Inbox.PostgreSql y EricksonLopez.Outbox.OpenTelemetry existen únicamente en el workspace local 4.2.0 y no están publicados en NuGet.org.

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.");
}
}
}