I am new in AWS lambda, I am trying to implement Dependency Injection in lambda function as suggested by link https://garywoodfine.com/dependency-injection-in-aws-lambda/
After implement as per above link it works properly for the "AddTransient" type but not work for the "AddScoped" if I register interface and class as AddTransient, it return new GUID in each time when lambda function invoke with same lambda instance before it goes to down/kill.
While I register same interface and class as AddScoped, even though it return same previous GUID in each request (menas lambda function invoke) with same lambda instance before it goes to down/kill. I am expecting same GUID in same request but new GUID in different request.
It seems it work as singleton even registered as scope type.
Sample Code
using Amazon.Lambda.Core;
using AWSLambda1.UseCase;
using Microsoft.Extensions.DependencyInjection;
using System;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace AWSLambda1
{
public class Function
{
ServiceProvider _serviceProvider;
public Function()
{
var serviceCollection = new ServiceCollection();
ConfigureServices(serviceCollection);
_serviceProvider = serviceCollection.BuildServiceProvider();
}
private void ConfigureServices(IServiceCollection serviceCollection)
{
serviceCollection.AddScoped<ITenantInfo, TenantInfo>();
serviceCollection.AddScoped<IService1, Service1>();
serviceCollection.AddScoped<IService2, Service2>();
}
public string FunctionHandler(object input, ILambdaContext context)
{
var tenantInfo= _serviceProvider.GetRequiredService<ITenantInfo>();
var service1 = _serviceProvider.GetRequiredService<IService1>();
var service2 = _serviceProvider.GetRequiredService<IService2>();
//Every time I got same GUID even I set "AddScoped"
//on each time Lambda Trigger before it goes to kill.
/*
First Lambda Trigger at 5:45:40 :
===================================
Service 1 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Service 2 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Second Lambda Trigger at 5:45:45 : (Idealy, it should generate new GUID but it returns previous one)
===================================
Service 1 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Service 2 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Third Lambda Trigger at 5:45:50 : (Idealy, it should generate new GUID but it returns previous one)
===================================
Service 1 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Service 2 GUID ==> b15399ba-5dba-4d92-8c28-b9065d34ef8e
Lambda function goes donw after 5 minutes and generate new GUID after it.
Fourth Lambda Trigger at 5:55:00 : (now, it generate new GUID)
===================================
Service 1 GUID ==> 44edc8f0-0988-43a1-ac51-f4187f5aae04
Service 2 GUID ==> 44edc8f0-0988-43a1-ac51-f4187f5aae04
//My expection is it should be new in first, second and third time lambda function invoke
//as I register class and interface by AddScoped method but it work as singleton.
*/
LambdaLogger.Log("Service 1 GUID==>" + service1.GetTenantInfo().GUID);
LambdaLogger.Log(Environment.NewLine);
LambdaLogger.Log("Service 2 GUID==>" + service2.GetTenantInfo().GUID);
LambdaLogger.Log(Environment.NewLine);
return "Done";
}
}
}