You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Amazon.Lambda.Annotations: allow an alternate IServiceProvider implementation #1729
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());
}
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.
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.
Activity
ashishdhingra commentedon Apr 15, 2024
Appears to be a useful feature request. Needs review with the team.
normj commentedon Apr 19, 2024
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.
werebear73 commentedon Apr 30, 2024
Upvote
ItsSmiffy commentedon May 9, 2024
Would also like to use it with SimpleInjector. Upvote.
chrischappell-rgare commentedon Dec 2, 2025
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.
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.