简体   繁体   English

使用dotnet核心2.1的AWS Lambda函数中的依赖注入

[英]Dependency Injection in AWS Lambda function using dotnet core 2.1

I am new to Aws Lambda and trying to figure out how to use Dependency Injection into Aws Lambda using .net core 2.1. 我是Aws Lambda的新手,并试图找出如何使用.net core 2.1将依赖注入用于Aws Lambda。

I am trying to inject IHttpClientFactory , but I am not sure if I am doing it correctly. 我正在尝试注入IHttpClientFactory ,但我不确定我是否正确执行此操作。

I am calling below method in the constructor of the lambda function class: 我在lambda函数类的构造函数中调用下面的方法:

  private static IServiceProvider ConfigureServices()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddHttpClient("client", client =>
        {
            client.BaseAddress = new Uri("someurl");
        });

       return serviceCollection.BuildServiceProvider();
    }

Is this correct? 它是否正确?

Also, after it returns IServiceProvider , how do I use it in any class where I need to call IHttpClientFactory ? 此外,在它返回IServiceProvider ,如何在我需要调用IHttpClientFactory任何类中使用它?

(I have gone through some related articles, but I am still unclear to use the output from ConfigureServices() method when called in the constructor?) (我已经阅读了一些相关文章,但在构造函数中调用时,我仍然不清楚使用ConfigureServices()方法的输出?)

Thanks. 谢谢。

Example of usage for DI: DI的使用示例:

public class Function
{
   private readonly ITestClass _test;
   public Function()
    {
       ConfigureServices();
    }

    public async Task Handler(ILambdaContext context)
    {
       _test.Run(); //Run method from TestClass that implements ITestClass and calls IHttpClientFactory to make call to an API

      //return something
    }

    private static void ConfigureServices()
    {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddHttpClient("client", client =>
        {
            client.BaseAddress = new Uri("someurl");
        });
       serviceCollection.AddTransient<ITestClass, TestClass>();
       serviceCollection.BuildServiceProvider(); //is it needed??
    }
}

Assign the service provider as the DI container and use it in your functions 将服务提供者指定为DI容器并在您的功能中使用它

Function.cs Function.cs

public class Function {

    public static Func<IServiceProvider> ConfigureServices = () => {
        var serviceCollection = new ServiceCollection();
        serviceCollection.AddHttpClient("client", client =>
        {
            client.BaseAddress = new Uri("someurl");
        });
        serviceCollection.AddTransient<ITestClass, TestClass>();
        return serviceCollection.BuildServiceProvider();
    };

    static IServiceProvider services;
    static Function() {
        services = ConfigureServices();
    }


    public async Task Handler(ILambdaContext context) {
        ITestClass test = services.GetService<ITestClass>();
        await test.RunAsync(); 

        //...
    }
}

Using a static constructor for a one time call to configure your services and build the service container. 使用静态构造函数进行一次调用以配置服务并构建服务容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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