Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues1<MyLambda.MyDataTransferObject>.set_ConstructorAttributeProviderFactory(System.Func1<System.Reflection.ICustomAttributeProvider>)' #1944

Open
1 task
HHobeck opened this issue Jan 27, 2025 · 3 comments
Labels
bug This issue is a bug. module/lambda-client-lib p2 This is a standard priority issue

Comments

@HHobeck
Copy link

HHobeck commented Jan 27, 2025

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

@HHobeck HHobeck added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 27, 2025
@ashishdhingra ashishdhingra self-assigned this Jan 27, 2025
@ashishdhingra ashishdhingra added module/lambda-client-lib investigating This issue is being investigated and/or work is in progress to resolve the issue. p2 This is a standard priority issue and removed needs-triage This issue or PR still needs to be triaged. labels Jan 27, 2025
@normj
Copy link
Member

normj commented Jan 27, 2025

Can you change the [JsonSerializable(typeof(int[]))] on the LambdaFunctionJsonSerializerContext to [JsonSerializable(typeof(IReadOnlyCollection<int>))]? Since you are returning IReadOnlyCollection<int> from the Lambda function that is what you should use on the JSON context object. For me I got exceptions with your code till I swapped that around. Not sure why it worked in .NET 6, I'm guessing the difference in behavior of the Native AOT compiler and trimming.

@normj normj added response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. and removed investigating This issue is being investigated and/or work is in progress to resolve the issue. labels Jan 27, 2025
@HHobeck
Copy link
Author

HHobeck commented Jan 28, 2025

Can you change the [JsonSerializable(typeof(int[]))] on the LambdaFunctionJsonSerializerContext to [JsonSerializable(typeof(IReadOnlyCollection<int>))]? Since you are returning IReadOnlyCollection<int> from the Lambda function that is what you should use on the JSON context object. For me I got exceptions with your code till I swapped that around. Not sure why it worked in .NET 6, I'm guessing the difference in behavior of the Native AOT compiler and trimming.

Thank you very much for taking the time. I have tried it but I’m getting the same error. In my opinion it makes no sense to define an interface for source generation because at the end (on execution time) the lambda wants to serialize or deserialize a concrete type which is in this example an array of integers, Thus you need to define this type. If you want to deserialize e.g. a list of integers then you need to define a list not a IReadOnlyCollection. Another point is that the exception occurred when deserialization to the class MyDataTransferObject which is way before serialization to the result type.

I have done some trial and error and think I have identified the reference which cause the error. May I kindly ask you to add the Serilog.Settings.Configuration in version 9.0.0 and try it again? Would be great to have it reproducable.

@github-actions github-actions bot removed the response-requested Waiting on additional info and feedback. Will move to close soon in 7 days. label Jan 29, 2025
@normj
Copy link
Member

normj commented Jan 29, 2025

I did reproduce you same stacktrace when I added Serilog.Settings.Configuration version 9.0.0. When I reverted Serilog.Settings.Configuration to 8.0.4 I was able to get it to to work. I suspect the 9.0.0 version is using some trimming attributes that are only available in .NET 9. I'll try and find time to look more into it but since this is triggered by a library we don't control there is little we can do.

I did have to make the change on the JSON context object to use [JsonSerializable(typeof(IReadOnlyCollection<int>))] as well. For serializing System.Text.Json doesn't need concrete types. If it wasn't doing the source generator it would just reflect off the public properties if the interface.

@ashishdhingra ashishdhingra removed their assignment Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. module/lambda-client-lib p2 This is a standard priority issue
Projects
None yet
Development

No branches or pull requests

3 participants