镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Amazon.Lambda.Annotations: allow an alternate IServiceProvider implementation #1729

Open

Description

@chrischappell-rgare

Describe the feature

Provide a way of using an inversion of control container other than the default Microsoft.Extensions.DependencyInjection.

Use Case

I would like to be able to use Autofac with Amazon.Lambda.Annotations.

Proposed Solution

A formal api around a Startup class would work, but it looks like a small tweak to the generated code may be sufficient to hook in an alternate IServiceProvider.

The serviceProvider is currently stored in a field defined like this:
private readonly ServiceProvider serviceProvider;

The field is using the concrete type ServiceProvider. If the field type were changed to IServiceProvider that would allow a different IServiceProvider to be used by implementing a BuildServiceProvider extension method in the Lambda project.

public static AutofacServiceProvider BuildServiceProvider(this IServiceCollection services)
{
      var builder = new ContainerBuilder();
      builder.Populate(services);
      return new AutofacServiceProvider(builder.Build());
}

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

AWS .NET SDK and/or Package version used

Amazon.Lambda.Annotations 1.3.0

Targeted .NET Platform

.Net 8

Operating System and version

AmazonLinux

Activity

  1. ashishdhingra commented on Apr 15, 2024

    @ashishdhingra
    Contributor

    Appears to be a useful feature request. Needs review with the team.

  2. added
    p2This is a standard priority issue
    and removed
    needs-triageThis issue or PR still needs to be triaged.
    on Apr 15, 2024
  3. normj commented on Apr 19, 2024

    @normj
    Member

    This is a valid feature request. We will need more upvotes to prioritize this work. If you are interested in contributing a PR to add support I would be happy to review it.

  4. werebear73 commented on Apr 30, 2024

    @werebear73

    Upvote

  5. ItsSmiffy commented on May 9, 2024

    @ItsSmiffy

    Would also like to use it with SimpleInjector. Upvote.

  6. chrischappell-rgare commented on Dec 2, 2025

    @chrischappell-rgare
    Author

    I reviewed the generated code using the new support for ConfigureHostBuilder, hoping that would make this issue obsolete since host builder has a ConfigureContainer method. The issue still exists in the new code.

    SetExecutionEnvironment();
    var startup = new Startup();
    var hostBuilder = startup.ConfigureHostBuilder();
    
    // By default, Lambda function class is added to the service container using the singleton lifetime
    // To use a different lifetime, specify the lifetime in Startup.ConfigureServices(IServiceCollection) method.
    hostBuilder.Services.AddSingleton<Functions>();
    hostBuilder.Services.AddSingleton<Amazon.Lambda.Serialization.SystemTextJson.CamelCaseLambdaJsonSerializer>();
    
    serviceProvider = hostBuilder.Services.BuildServiceProvider();
    
    host = hostBuilder.Build();
    host.RunAsync();
    
    // Intentionally eagerly build Functions and its dependencies.
    // This causes time spent in the Constructor to appear on INIT_REPORTs
    _ = serviceProvider.GetRequiredService<Functions>();
    

    serviceProvider is still concrete type ServiceProvider. The call to BuildServiceProvider is also redundant. hostBuilder.Build builds an IServiceProvider which is available as host.Services. So, the alternate service provider can be setup by hostBuilder but it is never used by the generated code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    annotationsfeature-requestA feature should be added or improved.p2This is a standard priority issuequeued

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions