Friday, 4 October 2019

Add .NET Core DI(Dependency Injection) and Config settings using appsettings.json file to AWS Lambda Functions.

Step 1: Add following nuget packages into your .Net core AWS Lamda project:
1. Microsoft.Extensions.DependencyInjection
2. Microsoft.Extensions.Configuration
3. Microsoft.Extensions.Configuration.FileExtensions
4. Microsoft.Extensions.Configuration.Json

Note: Versions of NuGet packages you install need to match the version of .NET Core supported by AWS Lambda for the project you created.  In this example it is 2.1.0.

Step 2: Add new file named appsettings.json in root of your .Net core AWS Lambda project and Define your keys as bellow:
{
  "TestKeys": {
    "Key1": "value1",
    "Key2": "value2",
   }
}

Step 3: Add new AppSettings.cs file as bellow:
    public class AppSettings
    {
        public string Key1 { get; set; }
        public string Key2 { get; set; }
    }

Step 4: Add new Interface named IConfigurationService.cs as bellow:
    public interface IConfigurationService
    {
        AppSettings GetAppSettings();
    }

Step 5: Add new class named ConfigurationService.cs and implement IConfigurationService.cs interface on it. Here method GetAppSettings() reads the appsettings.json file and fill the data into AppSettings and return.
  public class ConfigurationService : IConfigurationService
    {
        private readonly IConfiguration _configuration;
        public ConfigurationService()
        {
            _configuration = getConfiguration();
        }

        public AppSettings GetAppSettings()
        {
            var appSettings = new AppSettings
            {
                Key1 = _configuration["TestKeys:Key1"],
                Key2 = _configuration["TestKeys:Key2"]
            };

            return appSettings;
        }
        private IConfiguration getConfiguration()
        {
            return new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();
        }
    }

Step 6: Now time to define dependencies of your service classes. Add new class named Startup.cs in root of your project.
   public class Startup
    {
        public static void ConfigureServices(IServiceCollection serviceCollection)
        {
            serviceCollection.AddTransient<IConfigurationService, ConfigurationService>();
            serviceCollection.AddTransient<IYourService, YourService>();
        }
    }

Step 7: Set up Dependency Injection in your main class Functions.cs as bellow:
    public class Function
    {
        IYourService _yourService = null;
        public Function()
        {
            var serviceCollection = new ServiceCollection();
            Startup.ConfigureServices(serviceCollection);
            var serviceProvider = serviceCollection.BuildServiceProvider();
            _yourService = serviceProvider.GetService<IYourService>();
        }

        public async Task<string> FunctionHandler(YourModel input, ILambdaContext context)
        {
            var result = await _yourService.Test(input);
            return JsonConvert.SerializeObject(result);
        }
    }

Step 8: Using the appsettings.js keys into service classes.
public class YourService : IYourService
{
private readonly AppSettings _appSettings;
public EmailService(IConfigurationService configurationService)
  {
        _appSettings = configurationService.GetAppSettings();
  }

public AppSettings Test(){
string key1=_appSettings.Key1;
string key2=_appSettings.Key2;
return _appSettings;
}
}

Now you are all set to use dependency injection and appsetings.json keys in your .Net core AWS Lambda function.

No comments:

Post a Comment