Amazon Lambda in C # - JsonReaderException

I am trying to write an AWS Lambda function in C #. I have an AWS Toolkit for Visual Studio 2015. I created a project with the AWS Lambda Project (.Net Core) and then selected the Empty Function option. Which gave me the following code:

UPDATE and RESPONSE 02/24/17 . The comment designated as β€œAnswer” was useful knowledge, but was not the actual answer for me. It was a comment by @PavelSafronov in this answer that did the trick. I either missed nothing (and got an error), or I assumed that he wants me to send information in JSON format, so I would enter { "input": "Some string" } and still get the error. Now that I just went to "Some string", it worked. However , as far as I know, this seems like a mistake. A string is nullable by default, and the code written by Amazon even assumes that it can be virtual input?.ToUpper() , where ?. checks for null .

Note: I added the lines LambdaLogger.Log and Constructor to see where I get:

 using Amazon.Lambda.Core; using Amazon.Lambda.Serialization.Json; // Assembly attribute to enable the Lambda function JSON input to be converted into a .NET class. [assembly: LambdaSerializer(typeof(JsonSerializer))] namespace AWSLambdaTest1 { public class Function { public Function() { LambdaLogger.Log("Within the Constructor"); } public string KevinsTestFunction(string input, ILambdaContext context) { LambdaLogger.Log("Within the KTF"); return input?.ToUpper(); } } } 

The output screen and Solution Explorer said:

 Errors in C:\Test Projects\AWSLambda1\AWSLambda1\AWSLambda1.xproj Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0'. 

However, this will result in a crash and publication in AWS. I can even call it, which returns the following: And my main question :

 { "errorType" : "JsonReaderException", "errorMessage" : "Unexpected character encountered while parsing value: {. Path '', line 1, position 1.", "stackTrace" : [ "at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)", "at Newtonsoft.Json.JsonTextReader.ReadAsString()", "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.ReadForType(JsonReader reader, JsonContract contract, Boolean hasConverter)", "at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)", "at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)", "at Newtonsoft.Json.JsonSerializer.Deserialize[T](JsonReader reader)", "at lambda_method(Closure , Stream , Stream , ContextInfo )" ] } 

The log file indicated that the Constructors log message appeared, but is NOT the actual KevingsTestFunction log KevingsTestFunction .

On the side of the note, I can get the Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0' error Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0' to leave by adding the following to my project.json file:

 "runtimes": { "win10-x64": {}, "win81-x64": {}, "win8-x64": {}, "win7-x64": {} } 

Which makes sense on a Windows machine, not on Amazon. Do I need a few different runtime(s) ?

This change did not change the JsonReaderException .

I tried adding "Linux": {} , but didn't change anything.

I even tried updating the Microsoft.NETCore.App NuGet package from 1.0.0 to 1.1.0 , and it did nothing and even returned to 1.0.1 .

I would point out that the default example that they give you will work, but I'm wrong. It looks like there are problems with the Amazon.Lambda.Serialization.Json.JsonSerializer attribute. Can I just use NewtonSoft ?

+5
source share
2 answers

My experience with AWS Lambda in C # is a bit limited, but this is the kind of error that I get when the input ("test" data, if you run the function from the console) is not in real JSON format. The best bet might be to create a dummy input class for your testing purposes and then give lambda functions equivalent JSON data to ensure proper serialization.

As for your project.json, I didn't need to add a runtime section for AWS Lambda before. If you do this, you will need to select the appropriate time intervals from this list: https://docs.microsoft.com/en-us/dotnet/articles/core/rid-catalog

An example project.json file is also provided if you find this useful: https://gist.github.com/TAGray/8cc812065c3b6abcd732b7f6a3bda92a

Hope this helps.

+2
source

Per Pavel Safronov -

Yes, you should enter a string, not a json object, if your Lambda Function entry point:

 public string KevinsTestFunction(string input, ILambdaContext context) { 

Then the line you could enter:

 "{ \"input\": \"Something\" }" 

You can also change the entry point of Lambda functions to:

 public string KevinsTestFunction(JObject input, ILambdaContext context) { 

Then you can enter the json object like this:

 { "input": "Something" } 

If you are not expecting input and just query the SQS queue or DynamoDb table, you can change the Lambda Function entry point to:

 public string KevinsTestFunction(ILambdaContext context) { 

Many options to play with.


Now, for the update, in your default default question in the TEST area of ​​the AWS web console your default test data will be set to send your Lambda function. These test data are JSON objects by default. As noted, this contradicts the default templates for C # Lambdas (which accept string input), available through Visual Studio via the AWS SDK. Perhaps this is a built-in stumbling block (function) to make us deal with this problem, pull our hair over it, and then understand how versatile the function handler is ...

+2
source

Source: https://habr.com/ru/post/1264642/


All Articles