Phani
Phani

Reputation: 861

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

I would like to get some help here, I am using Alexa.NET nuget package to develop a custom alexa skill using c#, I get the following error.

My Request to the Function(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"
  }
}

The Deserialization Error:

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(From Alexa.NET nuget package):

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 property type in SkillRequest above(this is where the problem is I think):

[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?
    }

I tried different DateTime formats, I played around by removing properties, to see if it goes past the deserialization error, nothing seems to be working. Can someone help?

Upvotes: 3

Views: 976

Answers (2)

Bryan Masephol
Bryan Masephol

Reputation: 47

Just wanted to chime in with what worked for me.

I have a ASP.NET Core 3.1 Web API service I'm hitting from Alexa and was getting this same error. I added a reference to NuGet package Microsoft.AspNetCore.Mvc.NewtonsoftJson and then popped this into my Startup.cs file: services.AddControllers().AddNewtonsoftJson(); in the ConfigureServices() function.

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

Also looks like this was sort of reported as an issue https://github.com/timheuer/alexa-skills-dotnet/issues/193

Upvotes: 3

Adrian
Adrian

Reputation: 1147

I had this same issue, serializing JSON as per an Alexa tutorial I was following. This post helped me to resolve it, however, I was not comfortable with the idea of rewriting the Alexa.net class locally, as it was used this way in working tutorials I was following.

According to: Amazon From .net core 3 there is a new JSON serializer used in the templates. It provides a performance benefit, but also seems to introduce this error with Alexa.Net.

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

was replaced by

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

The new serializer was throwing the 'parameterless constructor' error. After installing the Amazon.Lambda.Serialization.Json package via Nuget, and referencing the previous version of the serializer referenced in the tutorials I was following, all worked perfectly.

Upvotes: 4

Related Questions