gpt4 book ai didi

c# - Alexa Skill 请求反序列化失败 - json 到 SkillRequest 对象 C#

转载 作者:行者123 更新时间:2023-12-05 01:12:43 33 4
gpt4 key购买 nike

我想在这里得到一些帮助,我正在使用 Alexa.NET nuget 包使用 c# 开发自定义 alexa 技能,我收到以下错误。

我对函数的请求(AWS Lambda):

{
"version": "1.0",
"session": {
"new": true,
"sessionId": "amzn1.echo-api.session.[unique-value-here]",
"application": {
"applicationId": "amzn1.ask.skill.[unique-value-here]"
},
"user": {
"userId": "amzn1.ask.account.[unique-value-here]"
},
"attributes": {}
},
"context": {
"AudioPlayer": {
"playerActivity": "IDLE"
},
"System": {
"application": {
"applicationId": "amzn1.ask.skill.[unique-value-here]"
},
"user": {
"userId": "amzn1.ask.account.[unique-value-here]"
},
"device": {
"supportedInterfaces": {
"AudioPlayer": {}
}
}
}
},
"request": {
"type": "LaunchRequest",
"requestId": "amzn1.echo-api.request.[unique-value-here]",
"timestamp": "2016-10-27T18:21:44Z",
"locale": "en-US"
}
}

反序列化错误:

System.Exception: Error deserializing the input JSON to type SkillRequest
at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\codebuild\tmp\output\src142363207\src\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 214
at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.ExecuteAsync(ExecutionRequest request) in C:\codebuild\tmp\output\src142363207\src\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 52
---------------- Inner 1 Exception ------------
System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation.
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor, Boolean wrapExceptions)
at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
at Amazon.Lambda.TestTool.Runtime.LambdaExecutor.BuildParameters(ExecutionRequest request, ILambdaContext context) in C:\codebuild\tmp\output\src142363207\src\Tools\LambdaTestTool\src\Amazon.Lambda.TestTool\Runtime\LambdaExecutor.cs:line 202
---------------- Inner 2 Exception ------------
Amazon.Lambda.Serialization.SystemTextJson.JsonSerializerException: 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'
at Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer.Deserialize[T](Stream requestStream)
---------------- Inner 3 Exception ------------
System.NotSupportedException: Deserialization of reference types without parameterless constructor is not supported. Type 'Alexa.NET.Request.Type.Request'
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)

SkillRequest.cs(来自 Alexa.NET nuget 包):

public class SkillRequest
{
public SkillRequest();

[JsonProperty("version")]
public string Version { get; set; }
[JsonProperty("session")]
public Session Session { get; set; }
[JsonProperty("context")]
public Context Context { get; set; }
[JsonProperty("request")]
public Type.Request Request { get; set; } //This is throwing the deserialization error
//See below for properties within this.

public System.Type GetRequestType();
}

上面 SkillRequest 中的请求属性类型(这是我认为的问题所在):

[JsonConverter(typeof(RequestConverter))]
public abstract class Request
{
protected Request();

[JsonProperty("type", Required = Required.Always)]
public string Type { get; set; }
[JsonProperty("requestId")]
public string RequestId { get; set; }
[JsonProperty("locale")]
public string Locale { get; set; }
[JsonConverter(typeof(MixedDateTimeConverter))]
[JsonProperty("timestamp")]
public DateTime Timestamp { get; set; } // This might be the problem?
}

我尝试了不同的 DateTime 格式,我通过删除属性来玩弄,看看它是否超过了反序列化错误,似乎没有任何效果。有人可以帮忙吗?

最佳答案

我遇到了同样的问题,按照我遵循的 Alexa 教程序列化 JSON。这篇文章帮助我解决了这个问题,但是,我对在本地重写 Alexa.net 类的想法并不满意,因为它在我正在遵循的工作教程中以这种方式使用。

根据:Amazon从 .net core 3 开始,模板中使用了一个新的 JSON 序列化程序。它提供了性能优势,但似乎也会在 Alexa.Net 中引入此错误。

[assembly: LambdaSerializerAttribute(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))] 

被替换为

[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

新的序列化程序抛出“无参数构造函数”错误。通过 Nuget 安装 Amazon.Lambda.Serialization.Json 包,并引用我所遵循的教程中引用的序列化程序的先前版本后,一切都运行良好。

关于c# - Alexa Skill 请求反序列化失败 - json 到 SkillRequest 对象 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61742951/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com