简体   繁体   中英

.Net Core - AWS Lambda Project enabling DI

I have a Lambda project in .net core and would like to enable Dependency Injection. I have created a Startup class in which I added ConfigureService and ConfigureContainer

public class Startup
{
        public void ConfigureServices(IServiceCollection services)
        {
            void ConfigureContainer()
            {
                services.AddTransient<IProfileEventHandler, ProfileEventHandler>();
                services.AddTransient<IRepository, ESRepository>();
                services.AddTransient<IDataKeyDecryption, KmsDataKeyDecryption>();
                services.AddTransient<IDecryptionProvider, DecryptionProvider>();
            }

            ConfigureContainer();
        }

}

Usually a typical .net core project have a Program Class and we will invoke the start up class in CreateWebHost method, when we run the WebHost, it will just resolve the dependency. But how can I do the same in AWS Lambda project.

You can actually use Asp.NET Core in Lambdas and this makes for easier web development of course. If you download the dotnet project templates, you can create a project from a template that already has a serverless template as well as the lambda entrypoint, all configured for lambda!

Using this will provide you with the DI and IoC that Asp.Net Core provides out of the box.

If you are using VS you can download the AWS Toolkit for Visual Studio: https://aws.amazon.com/visualstudio/

Alternatively, you can download the templates to be used via the dotnet cli https://aws.amazon.com/blogs/developer/creating-net-core-aws-lambda-projects-without-visual-studio/

You could check out the Serverless .NET example and take the idea from there. It has pretty straightforward implementations:

  1. Create LambdaEntryPoint class inherited from APIGatewayProxyFunction .

  2. Add resource point in serverless.template to this resource.

LambdaEntryPoint.cs:

public class LambdaEntryPoint : Amazon.Lambda.AspNetCoreServer.APIGatewayProxyFunction
{
    protected override void Init(IWebHostBuilder builder)
    {
        builder.UseStartup<Startup>();
    }
}

serverless.template:

"Resources": {
    "AspNetCoreFunction": {
      "Type": "AWS::Serverless::Function",
      "Properties": {
        "Handler": "AWSServerless::AWSServerless.LambdaEntryPoint::FunctionHandlerAsync",
        "Runtime": "dotnetcore3.1",
        "Events": {
          "ProxyResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/{proxy+}",
              "Method": "ANY"
            }
          },
          "RootResource": {
            "Type": "Api",
            "Properties": {
              "Path": "/",
              "Method": "ANY"
            }
          }
        }
      }
    },

Good explanation about .NET Lambda under the hood - https://aws.amazon.com/blogs/developer/one-month-update-to-net-core-3-1-lambda/

Dependency Injection has nothing to do with Lambda. If all in your code on how you handle it.

Take a look at the sample implementation here.

https://docs.aws.amazon.com/lambda/latest/dg/dotnet-programming-model-handler-types.html

Initialises DI code at the entry point of the static method.

Hope it helps.

here an implemented example of what you want (for those who like me do not feel lost)

https://github.com/Weyne/AWS-NET_CORE-NLOG/tree/main/WeyneLoggingWithNLog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM