-
Notifications
You must be signed in to change notification settings - Fork 4
DynamoDB doesn't work with native AOT #10
Comments
This is the big problem we have with AOT. I don't think it's ready for production use yet as too many of the libraries either contain reflection and/or lack the information for serialization. We backed off from switching to AOT for our Lambdas for now. To get around your problem I think you'll have to add this directive to your Serializer Context function: [JsonSerializable(typeof(System.Byte))] It may be the first of many you'll have to add. |
Thank you for the hint. I tried it, but it didn't help. Here is the new code:
The exception was the same. |
Apologies, I might have been a bit unclear. When you created your Lambda you would have a Main function and a Serializer Context function (these are the boilerplate ones you get when you create in Visual Studio using the AOT template)
You need to add the [JsonSerializable(typeof(System.Byte))] to the Serializer context function like this:
Give that a try. You might get the same error but with a different type mentioned in this like of the stack trace: ---> System.NotSupportedException: 'System.Nullable`1[System.Byte]' 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 which is another directive to add to that function. Our feeling is that libraries really need to have these built in before AOT can safely be used in production. |
Thank you for the explanation. I added it to my LambdaFunctionJsonSerializerContext:
but it throws the same exception. I tried it also with |
Thanks for opening this issue @branog68 (and thanks for all the helpful details). And thanks for the suggestion @3Scribe. The serialize was a good place to look, but this seems to be related to custom code in the dynamoDb library that is using reflection. It enters into the library here: And eventually throws here (because the type Nullable<Byte> was trimmed out): Unfortunately, this seems to be an error that can't be fixed by telling the compiler not to trim the DynamoDb library since the thing actually being trimmed out is in a System library. I can see if it would be possible to exclude System from trimming, but you'd probably end up with a gigantic binary that wouldn't be usable. I can also see if using a TrimmerRootAssembly or other trimming option would work. I'll also open this issue against the aws-sdk-net repo, so they can track it there. The real fix will take a bit more effort, which is to actually remove these reflective calls from the DynamoDb library. |
I opened the issue here. aws/aws-sdk-net#2574 Let's track it there going forward, so I'll close this issue. But please open another issue or reopen this one if needed. For what it's worth, I think I was able to put together something that may eventually work, but it's very much a hacky solution. The below code uses the calls to DoNothing to force the compiler to keep those types around. But I only got through 3 types, I assume more and more would just keep throwing the error.
|
Possible work-around for now, here: aws/aws-sdk-net#2542 (comment) |
I wrote a lambda function with .net 7 native AOT. Without a database access, it worked properly. I tried to code access to DynamoDB table. Here is my code:
In the constructor for the statement Table.LoadTable I got following exception:
I'm using the latest version of AWSSDK.DynamoDBv2 (3.7.102.5). I added the AWSSDK.DynamoDBv2 in the rd-config file too:
I'm using Windows 11 Pro 10.0.22621 with dotnet version 7.0.100. And here is my project file:
The text was updated successfully, but these errors were encountered: