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

Instala EricksonLopez.Idempotency (2.0.0 en NuGet) junto con EricksonLopez.Idempotency.Testing (o un store persistente como .PostgreSql o .Redis).

dotnet add package EricksonLopez.Idempotency --version 2.0.0
Ventana de terminal
dotnet add package EricksonLopez.Idempotency.Testing --version 2.0.0
dotnet add package EricksonLopez.Idempotency.AspNetCore --version 2.0.0
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using EricksonLopez.Idempotency;
using EricksonLopez.Idempotency.Testing;
using Microsoft.Extensions.DependencyInjection;
namespace Example.IdempotencyDocs;
public static class IdempotencyQuickStart
{
public static async Task<IdempotencyPipelineOutcome> ExecuteIdempotentAsync(CancellationToken ct = default)
{
var services = new ServiceCollection();
services.AddSingleton<IIdempotencyStore>(new InMemoryIdempotencyStore());
services.AddIdempotencyCore(options =>
{
options.DefaultLeaseDuration = TimeSpan.FromSeconds(30);
options.DefaultRetentionDuration = TimeSpan.FromDays(7);
});
await using var sp = services.BuildServiceProvider();
using var scope = sp.CreateScope();
var engine = scope.ServiceProvider.GetRequiredService<IdempotencyEngine>();
var key = IdempotencyKey.Create("pay-req-1001");
var fingerprint = IdempotencyFingerprintHasher.Compute(
operationName: "POST",
scope: "payments.charge",
tenantId: Guid.Empty.ToString("D"),
authenticatedSubject: "user-42",
payloadBytes: Encoding.UTF8.GetBytes("{\"amount\":100}"));
var headers = new Dictionary<string, string[]> { ["Content-Type"] = ["application/json"] };
IdempotencyPipelineResult result = await engine.ExecutePipelineAsync(
tenantId: Guid.Empty,
scope: "payments.charge",
key: key,
fingerprint: fingerprint,
leaseDuration: TimeSpan.FromSeconds(30),
retentionDuration: TimeSpan.FromDays(7),
executionDelegate: _ => Task.FromResult(
new ExecutionResponse(200, headers, Encoding.UTF8.GetBytes("{\"ok\":true}"))),
cancellationToken: ct);
return result.Outcome;
}
}