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

Alexa Skill request deserialization fails - json to SkillRequest object C#

我想在这里得到一些帮助,我正在使用 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();
    }

Request 属性 在上面输入 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。这个 post 帮助我解决了它,但是,我对在本地重写 Alexa.net class 的想法感到不舒服,因为它在我正在关注的工作教程中以这种方式使用。

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

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

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

新的序列化程序抛出 'parameterless constructor' 错误。在通过 Nuget 安装 Amazon.Lambda.Serialization.Json 包并引用我所遵循的教程中引用的先前版本的序列化程序之后,一切都完美无缺。

只是想谈谈对我有用的东西。

我有一个从 Alexa 访问的 ASP.NET Core 3.1 Web API 服务,但遇到了同样的错误。我添加了对 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