Skip to content

Commit afd6c19

Browse files
authored
Merge pull request awsdocs#423 from awsdocs/ldocs2825
Ldocs2825
2 parents 17aede5 + 5efd96d commit afd6c19

File tree

4 files changed

+62
-50
lines changed

4 files changed

+62
-50
lines changed

sample-apps/blank-csharp/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The project source includes function code and supporting resources:
1111
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.
1212

1313
# Requirements
14-
- [.NET Core SDK 3.1](https://dotnet.microsoft.com/download/dotnet-core/3.1)
14+
- [.NET Core SDK 6.0](https://dotnet.microsoft.com/download/dotnet-core/6.0)
1515
- [AWS extensions for .NET CLI](https://github.com/aws/aws-extensions-for-dotnet-cli)
1616
- 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.
1717
- [The AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) v1.17 or newer.

sample-apps/blank-csharp/src/blank-csharp/Function.cs

Lines changed: 51 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,50 +10,63 @@
1010
using Amazon.Lambda.SQSEvents;
1111
using Amazon.XRay.Recorder.Core;
1212
using Amazon.XRay.Recorder.Handlers.AwsSdk;
13-
using Newtonsoft.Json;
14-
using Newtonsoft.Json.Serialization;
13+
using System.IO;
1514

1615
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
1716

1817
namespace blankCsharp
1918
{
2019
public class Function
2120
{
22-
private static AmazonLambdaClient lambdaClient;
23-
24-
static Function() {
25-
initialize();
26-
}
27-
28-
static async void initialize() {
29-
AWSSDKHandler.RegisterXRayForAllServices();
30-
lambdaClient = new AmazonLambdaClient();
31-
await callLambda();
32-
}
33-
34-
public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context)
35-
{
36-
GetAccountSettingsResponse accountSettings;
37-
try
38-
{
39-
accountSettings = await callLambda();
40-
}
41-
catch (AmazonLambdaException ex)
42-
{
43-
throw ex;
44-
}
45-
AccountUsage accountUsage = accountSettings.AccountUsage;
46-
LambdaLogger.Log("ENVIRONMENT VARIABLES: " + JsonConvert.SerializeObject(System.Environment.GetEnvironmentVariables()));
47-
LambdaLogger.Log("CONTEXT: " + JsonConvert.SerializeObject(context));
48-
LambdaLogger.Log("EVENT: " + JsonConvert.SerializeObject(invocationEvent));
49-
return accountUsage;
50-
}
51-
52-
public static async Task<GetAccountSettingsResponse> callLambda()
53-
{
54-
var request = new GetAccountSettingsRequest();
55-
var response = await lambdaClient.GetAccountSettingsAsync(request);
56-
return response;
57-
}
21+
private AmazonLambdaClient lambdaClient;
22+
23+
public Function()
24+
{
25+
initialize();
26+
}
27+
28+
async void initialize()
29+
{
30+
AWSSDKHandler.RegisterXRayForAllServices();
31+
lambdaClient = new AmazonLambdaClient();
32+
await callLambda();
33+
}
34+
35+
public async Task<AccountUsage> FunctionHandler(SQSEvent invocationEvent, ILambdaContext context)
36+
{
37+
GetAccountSettingsResponse accountSettings;
38+
try
39+
{
40+
accountSettings = await callLambda();
41+
}
42+
catch (AmazonLambdaException ex)
43+
{
44+
throw ex;
45+
}
46+
47+
AccountUsage accountUsage = accountSettings.AccountUsage;
48+
MemoryStream logData = new MemoryStream();
49+
StreamReader logDataReader = new StreamReader(logData);
50+
51+
Amazon.Lambda.Serialization.Json.JsonSerializer serializer = new Amazon.Lambda.Serialization.Json.JsonSerializer();
52+
53+
serializer.Serialize<System.Collections.IDictionary>(System.Environment.GetEnvironmentVariables(), logData);
54+
LambdaLogger.Log("ENVIRONMENT VARIABLES: " + logDataReader.ReadLine());
55+
logData.Position = 0;
56+
serializer.Serialize<ILambdaContext>(context, logData);
57+
LambdaLogger.Log("CONTEXT: " + logDataReader.ReadLine());
58+
logData.Position = 0;
59+
serializer.Serialize<SQSEvent>(invocationEvent, logData);
60+
LambdaLogger.Log("EVENT: " + logDataReader.ReadLine());
61+
62+
return accountUsage;
63+
}
64+
65+
public async Task<GetAccountSettingsResponse> callLambda()
66+
{
67+
var request = new GetAccountSettingsRequest();
68+
var response = await lambdaClient.GetAccountSettingsAsync(request);
69+
return response;
70+
}
5871
}
5972
}

sample-apps/blank-csharp/src/blank-csharp/aws-lambda-tools-defaults.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
"profile":"default",
1212
"region" : "us-east-2",
1313
"configuration" : "Release",
14-
"framework" : "netcoreapp3.1",
15-
"function-runtime":"dotnetcore3.1",
14+
"framework" : "net6.0",
15+
"function-runtime":"dotnet6",
1616
"function-memory-size" : 512,
1717
"function-timeout" : 30,
1818
"function-handler" : "blank-csharp::blankCsharp.Function::FunctionHandler"
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFramework>netcoreapp3.1</TargetFramework>
3+
<TargetFramework>net6.0</TargetFramework>
44
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
55
<AWSProjectType>Lambda</AWSProjectType>
66
</PropertyGroup>
77
<ItemGroup>
8-
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
9-
<PackageReference Include="Amazon.Lambda.Core" Version="1.1.0" />
10-
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="1.1.0" />
11-
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="1.7.0" />
12-
<PackageReference Include="AWSSDK.Core" Version="3.3.104.38" />
13-
<PackageReference Include="AWSSDK.Lambda" Version="3.3.108.11" />
14-
<PackageReference Include="AWSXRayRecorder.Core" Version="2.6.2" />
15-
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.7.2" />
8+
<PackageReference Include="Amazon.Lambda.Core" Version="2.1.0" />
9+
<PackageReference Include="Amazon.Lambda.SQSEvents" Version="2.1.0" />
10+
<PackageReference Include="Amazon.Lambda.Serialization.Json" Version="2.1.0" />
11+
<PackageReference Include="AWSSDK.Core" Version="3.7.103.24" />
12+
<PackageReference Include="AWSSDK.Lambda" Version="3.7.104.3" />
13+
<PackageReference Include="AWSXRayRecorder.Core" Version="2.13.0" />
14+
<PackageReference Include="AWSXRayRecorder.Handlers.AwsSdk" Version="2.11.0" />
1615
</ItemGroup>
1716
</Project>

0 commit comments

Comments
 (0)