diff --git a/Readme.md b/Readme.md index 0b095c1..1744f0e 100644 --- a/Readme.md +++ b/Readme.md @@ -1,5 +1,7 @@ # AWS Lambda .Net Core Console Example +This is a boilerplate function for those looking to create lambda functions with .Net Core. + This starter project consists of: - Function.cs - class file containing a class with a single function handler method diff --git a/src/lambda-dotnet-console/App.cs b/src/lambda-dotnet-console/App.cs new file mode 100644 index 0000000..f956630 --- /dev/null +++ b/src/lambda-dotnet-console/App.cs @@ -0,0 +1,22 @@ +using System; + +namespace lambda_dotnet_console +{ + public class App + { + private AppSettings _settings; + + public App( + AppSettings settings + ) + { + _settings = settings ?? + throw new ArgumentNullException(nameof(settings)); + } + + public string Run(string input) + { + return $"{_settings.Prefix}{input?.ToUpper()}"; + } + } +} \ No newline at end of file diff --git a/src/lambda-dotnet-console/Function.cs b/src/lambda-dotnet-console/Function.cs index 5f5699d..e6a60ac 100644 --- a/src/lambda-dotnet-console/Function.cs +++ b/src/lambda-dotnet-console/Function.cs @@ -1,27 +1,51 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Threading.Tasks; using Amazon.Lambda.Core; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; + // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class. -[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] +[assembly : LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] namespace lambda_dotnet_console { public class Function { - + /// - /// A simple function that takes a string and does a ToUpper + /// Lamda Function /// /// /// - /// + /// string public string FunctionHandler(string input, ILambdaContext context) { - return input?.ToUpper(); + var configuration = new ConfigurationBuilder() + .SetBasePath(Directory.GetCurrentDirectory()) + .AddJsonFile("appsettings.json", optional : false, reloadOnChange : true) + .AddEnvironmentVariables(prefix: "LAMBDA_") + .Build(); + + var serviceCollection = new ServiceCollection(); + ConfigureServices(serviceCollection); + + var appSettings = new AppSettings(); + configuration.GetSection("App").Bind(appSettings); + serviceCollection.AddSingleton(appSettings); + + var serviceProvider = serviceCollection.BuildServiceProvider(); + return serviceProvider.GetService().Run(input); + } + + private static void ConfigureServices(IServiceCollection serviceCollection) + { + serviceCollection.AddTransient(); } } -} + +} \ No newline at end of file diff --git a/src/lambda-dotnet-console/Settings/AppSettings.cs b/src/lambda-dotnet-console/Settings/AppSettings.cs new file mode 100644 index 0000000..fc0e300 --- /dev/null +++ b/src/lambda-dotnet-console/Settings/AppSettings.cs @@ -0,0 +1,7 @@ +namespace lambda_dotnet_console +{ + public class AppSettings + { + public string Prefix { get; set; } + } +} \ No newline at end of file diff --git a/src/lambda-dotnet-console/appsettings.json b/src/lambda-dotnet-console/appsettings.json new file mode 100644 index 0000000..591872f --- /dev/null +++ b/src/lambda-dotnet-console/appsettings.json @@ -0,0 +1,5 @@ +{ + "App": { + "Prefix": "Test" + } +} diff --git a/src/lambda-dotnet-console/lambda-dotnet-console.csproj b/src/lambda-dotnet-console/lambda-dotnet-console.csproj index 11a3d67..5644179 100644 --- a/src/lambda-dotnet-console/lambda-dotnet-console.csproj +++ b/src/lambda-dotnet-console/lambda-dotnet-console.csproj @@ -1,14 +1,21 @@ - - - - netcoreapp2.1 - true - Lambda - - - - - - - - + + + netcoreapp2.1 + true + Lambda + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/lambda-dotnet-console.Tests/FunctionTest.cs b/test/lambda-dotnet-console.Tests/FunctionTest.cs index 169ff3e..b0a03f4 100644 --- a/test/lambda-dotnet-console.Tests/FunctionTest.cs +++ b/test/lambda-dotnet-console.Tests/FunctionTest.cs @@ -3,12 +3,13 @@ using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; -using Xunit; using Amazon.Lambda.Core; using Amazon.Lambda.TestUtilities; using lambda_dotnet_console; +using Xunit; + namespace lambda_dotnet_console.Tests { public class FunctionTest @@ -22,7 +23,7 @@ namespace lambda_dotnet_console.Tests var context = new TestLambdaContext(); var upperCase = function.FunctionHandler("hello world", context); - Assert.Equal("HELLO WORLD", upperCase); + Assert.Equal("TestHELLO WORLD", upperCase); } } -} +} \ No newline at end of file diff --git a/test/lambda-dotnet-console.Tests/appsettings.json b/test/lambda-dotnet-console.Tests/appsettings.json new file mode 100644 index 0000000..591872f --- /dev/null +++ b/test/lambda-dotnet-console.Tests/appsettings.json @@ -0,0 +1,5 @@ +{ + "App": { + "Prefix": "Test" + } +} diff --git a/test/lambda-dotnet-console.Tests/lambda-dotnet-console.Tests.csproj b/test/lambda-dotnet-console.Tests/lambda-dotnet-console.Tests.csproj index c43cfe0..4cdd943 100644 --- a/test/lambda-dotnet-console.Tests/lambda-dotnet-console.Tests.csproj +++ b/test/lambda-dotnet-console.Tests/lambda-dotnet-console.Tests.csproj @@ -17,5 +17,7 @@ - + + +