aws / aws/aws-lambda-dotnet

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
bug module/lambda-client-lib p2
Dominant language
C#
Stars
1.7k
Forks
503
Avg merge
1d 5h
Merged PRs (30d)
18

Description

### 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)](https://aws.amazon.com/blogs/compute/introducing-the-net-8-runtime-for-aws-lambda/). 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.JsonObjectInfoValues`1.set_ConstructorAttributeProviderFactory(System.Func`1)'.**

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

```csharp
using Amazon.Lambda.Annotations;
using Amazon.Lambda.Core;
using Amazon.Lambda.Serialization.SystemTextJson;
using MyLambda;
using System.Text.Json.Serialization;

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

namespace MyLambda;

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

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

///
/// 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.
///
[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:
- https://github.com/dotnet/runtime/issues/61737

I don't use any .netstandard libraries.

### Additional Information/Context

```json
{
"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.set_ConstructorAttributeProviderFactory(System.Func`1)'.",
"stackTrace": [
"at Amazon.Lambda.Serialization.SystemTextJson.AbstractLambdaJsonSerializer.Deserialize[T](Stream) + 0x216",
"at Amazon.Lambda.RuntimeSupport.HandlerWrapper.<>c__DisplayClass26_0`2.<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.d__19.MoveNext() + 0x1ff"
],
"cause": {
"errorType": "MissingMethodException",
"errorMessage": "Method not found: 'Void System.Text.Json.Serialization.Metadata.JsonObjectInfoValues`1.set_ConstructorAttributeProviderFactory(System.Func`1)'.",
"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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.