Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1<MyLambda.MyDataTransferObject>.set_ConstructorAttributeProviderFactory(System.Func`1<System.Reflection.ICustomAttributeProvider>)'

Open
#1,944 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
aws, csharp
Domain
backend, cloud

Research direction

Start with the supplied MyDataTransferObject and LambdaFunctionJsonSerializerContext reproduction, especially the generated Create_MyDataTransferObject call in the stack trace. Compare the .NET 8 source-generated serialization code with the listed Amazon.Lambda.Serialization.SystemTextJson, RuntimeSupport, and Annotations versions, then deploy to AmazonLinux to reproduce the mismatch. Done means the Lambda event deserializes successfully with the additional project reference.

Written by the indexing model from the issue text.

Description

bug module/lambda-client-lib p2
Describe the bug

Dear community,

I migrated an AWS lambda function from dotnet6.0 to dotnet8.0 by following the steps described here (Migrating from .NET 6 to .NET 8 Native AOT). When using the source generator class with name SourceGeneratorLambdaJsonSerializer I'm getting the following error on runtime in AWS (see the stack trace in Additional Information):

Error converting the Lambda event JSON payload to type MyLambda.MyDataTransferObject: Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues1<MyLambda.MyDataTransferObject>.set_ConstructorAttributeProviderFactory(System.Func1<System.Reflection.ICustomAttributeProvider>)'.

Any help would be appreciated.

Thank you!

Regression Issue
  • Select this option if this issue appears to be a regression.
Expected Behavior

The deserialization of the class MyDataTransferObject should be possible when using the generated source.

Current Behavior

When having an additional project reference (dotnet8.0) the MyDataTransferObject cannot be deserialized.

Reproduction Steps
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;
using MyLambda;
using System.Text.Json.Serialization;

[assembly: LambdaSerializer(
    typeof(SourceGeneratorLambdaJsonSerializer<LambdaFunctionJsonSerializerContext>)
)]
[assembly: LambdaGlobalProperties(GenerateMain = true)]

namespace MyLambda;

public record class MyDataTransferObject
{
    public string Id { get; set; } = null!;
}

public class Function
{
    [LambdaFunction]
    public async Task<IReadOnlyCollection<int>> FunctionHandler(MyDataTransferObject input, ILambdaContext context)
    {
        await Task.Delay(2000).ConfigureAwait(false);
        return [];
    }
}

/// <summary>
/// This class is used to register the input event and return type for the FunctionHandler method with the System.Text.Json source generator.
/// There must be a JsonSerializable attribute for each type used as the input and return type or a runtime error will occur 
/// from the JSON serializer unable to find the serialization information for unknown types.
/// </summary>
[JsonSerializable(typeof(MyDataTransferObject))]
[JsonSerializable(typeof(int[]))]
public partial class LambdaFunctionJsonSerializerContext : JsonSerializerContext
{
    // By using this partial class derived from JsonSerializerContext, we can generate reflection free JSON Serializer code at compile time
    // which can deserialize our class and properties. However, we must attribute this class to tell it what types to generate serialization code for
    // See https://docs.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-source-generation
}
Possible Solution

This problem occurs only on AWS and cannot be reproduced with the dotnet-lambda-test-tool-8.0 locally. I'm not sure if it is a dotnet problem or a AWS problem. Anyway I have found the following issue which might be related:

I don't use any .netstandard libraries.

Additional Information/Context
{
    "errorType": "JsonSerializerException",
    "errorMessage": "Error converting the Lambda event JSON payload to type MyLambda.MyDataTransferObject: Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1<MyLambda.MyDataTransferObject>.set_ConstructorAttributeProviderFactory(System.Func`1<System.Reflection.ICustomAttributeProvider>)'.",
    "stackTrace": [
        "at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream) + 0x216",
        "at Amazon.Lambda.RuntimeSupport.HandlerWrapper.<>c__DisplayClass26_0`2.<<GetHandlerWrapper>b__0>d.MoveNext() + 0x68",
        "--- End of stack trace from previous location ---",
        "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c",
        "at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task) + 0xbe",
        "at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task, ConfigureAwaitOptions) + 0x4e",
        "at Amazon.Lambda.RuntimeSupport.LambdaBootstrap.<InvokeOnceAsync>d__19.MoveNext() + 0x1ff"
    ],
    "cause": {
        "errorType": "MissingMethodException",
        "errorMessage": "Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1<MyLambda.MyDataTransferObject>.set_ConstructorAttributeProviderFactory(System.Func`1<System.Reflection.ICustomAttributeProvider>)'.",
        "stackTrace": [
            "at Internal.Runtime.TypeLoaderExceptionHelper.CreateMissingMethodException(ExceptionStringID, String) + 0x47",
            "at Internal.Runtime.CompilerHelpers.ThrowHelpers.ThrowMissingMethodException(ExceptionStringID, String) + 0x6",
            "at MyLambda.LambdaFunctionJsonSerializerContext.Create_MyDataTransferObject(JsonSerializerOptions) + 0x12",
            "at System.Text.Json.JsonSerializerOptions.GetTypeInfoNoCaching(Type) + 0x7f",
            "at System.Text.Json.JsonSerializerOptions.CachingContext.CreateCacheEntry(Type type, JsonSerializerOptions.CachingContext context) + 0x1e",
            "--- End of stack trace from previous location ---",
            "at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() + 0x1c",
            "at System.Text.Json.JsonSerializerOptions.CachingContext.CacheEntry.GetResult() + 0x1b",
            "at System.Text.Json.JsonSerializerOptions.GetTypeInfoInternal(Type, Boolean, Nullable`1, Boolean, Boolean) + 0x44",
            "at System.Text.Json.JsonSerializerOptions.TryGetTypeInfo(Type, JsonTypeInfo&) + 0x4b",
            "at MyLambda.LambdaFunctionJsonSerializerContext.GetTypeInfo(Type) + 0x27",
            "at Amazon.Lambda.Serialization.SystemTextJson.SourceGeneratorLambdaJsonSerializer`1.InternalDeserialize[T](Byte[]) + 0x4c",
            "at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream) + 0x176"
        ]
    }
}
AWS .NET SDK and/or Package version used
Targeted .NET Platform

.NET Core 8.0

Operating System and version

AmazonLinux

Dominant language
C#
Stars
1.7k
Forks
503
Avg merge
1d 18h
Merged PRs (30d)
21

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from aws/aws-lambda-dotnet

All issues in aws/aws-lambda-dotnet

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.