【发布时间】:2020-08-27 18:37:38
【问题描述】:
我想在这里得到一些帮助,我正在使用 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 格式,我通过删除属性来玩弄,看看它是否超过了反序列化错误,似乎没有任何效果。有人可以帮忙吗?
【问题讨论】:
-
我猜你的
Request类不能是abstract并且必须有公共构造函数 -
我制作了 nuget 类的本地副本,并删除了 Request 类的 abstract 关键字并且它起作用了。感谢您的提示,如果您可以将您的建议作为答案发布,我会将其标记为正确答案。 @vasily.sib
-
虽然不需要公共构造函数.....
标签: c# json aws-lambda deserialization alexa