.NET Core
Overview of creating .NET Core functions
Last updated
var envVariable = Environment.GetEnvironmentVariable("myVariableName");public static class AppSettings
{
private static IConfigurationRoot instance;
// Method to get a string from settings
public static string GetString(string key)
{
if (instance == null)
{
// Create an instance with settings
instance = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();
}
// Get value by key
return instance[key];
}
}{
"Parameter1": "First parameter.",
"NestedParams": {
"NestedParam1": "First nested parameter."
}
}// First level parameter
var settings = AppSettings.GetString("Parameter1");
// Second level parameter
var nestedSettings = AppSettings.GetString("NestedParams:NestedParam1");public class Function
{
private readonly IExampleService _exampleService;
// (Required if adding other constructors. Otherwise, optional.) A default constructor
// called by Lambda. If you are adding your custom constructors,
// default constructor with no parameters must be added
public Function() : this (new ExampleService()) {}
// (Optional) An example of injecting a service. As a default constructor is called by Lambda
// this constructor has to be called from default constructor
public Function(IExampleService exampleService)
{
_exampleService = exampleService;
}
public async Task<APIGatewayProxyResponse> Handler(CustomEventRequest<BasicInput> lambdaEvent, ILambdaContext context)
{
...
// - Call example service
var helloWorldMessage = await _exampleService.GetHelloWorld();
...
}
}