Skip to content

refactor(host): migrate BootstrapHttpClient from options to dependency injection #218

Description

@j-d-ha

Description

Currently, the Lambda Bootstrap HttpClient is configured through LambdaHostOptions.BootstrapHttpClient, which is an anti-pattern. HTTP clients should be registered and resolved through dependency injection to leverage proper lifetime management, configuration, and testing.

Current Implementation

File: src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs:14

public HttpClient? BootstrapHttpClient { get; set; } = null;

File: src/AwsLambda.Host/Runtime/LambdaBootstrapAdapter.cs:35-42

using var bootstrap = _settings.BootstrapHttpClient is null
    ? new LambdaBootstrap(wrappedHandler, _settings.BootstrapOptions, convertedInitializer)
    : new LambdaBootstrap(
        _settings.BootstrapHttpClient,
        wrappedHandler,
        _settings.BootstrapOptions,
        convertedInitializer
    );

Proposed Solution

Migrate to dependency injection using one of these approaches:

Option 1: Keyed Services (Recommended)

Register the Lambda Bootstrap HTTP client as a keyed service to distinguish it from other HTTP clients in the DI container:

builder.Services.AddHttpClient("LambdaBootstrap", client => 
{
    // Configure Lambda Runtime API specific settings
});

Then inject via:

IHttpClientFactory httpClientFactory
var bootstrapClient = httpClientFactory.CreateClient("LambdaBootstrap");

Option 2: Dedicated Interface

Create a wrapper interface like ILambdaBootstrapHttpClient that wraps HttpClient for better type safety:

public interface ILambdaBootstrapHttpClient 
{
    HttpClient Client { get; }
}

Option 3: Named Factory

Use a factory pattern with IHttpClientFactory and register a named client specific to Lambda bootstrap operations.

Benefits

  • ✅ Proper lifetime management
  • ✅ Better testability (mock the factory or keyed service)
  • ✅ Follows .NET dependency injection best practices
  • ✅ Removes configuration object responsibility for managing instances
  • ✅ Enables configuration through IHttpClientBuilder extensions

Breaking Changes

This will require deprecating LambdaHostOptions.BootstrapHttpClient and may require users to migrate their HTTP client configuration to the DI container.

Tasks

  • Choose the DI approach (keyed services, dedicated interface, or named factory)
  • Add HTTP client registration to service collection
  • Update LambdaBootstrapAdapter to accept injected HTTP client
  • Deprecate LambdaHostOptions.BootstrapHttpClient
  • Update documentation in docs/guides/configuration.md
  • Add migration guide for users
  • Update tests

References

  • Current implementation: src/AwsLambda.Host/Runtime/LambdaBootstrapAdapter.cs:35-42
  • Options definition: src/AwsLambda.Host/Core/Options/LambdaHostOptions.cs:14
  • Documentation: docs/guides/configuration.md:89

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions