Add dependency injection and settings
This commit is contained in:
parent
998266798a
commit
c75cb84abb
9 changed files with 99 additions and 24 deletions
|
@ -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
|
||||
|
|
22
src/lambda-dotnet-console/App.cs
Normal file
22
src/lambda-dotnet-console/App.cs
Normal file
|
@ -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()}";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,12 +1,16 @@
|
|||
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
|
||||
{
|
||||
|
@ -14,14 +18,34 @@ namespace lambda_dotnet_console
|
|||
{
|
||||
|
||||
/// <summary>
|
||||
/// A simple function that takes a string and does a ToUpper
|
||||
/// Lamda Function
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <param name="context"></param>
|
||||
/// <returns></returns>
|
||||
/// <returns>string</returns>
|
||||
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>(appSettings);
|
||||
|
||||
var serviceProvider = serviceCollection.BuildServiceProvider();
|
||||
return serviceProvider.GetService<App>().Run(input);
|
||||
}
|
||||
|
||||
private static void ConfigureServices(IServiceCollection serviceCollection)
|
||||
{
|
||||
serviceCollection.AddTransient<App>();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
7
src/lambda-dotnet-console/Settings/AppSettings.cs
Normal file
7
src/lambda-dotnet-console/Settings/AppSettings.cs
Normal file
|
@ -0,0 +1,7 @@
|
|||
namespace lambda_dotnet_console
|
||||
{
|
||||
public class AppSettings
|
||||
{
|
||||
public string Prefix { get; set; }
|
||||
}
|
||||
}
|
5
src/lambda-dotnet-console/appsettings.json
Normal file
5
src/lambda-dotnet-console/appsettings.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"App": {
|
||||
"Prefix": "Test"
|
||||
}
|
||||
}
|
|
@ -1,14 +1,21 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp2.1</TargetFramework>
|
||||
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
|
||||
<AWSProjectType>Lambda</AWSProjectType>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0" />
|
||||
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0" />
|
||||
<PackageReference Include="Amazon.Lambda.Core" Version="1.0.0"/>
|
||||
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.4.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.CommandLine" Version="2.2.0"/>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="2.2.0"/>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
5
test/lambda-dotnet-console.Tests/appsettings.json
Normal file
5
test/lambda-dotnet-console.Tests/appsettings.json
Normal file
|
@ -0,0 +1,5 @@
|
|||
{
|
||||
"App": {
|
||||
"Prefix": "Test"
|
||||
}
|
||||
}
|
|
@ -17,5 +17,7 @@
|
|||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\src\lambda-dotnet-console\lambda-dotnet-console.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Include="appsettings.json" CopyToPublishDirectory="Always" CopyToOutputDirectory="PreserveNewest"/>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
|
Loading…
Reference in a new issue