This packages focuses on predicting the flows needed to use idempotency.
Classes and interfaces required to use idempotency.
- Interface IIdempotencyKeyReader
Interface responsible for identifying and reading the Request idempotence key.
- Class IdempotencyKeyReader
Class with default implementation to obtain idempotency key. It uses the endpoint address, method, and key used in the Request. This way, it does not allow duplication by address, method and key.
public string Read(HttpRequest request)
{
return $"[{request.Method}] {request.Path} - {request.Headers[IDEMPOTENCY_KEY_NAME]}";
}
- Interface IIdempotencyRepository
Interface responsible for save and retrieve key and value from some storage.
- Class IdempotencyRegister
Class relating to the registration of idempotency, it contains the key used, the status code returned in the operation and the response serialized JSON body.
- Class IdempotencyMiddleware
Middleware responsible for inserting idempotency logic into the request flows of WebAPI applications.
- .Net Cli
dotnet add package Idempotency.Core
- Package Manager
Install-Package Idempotency.Core
In the Startup.cs file, add the following dependencies as scoped:
- IIdempotencyKeyReader and implementation;
- IIdempotencyRepository and implementation.
services.AddScoped<IIdempotencyKeyReader, IdempotencyKeyReader>();
services.AddScoped<IIdempotencyRepository, IdempotencyRepository>();
And the middleware IdempotencyMiddleware.
app.UseMiddleware<IdempotencyMiddleware>();
Class and extensions to use Redis as registers storage.
- Class IdempotencyRepository
IIdempotencyRepository implementation that uses Redis as database.
- Extensions Extensions
Extensions to add Services and Middleware to WebAPI.
- .Net Cli
dotnet add package Idempotency.Redis
- Package Manager
Install-Package Idempotency.Redis
To add services:
services.AddRedisIdempotency(Configuration);
To use middleware:
app.UseIdempotency();