c# - 在 Visual Studio 的 AWS Toolkit 中调用 Alexa Skill 示例请求时出现 JSON 错误
问题描述
我正在尝试跟随一个 youtube 视频教程,教你如何在 C# 中构建一个简单的 alexa 技能。视频很旧,有些内容已弃用。最近的一条评论说,在 Visual Studio 2019 中,您必须将一行代码更改为
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
所以现在我的函数看起来像这样:
using System.Net.Http;
using Alexa.NET.Request;
using Alexa.NET.Request.Type;
using Alexa.NET.Response;
using Amazon.Lambda.Core;
// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
namespace LambdaAlexa
{
public class Function
{
public const string INVOCATION_NAME = "Country Info";
public SkillResponse FunctionHandler(SkillRequest input, ILambdaContext context)
{
var requestType = input.GetRequestType();
if (requestType == typeof(IntentRequest))
{
return MakeSkillResponse(
$"Hello Infotec! This is the first response from your Alexa skill using c sharp.",
true);
}
else
{
return MakeSkillResponse(
$"I don't know how to handle this intent. Please say something like Alexa, ask {INVOCATION_NAME} about Canada.",
true);
}
}
private SkillResponse MakeSkillResponse(string outputSpeech,
bool shouldEndSession,
string repromptText = "Just say, tell me about Canada to learn more. To exit, say, exit.")
{
var response = new ResponseBody
{
ShouldEndSession = shouldEndSession,
OutputSpeech = new PlainTextOutputSpeech { Text = outputSpeech }
};
if (repromptText != null)
{
response.Reprompt = new Reprompt() { OutputSpeech = new PlainTextOutputSpeech() { Text = repromptText } };
}
var skillResponse = new SkillResponse
{
Response = response,
Version = "1.0"
};
return skillResponse;
}
}
}
我在上传函数后尝试调用 MyColorIs 示例请求,但由于某种原因我得到了这个响应。
{
"errorType": "JsonSerializerException",
"errorMessage": "Error converting the Lambda event JSON payload to type Alexa.NET.Request.SkillRequest: Deserialization of reference types without parameterless constructor is not supported. Type 'Alexa.NET.Request.Type.Request'",
"stackTrace": [
"at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)",
"at lambda_method(Closure , Stream , Stream , LambdaContextInternal )"
],
"cause": {
"errorType": "NotSupportedException",
"errorMessage": "Deserialization of reference types without parameterless constructor is not supported. Type 'Alexa.NET.Request.Type.Request'",
"stackTrace": [
"at System.Text.Json.ThrowHelper.ThrowNotSupportedException_DeserializeCreateObjectDelegateIsNull(Type invalidType)",
"at System.Text.Json.JsonSerializer.HandleStartObject(JsonSerializerOptions options, ReadStack& state)",
"at System.Text.Json.JsonSerializer.ReadCore(JsonSerializerOptions options, Utf8JsonReader& reader, ReadStack& readStack)",
"at System.Text.Json.JsonSerializer.ReadCore(Type returnType, JsonSerializerOptions options, Utf8JsonReader& reader)",
"at System.Text.Json.JsonSerializer.ParseCore(ReadOnlySpan`1 utf8Json, Type returnType, JsonSerializerOptions options)",
"at System.Text.Json.JsonSerializer.Deserialize[TValue](ReadOnlySpan`1 utf8Json, JsonSerializerOptions options)",
"at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)"
]
}
}
有谁知道这里有什么问题?我已经用谷歌搜索了这个错误,但没有出现大量有用的信息,而且我很困惑。谢谢
解决方案
通过NuGet安装Amazon.Lambda.Serialization.Json
包并替换
[assembly:LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
和
[assembly: LambdaSerializerAttribute(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
推荐阅读
- c# - 将 Cognito 与 C# 无服务器应用程序一起使用
- javascript - 如何在javascript中将ajax数据设置为数据表
- java - webivew中的Android支付网关错误
- django - UnicodeEncodeError:'charmap'编解码器无法编码字符 - Python Report CSS
- powershell - PowerShell - 使用未签名证书查询 REST API
- javascript - 显示确认弹出窗口并禁用点击 Facebook 等侧边弹出窗口,如何?
- python - 使用自定义估算器 api 包装的 tensorflow 代码是否在 google cloud-ml 引擎或本地机器中有效地使用 gpu?
- django - 错误 RuntimeError: populate() is not reentrant 是什么意思
- python-3.6 - python3库安装后消失
- sql - 如何获取表的所有匹配列的值并存储在SQL中的新表中