From 966f49d03a8759530ac43496693dea05b8ee98db Mon Sep 17 00:00:00 2001 From: Tim Trevor Date: Thu, 2 Mar 2023 15:03:33 -0800 Subject: [PATCH 1/4] Update blank-csharp README.md to require .NET core SDK 6.0 --- sample-apps/blank-csharp/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sample-apps/blank-csharp/README.md b/sample-apps/blank-csharp/README.md index 230e901d..6a4687cc 100644 --- a/sample-apps/blank-csharp/README.md +++ b/sample-apps/blank-csharp/README.md @@ -11,7 +11,7 @@ The project source includes function code and supporting resources: Use the following instructions to deploy the sample application. For more information on the application's architecture and implementation, see [Managing Spot Instance Requests](https://docs.aws.amazon.com/lambda/latest/dg/services-ec2-tutorial.html) in the developer guide. # Requirements -- [.NET Core SDK 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1) +- [.NET Core SDK 6.0](https://dotnet.microsoft.com/download/dotnet-core/6.0) - [AWS extensions for .NET CLI](https://github.com/aws/aws-extensions-for-dotnet-cli) - The Bash shell. For Linux and macOS, this is included by default. In Windows 10, you can install the [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) to get a Windows-integrated version of Ubuntu and Bash. - [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) v1.17 or newer. From 17efd29c9e904b09c53d74d61a35dbd04216ffcd Mon Sep 17 00:00:00 2001 From: Tim Trevor Date: Thu, 2 Mar 2023 15:05:48 -0800 Subject: [PATCH 2/4] Update blank-csharp aws-lambda-tools-defaults.json to use net6.0 framework and dotnet6 runtime --- .../src/blank-csharp/aws-lambda-tools-defaults.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sample-apps/blank-csharp/src/blank-csharp/aws-lambda-tools-defaults.json b/sample-apps/blank-csharp/src/blank-csharp/aws-lambda-tools-defaults.json index 9e276d0c..c815dcf9 100644 --- a/sample-apps/blank-csharp/src/blank-csharp/aws-lambda-tools-defaults.json +++ b/sample-apps/blank-csharp/src/blank-csharp/aws-lambda-tools-defaults.json @@ -11,8 +11,8 @@ "profile":"default", "region" : "us-east-2", "configuration" : "Release", - "framework" : "netcoreapp3.1", - "function-runtime":"dotnetcore3.1", + "framework" : "net6.0", + "function-runtime":"dotnet6", "function-memory-size" : 512, "function-timeout" : 30, "function-handler" : "blank-csharp::blankCsharp.Function::FunctionHandler" From 0506af67d4470d1f5ac0f8a634d1956536ea1cae Mon Sep 17 00:00:00 2001 From: Tim Trevor Date: Thu, 2 Mar 2023 15:07:26 -0800 Subject: [PATCH 3/4] Update blank-csharp Function.cs to remove static constructor --- .../blank-csharp/src/blank-csharp/Function.cs | 89 +++++++++++-------- 1 file changed, 51 insertions(+), 38 deletions(-) diff --git a/sample-apps/blank-csharp/src/blank-csharp/Function.cs b/sample-apps/blank-csharp/src/blank-csharp/Function.cs index a97a3199..e3a8db5d 100644 --- a/sample-apps/blank-csharp/src/blank-csharp/Function.cs +++ b/sample-apps/blank-csharp/src/blank-csharp/Function.cs @@ -10,8 +10,7 @@ using Amazon.Lambda.SQSEvents; using Amazon.XRay.Recorder.Core; using Amazon.XRay.Recorder.Handlers.AwsSdk; -using Newtonsoft.Json; -using Newtonsoft.Json.Serialization; +using System.IO; [assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] @@ -19,41 +18,55 @@ namespace blankCsharp { public class Function { - private static AmazonLambdaClient lambdaClient; - - static Function() { - initialize(); - } - - static async void initialize() { - AWSSDKHandler.RegisterXRayForAllServices(); - lambdaClient = new AmazonLambdaClient(); - await callLambda(); - } - - public async Task FunctionHandler(SQSEvent invocationEvent, ILambdaContext context) - { - GetAccountSettingsResponse accountSettings; - try - { - accountSettings = await callLambda(); - } - catch (AmazonLambdaException ex) - { - throw ex; - } - AccountUsage accountUsage = accountSettings.AccountUsage; - LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables())); - LambdaLogger.Log("CONTEXT: " + JsonConvert.SerializeObject(context)); - LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(invocationEvent)); - return accountUsage; - } - - public static async Task callLambda() - { - var request = new GetAccountSettingsRequest(); - var response = await lambdaClient.GetAccountSettingsAsync(request); - return response; - } + private AmazonLambdaClient lambdaClient; + + public Function() + { + initialize(); + } + + async void initialize() + { + AWSSDKHandler.RegisterXRayForAllServices(); + lambdaClient = new AmazonLambdaClient(); + await callLambda(); + } + + public async Task FunctionHandler(SQSEvent invocationEvent, ILambdaContext context) + { + GetAccountSettingsResponse accountSettings; + try + { + accountSettings = await callLambda(); + } + catch (AmazonLambdaException ex) + { + throw ex; + } + + AccountUsage accountUsage = accountSettings.AccountUsage; + MemoryStream logData = new MemoryStream(); + StreamReader logDataReader = new StreamReader(logData); + + Amazon.Lambda.Serialization.Json.JsonSerializer serializer = new Amazon.Lambda.Serialization.Json.JsonSerializer(); + + serializer.Serialize(System.Environment.GetEnvironmentVariables(), logData); + LambdaLogger.Log("ENVIRONMENT VARIABLES: " + logDataReader.ReadLine()); + logData.Position = 0; + serializer.Serialize(context, logData); + LambdaLogger.Log("CONTEXT: " + logDataReader.ReadLine()); + logData.Position = 0; + serializer.Serialize(invocationEvent, logData); + LambdaLogger.Log("EVENT: " + logDataReader.ReadLine()); + + return accountUsage; + } + + public async Task callLambda() + { + var request = new GetAccountSettingsRequest(); + var response = await lambdaClient.GetAccountSettingsAsync(request); + return response; + } } } From 5efd96da596e29d31851a9765d7cbf893124abb6 Mon Sep 17 00:00:00 2001 From: Tim Trevor Date: Thu, 2 Mar 2023 15:10:05 -0800 Subject: [PATCH 4/4] Update blank-csharp.csproj to include latest library versions and target net6.0 framework --- .../src/blank-csharp/blank-csharp.csproj | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/sample-apps/blank-csharp/src/blank-csharp/blank-csharp.csproj b/sample-apps/blank-csharp/src/blank-csharp/blank-csharp.csproj index 4cbb52cc..aa648b33 100644 --- a/sample-apps/blank-csharp/src/blank-csharp/blank-csharp.csproj +++ b/sample-apps/blank-csharp/src/blank-csharp/blank-csharp.csproj @@ -1,17 +1,16 @@ - netcoreapp3.1 + net6.0 true Lambda - - - - - - - - + + + + + + + \ No newline at end of file