【发布时间】:2020-08-07 19:42:31
【问题描述】:
我正在尝试跟随一个 youtube 视频教程,该教程教您如何在 C# 中构建一个简单的 alexa 技能。视频很旧,有些内容已弃用。最近的一位 cmets 说,在 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)"
]
}
}
有人知道这里出了什么问题吗?我已经用谷歌搜索了这个错误,但没有出现大量有用的信息,而且我很困惑。谢谢
【问题讨论】:
标签: c# amazon-web-services visual-studio alexa alexa-skills-kit