2
votes

I have a requirement to expose a Web API using Autofac as the IoC container in the AWS Lambda serverless environment.

The issue is that is seems there is no way to use Autofac as AWS expose the IWebHostBuilder in their preconfigured entry point (the LambdaEntryPoint class): -

protected override void Init(IWebHostBuilder builder)
{
  builder.UseStartup<Startup>();
}

Testing locally works fine as the LocalEntryPoint class looks like this: -

    public class LocalEntryPoint
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args)
        {
            return Host.CreateDefaultBuilder(args)
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                        .UseStartup<Startup>();
                });
        }
    }

Notice the use of IHostBuilder using the methods described here https://autofaccn.readthedocs.io/en/latest/integration/aspnetcore.html#asp-net-core-3-0-and-generic-hosting

Can anyone suggest a way round this?

1
Hooking things in at the generic host level and not at the web host level isn't specific to Autofac. Serilog integration is also at the generic host level, for example. While I don't have the answer, looking for how other systems like that solve the issue may give you some guidance here.Travis Illig

1 Answers

4
votes

Good News!

I raised this with AWS and they responded with their new version of Amazon.Lambda.AspNetCoreServer (v5.1.0) which now includes the Lambda Entry point using IHostBuilder.

https://aws.amazon.com/blogs/developer/one-month-update-to-net-core-3-1-lambda/

I tested it out and it works great with Autofac using the entry point code as follows: -

        protected override void Init(IHostBuilder builder)
        {
            builder
                .UseServiceProviderFactory(new AutofacServiceProviderFactory())
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder
                        .UseStartup<Startup>();
                });
        }