aws-sdk-net: Amazon DynamoDBContext - NET 7 AOT Incompatibilities
Describe the bug
Was running some NET7 AOT experiments with DynamoDBContext and was receiving runtime errors after doing an AOT compilation:
System.NotSupportedException: 'System.Collections.Generic.HashSet`1[System.Boolean]' is missing native code or metadata. This can happen for code that is not compatible with trimming or AOT. Inspect and fix trimming and AOT related warnings that were generated when the app was published. For more information see https://aka.ms/nativeaot-compatibility
at System.Reflection.Runtime.General.TypeUnifier.WithVerifiedTypeHandle(RuntimeConstructedGenericTypeInfo, RuntimeTypeInfo[]) + 0x88
at Amazon.DynamoDBv2.CollectionConverter.<GetTargetTypes>d__1.MoveNext() + 0x1d4
at Amazon.DynamoDBv2.ConverterCache.AddConverter(Converter converter, DynamoDBEntryConversion conversion) + 0x9b
at Amazon.DynamoDBv2.DynamoDBEntryConversion.AddConverter(Type type) + 0x41
at Amazon.DynamoDBv2.DynamoDBEntryConversion.AddConverters(String suffix) + 0x184
at Amazon.DynamoDBv2.DynamoDBEntryConversion..ctor(ConversionSchema schema, Boolean isImmutable) + 0x6e
at Amazon.DynamoDBv2.DynamoDBEntryConversion..cctor() + 0x25
at System.Runtime.CompilerServices.ClassConstructorRunner.EnsureClassConstructorRun(StaticClassConstructionContext*) + 0xc6
Expected Behavior
For DynamoDB to be AOT NET 7 supported
Current Behavior
Errors out after dotnet publish in NET7 AOT mode
Reproduction Steps
Attempt to run any DynamoDB queries that use DynamoDBContext with latest version of AWS SDK
Possible Solution
Decided to hack into the SDK to find the culprit. Discovered .MakeGenericType is throwing the runtime errors. Switched these 4 lines for experimentation purposes:
In DynamoDBEntryConversion.cs:
Line 651: var nullableType = typeof(Nullable<>).MakeGenericType(type)
Potential Fix?: yield return typeof(Nullable<>);
In SchemaV1.cs:
Line 379: yield return pt.MakeArrayType();
Fix?: yield return typeof(Array);
Line 382: yield return listType.MakeGenericType(pt);
Fix?: yield return typeof(List<>);
Line 384: yield return setType.MakeGenericType(pt);
Fix?: yield return typeof(HashSet<>);
This allowed my test application to run and work correctly. Is this is right long-term fix to the problem? I don’t know, but just wanted to let you know!
Additional Information/Context
With AWS starting to push AOT further, it would be great to start working through the bugs that prevent an AOT build from working. I believe this fits as a bug opposed to a feature request, especially with the push towards AOT compatible lambdas!
AWS .NET SDK and/or Package version used
AWS SDK DynamoDBv2 3.7.101.44
Targeted .NET Platform
.NET 7
Operating System and version
Windows 10
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Reactions: 6
- Comments: 19 (2 by maintainers)
In System.Runtime. Here the whole rd.xml:
Thanks for the detailed explanation. That helps a lot. I think this quote for the other issue you linked is important:
If option 2 is working as a work-around for now, that’s okay. But we wouldn’t recommend using any of this in production unless you can get rid of all trimming warnings in the first place. Because even if it’s working today, something could change in your code that breaks it at runtime in the future and you could have no advanced warning that it will break.
What really needs to be done is a refactoring/rewrite of the DynamoDb reflection code to remove unconstrained reflection in order to remove all trimming warnings. Possibly with source generators.
Adding to @jesmiatka. I needed these lines additionally to make it work: