Aws-lambda-dotnet: Question: How to add Autofac to LambdaEntryPoint in net core 3.0?

Created on 7 Apr 2020  路  7Comments  路  Source: aws/aws-lambda-dotnet

Hi,

I am testing out an api project with Amazon.Lambda.AspNetCoreServer and i stumbled upon a problem which i am not sure how to proceed with.
Autofac has changed how its added:
New way: With IHostBuilder .UseServiceProviderFactory(new AutofacServiceProviderFactory()
Old Way: With IWebHostBuilder serviceCollection.AddAutofac()
Also from my understanding IWebHostBuilder will be deprecated which LambdaEntryPoint is using in the Init method.

For the IWebHostBuilder when i try to add Autofac i get this info message from the method:

ONLY FOR PRE-ASP.NET 3.0 HOSTING. THIS WON'T WORK FOR ASP.NET CORE 3.0+ OR GENERIC HOSTING.

And i cant use IHostBuilder with LambdaEntryPoint.

Anyone has done this or has any ideas how to proceed? Thanks in advance!

bug modullambda-client-lib response-requested

Most helpful comment

Yeah I see now the continual use if IWebHostBuilder is a blocker for using Autofac in .NET Core 3.1. Sorry I should have switched to IHostBuilder as part of the major version change that went out with .NET Core 3.1. I was trying to avoid complexity with this library also supporting .NET Core 2.1 which does use IWebHostBuilder.

I don't want to make significant set breaking changes just a couple weeks after the last major version came out so let me think if there is a less invasive approach to unblock you all.

All 7 comments

I am also having issues (.NET Core 3.1) with this and do not have the option of replacing Lambda as the DI provider. My LocalEntryPoint class is simple and shown below, however this seems impossible to wire up in the LambdaEntryPoint class.

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>(); }); 
}

_normj edited formatting of code to make readable._

Yeah I see now the continual use if IWebHostBuilder is a blocker for using Autofac in .NET Core 3.1. Sorry I should have switched to IHostBuilder as part of the major version change that went out with .NET Core 3.1. I was trying to avoid complexity with this library also supporting .NET Core 2.1 which does use IWebHostBuilder.

I don't want to make significant set breaking changes just a couple weeks after the last major version came out so let me think if there is a less invasive approach to unblock you all.

I have created PR https://github.com/aws/aws-lambda-dotnet/pull/633 to switch to IHostBuilder unless the function overrode the CreateWebHostBuilder function.

Version 5.1.0 of Amazon.Lambda.AspNetCoreServer went out today with this change.

I also published a blog post that has a section describing the change.
https://aws.amazon.com/blogs/developer/one-month-update-to-net-core-3-1-lambda/

I am not sure, if this was the cause, but I had a regression between 5.0.0 and 5.1.1.

I made a comment, in another Issue: https://github.com/aws/aws-lambda-dotnet/issues/669#issuecomment-629447122 with the Stacktrace. My project can be found at github. My Entrypoint has this lines, which wil cause the problems (I think):

protected override void Init(IWebHostBuilder webHostBuilder)
        {
            webHostBuilder
                .ConfigureAppConfiguration((context, configurationBuilder) =>
                    {
                        configurationBuilder.Sources.Clear();
                        configurationBuilder.AddEnvironmentVariables();
                    }
                )
                .ConfigureServices(
                    sc =>
                    {
                        sc.TryAddSingleton<ICustomServiceProviderFactory>(new CustomServiceProviderFactory());
                    })
                .ConfigureLogging((hostingContext, logging) =>
                    {

                        logging.AddLambdaLogger(new LambdaLoggerOptions
                        {
                            IncludeLogLevel = true,
                            Filter = (category, logLevel) => true
                        });

                    })
                    .UseStartup<Startup>();
        }

https://github.com/d-velop/dvelop-app-template-cs/blob/bug/5_1_1/AwsLambda/Entrypoint/LambdaEntryPoint.cs

Hi @Xocix,

Please advise if using the latest version resolves the issue.

Thanks,
Ashish

Hey @ashishdhingra sorry for not responding. For me the latest version resolved the issue, thanks everyone involved, i am happy with the result!

Was this page helpful?
0 / 5 - 0 ratings