Do not re-use an instance of ResilienceContext across more than one execution. Retrieve contexts from the pool using ResilienceContextPool.Get(CancellationToken) and return them with ResilienceContextPool.Return(ResilienceContext).
using Polly;// Create a context from the poolvar context = ResilienceContextPool.Shared.Get(cancellationToken);context.OperationKey = "my-operation";try{ // Use the context in pipeline execution await pipeline.ExecuteAsync( static (ctx, state) => { /* your logic */ }, context, state);}finally{ // Always return the context to the pool ResilienceContextPool.Shared.Return(context);}
// Using Outcome for advanced scenariospublic async ValueTask<Outcome<string>> GetDataAsync( ResilienceContext context, CancellationToken ct){ try { var result = await FetchDataAsync(ct); return Outcome.FromResult(result); } catch (Exception ex) { return Outcome.FromException<string>(ex); }}