【问题标题】:Alexa Skill request deserialization fails - json to SkillRequest object C#Alexa Skill 请求反序列化失败 - json 到 SkillRequest 对象 C#
【发布时间】: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


【解决方案1】:

我遇到了同样的问题,按照我遵循的 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 包并引用我所关注的教程中引用的序列化程序的先前版本后,一切都运行良好。

【讨论】:

    【解决方案2】:

    只是想加入对我有用的事情。

    我有一个 ASP.NET Core 3.1 Web API 服务,我从 Alexa 获得了同样的错误。我添加了对 NuGet 包 Microsoft.AspNetCore.Mvc.NewtonsoftJson 的引用,然后将其弹出到我的 Startup.cs 文件中:ConfigureServices() 函数中的services.AddControllers().AddNewtonsoftJson();

    来源:https://dotnetcoretutorials.com/2019/12/19/using-newtonsoft-json-in-net-core-3-projects/

    看起来这也被报告为一个问题 https://github.com/timheuer/alexa-skills-dotnet/issues/193

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-11-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多