I am attempting to write an AWS Lambda function in C#. I have the 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 & ANSWER 02/24/17 - The comment marked as the Answer was useful knowledge but was not the actual answer for me. It was @PavelSafronov's comment in that answer that did the trick. I was either passing nothing in (and getting the error) or I assumed it wanted ME to give the information in JSON format so I would enter { "input": "Some string" } and still get the error. Now that I just passed in "Some string" it worked. However, as far as I am concerned, this seems like a bug. A string is by default nullable and the code written by Amazon even assumes it could be by virtual of the input?.ToUpper() where the ?. is checking for null.
Note: I added the LambdaLogger.Log lines and the Constructor to see where I was getting:
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.Json;
// Assembly attribute to enable the Lambda function's 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 were saying:
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 build and publish to AWS without fail. I can even Invoke it which returns the following - And is 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 showed that the Constructors log message got there but NOT the actual KevingsTestFunction log message.
On a side note, I am able to get the Unable to resolve 'Amazon.Lambda.Core (>= 1.0.0)' for '.NETCoreApp,Version=v1.0' error to go away 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 so much here on Amazon. Do I need some different runtime(s)?
That change did not change the JsonReaderException exception.
I tried adding "Linux": {} but that made no change either.
I even tried to update the NuGet package Microsoft.NETCore.App from 1.0.0 to 1.1.0 and that did nothing as well and even back to 1.0.1.
I would figure the default example they give you would work, but I am wrong there. It seems like there are issues with the Amazon.Lambda.Serialization.Json.JsonSerializer attribute. Is it possible to just use NewtonSoft?
nullis valid JSON, and if that is passed into a function that expects a string, the string parameter will be null. This can be verified by using the AWS Lambda Console and specifyingnullas the test JSON payload, but not the AWS VS Toolkit, as the Toolkit treats unquoted text as string (for convenience) and quotes it before passing it to Lambda.