amazon-web-services - .NET Core API Gateway (AWS) 如何设置依赖注入(inject) (DI) 以使用 MemCached .NET Core 包、基于 DI 的记录器等

标签 amazon-web-services .net-core dependency-injection aws-lambda aws-api-gateway

我有一个基于 .NET Core API 网关的项目。我想引入依赖注入(inject)(di),因为我需要引入的很多包都是基于这种模式的,所以需要使用IServiceCollection等等。

我在网上找到的将 DI 引入 AWS Lambda 的示例仅关注标准 Lambda 项目,其中入口点是 Handler 函数。我不确定如何用我的 API Gateway 项目复制它,因为它使用不同的结构。我有一个无参数构造函数

Public Functions()
{}

以及许多实例

        public async Task<APIGatewayProxyResponse> MyProxyResponse(APIGatewayProxyRequest request, ILambdaContext context)
{
}

我不清楚如何将 DI 引入到这个项目中。例如,采用 .NET Core MemCached 包,详细信息请参见此处 https://github.com/cnblogs/EnyimMemcachedCore

我可以设置以下内容:

public class Functions
{
    public IConfiguration Configuration { get; private set; }

    private void ConfigureServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEnyimMemcached();
        serviceCollection.AddEnyimMemcached(options => options.AddServer(Environment.GetEnvironmentVariable("CACHE_URL"), 
            Convert.ToInt32(Environment.GetEnvironmentVariable("CACHE_PORT"))));
        // TODO: Register services with DI system
    }

    private readonly AmazonSimpleSystemsManagementClient _systemsManagementClient;
    private readonly JSchema _jSchema;
    private readonly loyaltyContext _loyaltyContext;

    private readonly IMemcachedClient _memcachedClientv2;

但是 _memcachedClientv2 从未被分配,因此它的值将为 null。我不确定如何从上面的内容到达每个 APIGatewayProxyRequest 方法中工作的 _memcachedClientv2。

最佳答案

Naadem Taj 已经为您指明了正确的方向,但这里有一个示例可供澄清。

您需要在 Startup.cs 中设置服务,然后您就可以访问您创建的其他服务中的服务。

举个例子:

public class Startup
{
    public IConfiguration Configuration { get; private set; }

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    private void ConfigureServices(IServiceCollection serviceCollection)
    {
        serviceCollection.AddEnyimMemcached(options => 
            options.AddServer(Environment.GetEnvironmentVariable("CACHE_URL"), 
            Convert.ToInt32(Environment.GetEnvironmentVariable("CACHE_PORT"))));
        
        serviceCollection.AddScoped<IFunctions, Functions>();

        // TODO: Register services with DI system
    }
}

在你的函数中

public interface IFunctions
{
    async Task DoStuff();
}

public class Functions : IFunctions
{
    private readonly AmazonSimpleSystemsManagementClient _systemsManagementClient;
    private readonly JSchema _jSchema;
    private readonly loyaltyContext _loyaltyContext;
    private readonly IMemcachedClient _memcachedClientv2;

    public Functions(loyaltyContext context, AmazonSimpleSystemsManagementClient amazonClient, JSchema jschema, IMemcachedClient memcachedClient)
    {
        _loyaltyContext = context;
        _systemsManagementClient= amazonClient;
        _jSchema = jschema;
        _memcachedClientv2 = memcachedClient;
    }

    public async Task DoStuff()
    {
       // Do stuff here
    }
}

关于amazon-web-services - .NET Core API Gateway (AWS) 如何设置依赖注入(inject) (DI) 以使用 MemCached .NET Core 包、基于 DI 的记录器等,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63348509/

相关文章:

mysql - Amazon Athena 将字符串转换为日期

amazon-web-services - GlueJobRunnerSession无权执行: lakeformation:GetDataAccess on resource

amazon-web-services - 通过 DynamoDB 流的 Lambda 触发器延迟

amazon-web-services - 在 AWS Lambda@edge 中获取引荐来源网址

iis - ServiceStack.Redis 在 Windows 服务器的 IIS 上部署时无法连接 : sPort: 0,

c# - 如何防止卫星程序集被发布

c# - 在 Application Insight .Net Core 2.1 中使用自定义属性记录用户名和 IP

jquery - 为什么我会因为 jQuery 而出现 'failed to initiate app' 错误?

java - 多个 dex 文件使用 TMDb 依赖项定义 Ljavax/inject/Inject

java - 记录 Spring bean 创建/依赖注入(inject)