LayeredCraft.Lambda.AspNetCore.HostingExtensions 1.0.0.4

dotnet add package LayeredCraft.Lambda.AspNetCore.HostingExtensions --version 1.0.0.4
                    
NuGet\Install-Package LayeredCraft.Lambda.AspNetCore.HostingExtensions -Version 1.0.0.4
                    
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="LayeredCraft.Lambda.AspNetCore.HostingExtensions" Version="1.0.0.4" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="LayeredCraft.Lambda.AspNetCore.HostingExtensions" Version="1.0.0.4" />
                    
Directory.Packages.props
<PackageReference Include="LayeredCraft.Lambda.AspNetCore.HostingExtensions" />
                    
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 LayeredCraft.Lambda.AspNetCore.HostingExtensions --version 1.0.0.4
                    
#r "nuget: LayeredCraft.Lambda.AspNetCore.HostingExtensions, 1.0.0.4"
                    
#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 LayeredCraft.Lambda.AspNetCore.HostingExtensions@1.0.0.4
                    
#: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=LayeredCraft.Lambda.AspNetCore.HostingExtensions&version=1.0.0.4
                    
Install as a Cake Addin
#tool nuget:?package=LayeredCraft.Lambda.AspNetCore.HostingExtensions&version=1.0.0.4
                    
Install as a Cake Tool

LayeredCraft Lambda ASP.NET Core Hosting Extensions

Build Status NuGet Downloads

Extensions and middleware for Amazon.Lambda.AspNetCoreServer.Hosting. Provides ASP.NET Core components designed specifically for AWS Lambda hosting, delivering improved reliability, observability, and developer experience.

Features

  • โฑ๏ธ Lambda Timeout Handling: Intelligent timeout middleware that links Lambda execution limits with HTTP request cancellation
  • ๐Ÿ”„ Graceful Shutdown: Proper handling of approaching Lambda timeouts with configurable safety buffers
  • ๐Ÿ› ๏ธ Developer Experience: Standard CancellationToken patterns work seamlessly in Lambda environments
  • ๐Ÿงช Local Development: Pass-through behavior when running outside Lambda (Kestrel, IIS Express)
  • ๐Ÿ“Š Observability: Structured logging with detailed timeout and cancellation telemetry

Installation

dotnet add package LayeredCraft.Lambda.AspNetCore.HostingExtensions

Quick Start

Basic Timeout Middleware

using LayeredCraft.Lambda.AspNetCore.Hosting.Extensions;

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Add Lambda timeout-aware cancellation early in the pipeline
    app.UseLambdaTimeoutLinkedCancellation();
    
    // Your other middleware
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

With Custom Safety Buffer

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Custom safety buffer of 500ms for cleanup operations
    app.UseLambdaTimeoutLinkedCancellation(TimeSpan.FromMilliseconds(500));
    
    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });
}

Using Cancellation in Controllers

[ApiController]
[Route("api/[controller]")]
public class DataController : ControllerBase
{
    private readonly IDataService _dataService;

    public DataController(IDataService dataService)
    {
        _dataService = dataService;
    }

    [HttpGet]
    public async Task<IActionResult> GetData(CancellationToken cancellationToken)
    {
        try
        {
            // This will be cancelled if Lambda timeout approaches
            var data = await _dataService.GetDataAsync(cancellationToken);
            return Ok(data);
        }
        catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
        {
            // Lambda timeout occurred - return appropriate response
            return StatusCode(504, "Request timeout");
        }
    }
}

Documentation

๐Ÿ“– Complete Documentation

How It Works

Lambda Timeout Linking

The LambdaTimeoutLinkMiddleware creates a sophisticated cancellation token that triggers when either:

  1. Client disconnects or the server aborts the request (standard ASP.NET Core behavior)
  2. Lambda timeout approaches (calculated from ILambdaContext.RemainingTime with safety buffer)

The middleware replaces HttpContext.RequestAborted with this linked token, enabling downstream code to respond to Lambda timeouts through standard CancellationToken patterns.

Safety Buffer

The configurable safety buffer (default: 250ms) ensures your application has time to:

  • Complete cleanup operations
  • Write final log entries
  • Return appropriate HTTP status codes
  • Flush telemetry data

Local Development

When running locally (Kestrel, IIS Express) where ILambdaContext is unavailable, the middleware operates as a pass-through with only standard client disconnect cancellation active.

Status Codes

The middleware automatically sets appropriate HTTP status codes on timeout:

  • 504 Gateway Timeout: Lambda execution timeout occurred
  • 499 Client Closed Request: Client disconnected (non-standard but widely recognized)

Requirements

  • .NET 8.0 or .NET 9.0
  • Amazon.Lambda.AspNetCoreServer 9.2.0+
  • LayeredCraft.StructuredLogging 1.1.1.8+

Contributing

We welcome contributions! Please see our Contributing Guidelines for details.

Development Setup

# Clone the repository
git clone https://github.com/LayeredCraft/lambda-aspnetcore-hosting-extensions.git
cd lambda-aspnetcore-hosting-extensions

# Restore dependencies
dotnet restore

# Build the project
dotnet build

# Run tests
dotnet test

Code Style

  • Follow C# coding conventions
  • Use meaningful names for variables and methods
  • Add XML documentation for public APIs
  • Include unit tests for new features
  • Run tests before submitting PRs

License

This project is licensed under the MIT License.

Support

Changelog

See CHANGELOG.md for details on releases and changes.


Built with โค๏ธ by the LayeredCraft team

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

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.0.4 26 9/15/2025
1.0.0.3 38 9/12/2025
1.0.0.2 26 9/12/2025