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)

Most upvoted comments

In System.Runtime. Here the whole rd.xml:

<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
  <Application>
	<Assembly Name="AWSSDK.Core" Dynamic="Required All"></Assembly>
	<Assembly Name="AWSSDK.DynamoDBv2" Dynamic="Required All"></Assembly>
	<Assembly Name="System.Collections">
	  <Type Name="System.Collections.Generic.HashSet`1[[System.Boolean,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Byte,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Byte[],System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Char,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.DateTime,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Decimal,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Double,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Guid,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Int16,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Int32,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Int64,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.UInt16,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.UInt32,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.UInt64,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.SByte,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.Single,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.HashSet`1[[System.String,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Boolean,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Byte,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Byte[],System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Char,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.DateTime,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Decimal,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Double,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Guid,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Int16,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Int32,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Int64,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.UInt16,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.UInt32,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.UInt64,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.SByte,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.Single,System.Runtime]]" Dynamic="Required All" />
      <Type Name="System.Collections.Generic.List`1[[System.String,System.Runtime]]" Dynamic="Required All" />
	</Assembly>
	<Assembly Name="System.Runtime">
	  <Type Name="System.Nullable`1[System.Byte]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.SByte]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.UInt16]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Int32]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.UInt32]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Int16]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Int64]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.UInt64]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Single]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Double]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Decimal]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Char]" Dynamic="Required All"/>'
	  <Type Name="System.Nullable`1[System.DateTime]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Guid]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Enum]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.Boolean]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.IO.MemoryStream]" Dynamic="Required All"/>
	  <Type Name="System.Nullable`1[System.String]" Dynamic="Required All"/>
	</Assembly>
  </Application>
</Directives>

Thanks for the detailed explanation. That helps a lot. I think this quote for the other issue you linked is important:

The position is really that if something is generating warnings, it may not work, it needs to be written so that it doesn’t warn, and that’s it.

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:

<Type Name="System.Nullable`1[System.Byte]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.SByte]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.UInt16]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Int32]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.UInt32]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Int16]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Int64]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.UInt64]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Single]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Double]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Decimal]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Char]" Dynamic="Required All"/>'
<Type Name="System.Nullable`1[System.DateTime]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Guid]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Enum]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.Boolean]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.IO.MemoryStream]" Dynamic="Required All"/>
<Type Name="System.Nullable`1[System.String]" Dynamic="Required All"/>