Last active
June 13, 2021 08:23
-
-
Save jiimaho/d0976c4791da00308923514f8926bfe9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Function | |
{ | |
private static ServiceProvider ServiceProvider { get; set; } | |
/// <summary> | |
/// The parameterless constructor is what Lambda uses to construct your instance the first time. | |
/// It will only ever be called once for the lifetime of the container that it's running on. | |
/// We want to build our ServiceProvider once, and then use the same provider in all subsequent | |
/// Lambda invocations. This makes things like using local MemoryCache techniques viable (Just | |
/// remember that you can never count on a locally cached item to be there!) | |
/// </summary> | |
public Function() | |
{ | |
var services = new ServiceCollection(); | |
ConfigureServices(services); | |
ServiceProvider = services.BuildServiceProvider(); | |
} | |
public async Task FunctionHandler(SQSEvent evnt, ILambdaContext context) | |
{ | |
await ServiceProvider.GetService<App>().Run(evnt); | |
} | |
/// <summary> | |
/// Configure whatever dependency injection you like here | |
/// </summary> | |
/// <param name="services"></param> | |
private static void ConfigureServices(IServiceCollection services) | |
{ | |
// add dependencies here ex: Logging, IMemoryCache, Interface mapping to concrete class, etc... | |
// add a hook to your class that will actually do the application logic | |
services.AddTransient<App>(); | |
} | |
/// <summary> | |
/// Since we don't want to dispose of the ServiceProvider in the FunctionHandler, we will | |
/// at least try to clean up after ourselves in the destructor for the class. | |
/// </summary> | |
~Function() | |
{ | |
ServiceProvider.Dispose(); | |
} | |
} | |
// Class copied from: https://stackoverflow.com/a/54166289/2874896 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment