Idempwanna 1.0.0

dotnet add package Idempwanna --version 1.0.0
                    
NuGet\Install-Package Idempwanna -Version 1.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Idempwanna" Version="1.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Idempwanna" Version="1.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Idempwanna" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Idempwanna --version 1.0.0
                    
#r "nuget: Idempwanna, 1.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Idempwanna@1.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Idempwanna&version=1.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Idempwanna&version=1.0.0
                    
Install as a Cake Tool

Idempwanna

Idempwanna is an abstract idempotency feature for ASP.NET projects that can be used with different types of caching offerings in the .NET Core framework.

What is Idempotency?

Idempotency is the property of certain operations whereby they can be applied multiple times without changing the result beyond the initial application. In web APIs, this means that making the same request multiple times has the same effect as making it once.

Features

  • Simple API for handling idempotent operations
  • Support for different caching mechanisms:
    • In-memory caching
    • Custom cache implementations
  • Attribute-based approach for marking ASP.NET Core endpoints as idempotent
  • Configurable idempotency key handling (header, query parameter, or custom)
  • Parameter-based idempotency keys using [IdempotentKey] attribute
  • Support for both synchronous and asynchronous operations
  • Thread-safe implementation

Installation

dotnet add package Idempwanna

Basic Usage

1. Register Services

Register the idempotency services in your Program.cs or Startup.cs:

// Using in-memory cache (simplest approach)
builder.Services.AddIdempotency()
    .WithInMemoryCache();

// Or with custom options
builder.Services.AddIdempotency()
    .WithInMemoryCache(options =>
    {
        options.DefaultCacheExpiration = TimeSpan.FromHours(1);
        options.DefaultHeaderName = "x-idempotency-key";
        options.ThrowOnMissingKey = true;
    });

// Or with a custom cache implementation
builder.Services.AddIdempotency()
    .WithCustomCache<YourCustomCacheImplementation>();
    
// Advanced configuration using method chaining
builder.Services.AddIdempotency()
    .WithCustomCache<RedisIdempotencyCache>()
    .WithKeyGenerator<CustomKeyGenerator>()
    .WithService<CustomIdempotencyService>()
    .Configure(options =>
    {
        options.AllowBodyBasedKeys = true;
        options.AllowQueryParameterKeys = true;
    });

2. Mark Endpoints as Idempotent

Use the [Idempotent] attribute on your controller actions:

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    [HttpPost]
    [Idempotent] // This makes the endpoint idempotent
    public async Task<ActionResult<OrderResponse>> CreateOrder(OrderRequest request)
    {
        // Your implementation here
        // The idempotency is handled automatically by the attribute
        return Ok(new OrderResponse { /* ... */ });
    }
}

3. Manual Usage

If you need more control, you can use the IIdempotencyService directly:

[ApiController]
[Route("api/[controller]")]
public class PaymentsController : ControllerBase
{
    private readonly IIdempotencyService _idempotencyService;
    private readonly IIdempotencyKeyGenerator _keyGenerator;
    
    public PaymentsController(
        IIdempotencyService idempotencyService,
        IIdempotencyKeyGenerator keyGenerator)
    {
        _idempotencyService = idempotencyService;
        _keyGenerator = keyGenerator;
    }
    
    [HttpPost]
    public async Task<ActionResult<PaymentResponse>> ProcessPayment(PaymentRequest request)
    {
        // Generate a key from the request
        var idempotencyKey = _keyGenerator.GenerateKey(request);
        
        // Use the idempotency service to process the request
        var response = await _idempotencyService.ProcessAsync<PaymentResponse>(
            idempotencyKey,
            async cancellationToken =>
            {
                // This will only execute if this is not a duplicate request
                PaymentResponse result = await ProcessPaymentLogic(request);
                return result;
            });
            
        return Ok(response);
    }
}

4. Using Parameter-Based Idempotency Keys

You can mark specific parameters to be used as idempotency keys using the [IdempotentKey] attribute:

[ApiController]
[Route("api/[controller]")]
public class OrdersController : ControllerBase
{
    [HttpPost("{requestId}")]
    [Idempotent] 
    public async Task<ActionResult<OrderResponse>> CreateOrder(
        [IdempotentKey] Guid requestId,
        OrderRequest request)
    {
        // The requestId parameter will be used as the idempotency key
        // No need to extract it from headers or generate it from the request body
        return Ok(new OrderResponse { /* ... */ });
    }
}

This is particularly useful for operations where you want to use a client-generated ID as the idempotency key, such as in distributed systems or event-driven architectures.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 is compatible.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Idempwanna:

Package Downloads
Corban.Internal.AspNet

Package Description

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0 359 5/20/2025