Skip to content

Commit

Permalink
Update to .NET Core 3.1 runtime (#14)
Browse files Browse the repository at this point in the history
* Libraries now target .NET Core 3.1 instead of .NET Standard 2.0
* Templates now target .NET Core 3.1
* Templates now use `System.Text.Json` instead of `Newtonsoft.Json` for handling JSON
* Update to README
  • Loading branch information
Kralizek authored Apr 29, 2020
1 parent 520341f commit c576de1
Show file tree
Hide file tree
Showing 36 changed files with 114 additions and 118 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ This is what a minimal Lambda function will look like
```csharp
public class Function : EventFunction<string>
{
protected override void Configure(IConfigurationBuilder builder)
protected override void Configure(IConfigurationBuilder builder)
{

}

protected override void ConfigureLogging(ILoggerFactory loggerFactory, IExecutionEnvironment executionEnvironment)
protected override void ConfigureLogging(ILoggingBuilder logging, IExecutionEnvironment executionEnvironment)
{

}

protected override void ConfigureServices(IServiceCollection services)
protected override void ConfigureServices(IServiceCollection services, IExecutionEnvironment executionEnvironment)
{
RegisterHandler<EventHandler>(services);
}
}
```

The only method required is `ConfigureServices` that you use to register your handler.
You can use `ConfigureLogging` to add log providers to the provided `LoggerFactory`.
You can use `ConfigureLogging` to add log providers to the provided `ILoggingBuilder`.
`Configure` allows you to customize where your settings are taken from. Very much like ASP.NET Core web applications, you can use environment variables, configuration files and in-memory collections.

# Type of functions and handlers
Expand Down Expand Up @@ -83,8 +83,8 @@ public interface IEventHandler<TInput>

The best way to create a new AWS Lambda that uses this structure is to use the `dotnet new` template provided via NuGet.

1. Ensure you have the [.NET Core SDK 1.0.0+](https://www.microsoft.com/net/download/core) installed.
2. Open your console prompt of choice and type `dotnet new -i "Kralizek.Lambda.Templates::*"`. This will install the latest version of the templates. Depending on your previous usage of `dotnet new`, it might take some time.
1. Ensure you have the latest [.NET Core SDK](https://www.microsoft.com/net/download/core) installed.
2. Open your console prompt of choice and type `dotnet new -i "Kralizek.Lambda.Templates"`. This will install the latest version of the templates. Depending on your previous usage of `dotnet new`, it might take some time.
3. List all available templates by typing `dotnet new -all`. You will see 4 new entries, all starting with Lambda.
4. Create a new project using the template of your choice by typing `dotnet new {short name of the template} -n NameOfYourProject`. E.g. `dotnet new lambda-template-event-empty -n Sample`
5. Start hacking!
Expand All @@ -110,7 +110,7 @@ The empty templates are created with just the minimum required dependencies.

These include:
* `Amazon.Lambda.Core`
* `Amazon.Lambda.Serialization.Json`
* `Amazon.Lambda.Serialization.SystemTextJson`
* `Kralizek.Lambda.Template`
* `Amazon.Lambda.Tools`

Expand All @@ -124,7 +124,7 @@ The boilerplate templates are an enriched version of the empty templates. They c
Besides the basic dependencies of the empty templates, the boilerplate templates have some extra dependencies.

The extra dependencies are:
* `Amazon.Lambda.Serialization.Json` is used to push logs into AWS CloudWatch
* `Amazon.Lambda.Serialization.SystemTextJson` is used to push logs into AWS CloudWatch
* `Kralizek.Extensions.Logging` contains several helper methods for better logging
* `Microsoft.Extensions.Configuration.EnvironmentVariables` used to load configuration values from environment variables
* `Microsoft.Extensions.Configuration.Json` used to load configuration values from json files
Expand Down
2 changes: 1 addition & 1 deletion appveyor.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
max_jobs: 1

image: Visual Studio 2017
image: Visual Studio 2019

environment:
MYGET_TOKEN:
Expand Down
40 changes: 37 additions & 3 deletions build.cake
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#tool "nuget:?package=ReportGenerator&version=4.0.5"
#tool "nuget:?package=JetBrains.dotCover.CommandLineTools&version=2018.3.1"
#tool "nuget:?package=JetBrains.dotCover.CommandLineTools&version=2019.3.4"
#tool "nuget:?package=GitVersion.CommandLine&version=4.0.0"
#tool "nuget:?package=NuGet.CommandLine&version=4.9.2"

#addin "Cake.FileHelpers"
#load "./build/types.cake"

var target = Argument("Target", "Full");

Expand Down Expand Up @@ -242,4 +241,39 @@ Task("Full")
.IsDependentOn("Pack")
.IsDependentOn("Push");

RunTarget(target);
RunTarget(target);

public class BuildState
{
public VersionInfo Version { get; set; }

public BuildPaths Paths { get; set; }
}

public class BuildPaths
{
public FilePath SolutionFile { get; set; }

public DirectoryPath SolutionFolder => SolutionFile.GetDirectory();

public DirectoryPath TestFolder => SolutionFolder.Combine("tests");

public DirectoryPath OutputFolder => SolutionFolder.Combine("outputs");

public DirectoryPath TestOutputFolder => OutputFolder.Combine("tests");

public DirectoryPath ReportFolder => TestOutputFolder.Combine("report");

public FilePath DotCoverOutputFile => TestOutputFolder.CombineWithFilePath("coverage.dcvr");

public FilePath DotCoverOutputFileXml => TestOutputFolder.CombineWithFilePath("coverage.xml");

public FilePath OpenCoverResultFile => OutputFolder.CombineWithFilePath("OpenCover.xml");
}

public class VersionInfo
{
public string PackageVersion { get; set; }

public string BuildVersion {get; set; }
}
34 changes: 0 additions & 34 deletions build/types.cake

This file was deleted.

2 changes: 1 addition & 1 deletion samples/EventFunction/EventFunction.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
Expand Down
2 changes: 1 addition & 1 deletion samples/EventFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
using Microsoft.Extensions.Logging;

// 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.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace EventFunction
{
Expand Down
2 changes: 1 addition & 1 deletion samples/RequestResponseFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
using Microsoft.Extensions.Logging;

// 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.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace RequestResponseFunction
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
Expand Down
5 changes: 1 addition & 4 deletions samples/SnsEventFunction/Function.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization;
using Amazon.Lambda.SNSEvents;
using Kralizek.Lambda;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace SnsEventFunction
{
Expand Down
2 changes: 1 addition & 1 deletion samples/SnsEventFunction/SnsEventFunction.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>Kralizek.Lambda.Template.Sns</PackageId>
<PackageTags>aws-lambda-csharp;dotnet-core;aws-lambda;aws;lambda;csharp;sns</PackageTags>
<Description>Extension to Kralizek.Lambda.Template to better support AWS Lambda functions that respond to SNS notifications.</Description>
Expand Down
5 changes: 2 additions & 3 deletions src/Kralizek.Lambda.Template.Sns/SnsEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
using Amazon.Lambda.SNSEvents;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

using System.Text.Json;

namespace Kralizek.Lambda
{
Expand All @@ -29,7 +28,7 @@ public async Task HandleAsync(SNSEvent input, ILambdaContext context)
using (_serviceProvider.CreateScope())
{
var message = record.Sns.Message;
var notification = JsonConvert.DeserializeObject<TNotification>(message);
var notification = JsonSerializer.Deserialize<TNotification>(message);

var handler = _serviceProvider.GetService<INotificationHandler<TNotification>>();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>Kralizek.Lambda.Template.Sqs</PackageId>
<PackageTags>aws-lambda-csharp;dotnet-core;aws-lambda;aws;lambda;csharp;sqs</PackageTags>
<Description>Extension to Kralizek.Lambda.Template to better support AWS Lambda functions that respond to SQS messages.</Description>
Expand Down
4 changes: 2 additions & 2 deletions src/Kralizek.Lambda.Template.Sqs/SqsEventHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Amazon.Lambda.SQSEvents;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text.Json;

namespace Kralizek.Lambda
{
Expand All @@ -26,7 +26,7 @@ public async Task HandleAsync(SQSEvent input, ILambdaContext context)
using (_serviceProvider.CreateScope())
{
var sqsMessage = record.Body;
var message = JsonConvert.DeserializeObject<TMessage>(sqsMessage);
var message = JsonSerializer.Deserialize<TMessage>(sqsMessage);

var handler = _serviceProvider.GetService<IMessageHandler<TMessage>>();

Expand Down
10 changes: 5 additions & 5 deletions src/Kralizek.Lambda.Template/Kralizek.Lambda.Template.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<PackageId>Kralizek.Lambda.Template</PackageId>
<PackageTags>aws-lambda-csharp;dotnet-core;aws-lambda;aws;lambda;csharp</PackageTags>
<Description>A structured template to create AWS Lambda in C#. It supports Logging, Dependency Injection and Configuration like ASP.NET Core projects do.</Description>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.5.0" />
<PackageReference Include="Amazon.Lambda.Serialization.SystemTextJson" Version="2.0.0" />
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.1.1" />
<PackageReference Include="Microsoft.Extensions.Logging" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="3.1.0" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="3.1.0" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Kralizek.Lambda.Template" Version="3.0.0-v3.30" />
<PackageReference Include="Kralizek.Lambda.Template" Version="3.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion templates/content/EmptyEventFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace EmptyEventFunction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"profile": "DefaultProfile",
"region": "DefaultRegion",
"configuration": "Release",
"framework": "netcoreapp2.0",
"framework": "netcoreapp3.1",
"function-name": "EmptyEventFunction",
"function-role": "DefaultRole",
"function-runtime": "dotnetcore2.0",
"function-runtime": "dotnetcore3.1",
"function-memory-size": 128,
"function-timeout": 30,
"function-handler": "EmptyEventFunction::EmptyEventFunction.Function::FunctionHandlerAsync"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
<AWSProjectType>Lambda</AWSProjectType>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Kralizek.Lambda.Template" Version="3.0.0-v3.30" />
<PackageReference Include="Kralizek.Lambda.Template" Version="3.0.1" />
</ItemGroup>

</Project>
2 changes: 1 addition & 1 deletion templates/content/EmptyRequestResponseFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace EmptyRequestResponseFunction
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
"profile": "DefaultProfile",
"region": "DefaultRegion",
"configuration": "Release",
"framework": "netcoreapp2.1",
"framework": "netcoreapp3.1",
"function-name": "EmptyRequestResponseFunction",
"function-role": "DefaultRole",
"function-runtime": "dotnetcore2.1",
"function-runtime": "dotnetcore3.1",
"function-memory-size": 128,
"function-timeout": 30,
"function-handler": "EmptyRequestResponseFunction::EmptyRequestResponseFunction.Function::FunctionHandlerAsync"
Expand Down
2 changes: 1 addition & 1 deletion templates/content/RichEventFunction/Function.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using Microsoft.Extensions.Logging;
using System.IO;

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace RichEventFunction
{
Expand Down
Loading

0 comments on commit c576de1

Please sign in to comment.