2

I have a .NET Core API Gateway based project. I want to introduce dependency injection (di) as many of the packages that I need to introduce are based on this pattern, so require the use of IServiceCollection and so on.

The examples I can find online of introducing DI to AWS Lambda focus only on the standard Lambda projects, where the entry point is the Handler function. I'm unsure how to replicate this with my API Gateway project, as it uses a different structure. I have one parameterless constructor of

Public Functions()
{}

And many instances of

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

I'm not clear on how to introduce DI to this project. For instance, take the .NET Core MemCached package which is detailed here https://github.com/cnblogs/EnyimMemcachedCore

I can set up the following:

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;

But _memcachedClientv2 is never assigned to, so it's value will be null. I'm not sure how to get from the above, to a working _memcachedClientv2 within each APIGatewayProxyRequest method.

JamesMatson
  • 2,522
  • 2
  • 37
  • 86
  • you must set a protocol. config.Protocol = Enyim.Caching.Memcached.MemcachedProtocol.Text; – Nadeem Taj Aug 19 '20 at 04:26
  • Thanks @NadeemTaj but that doesn't really do anything to address the core of the question, which is around setting up for DI – JamesMatson Aug 19 '20 at 10:20
  • you are welcome. please read this link. Not sure but hopeful will help you.https://stackoverflow.com/questions/32459670/resolving-instances-with-asp-net-core-di-from-within-configureservices. How to inject. https://stackoverflow.com/questions/39065458/injecting-dependency-programmatically-asp-net-core – Nadeem Taj Aug 19 '20 at 10:35
  • Hello @James how is it going? – Nadeem Taj Aug 20 '20 at 05:17

1 Answers1

2

You were already pointed in the right direction by Naadem Taj, but here is an example to clarify.

You would want to set up the services in Startup.cs, and after that you have an access to those in other services you create.

Take an example:

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
    }
}

and in your Functions

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
    }
}