Compatibility Issue with MongoDB Driver in .NET 8 AOT Environment

Hello MongoDB Community,

I am currently working on a project using .NET 8 and am attempting to leverage the Ahead-Of-Time (AOT) compilation feature. However, I’ve encountered a compatibility issue with the MongoDB driver, specifically when using AOT.

The error message I’m receiving is as follows:

Unhandled Exception: System.MissingMethodException: No suitable constructor found for serializer type: 'MongoDB.Bson.Serialization.Serializers.StringSerializer'.
   at MongoDB.Bson.Serialization...
   ... [remainder of stack trace] ...
   at WebApplication2!<BaseAddress>+0xcde883

This exception suggests that there might be an issue with the constructor selection for the StringSerializer in the AOT context. It seems to be a problem with reflection or dynamic code generation, which are known limitations of AOT environments.

I am keen to know if there is an existing or planned update for the MongoDB driver that will address this compatibility issue with .NET 8 AOT. Additionally, if anyone has encountered a similar problem or has suggestions for a workaround, your insights would be greatly appreciated.

Thank you for your time and assistance.

Best regards,
Marcelo

Hello @Marcelo_Gondim

Yes this a known issue, we are tracking this in CSHARP-4840.

The workaround would be opting-out the driver from trimming.
One way to do so is setting TrimMode to partial and specifying all trimmed assemblies manually by TrimmableAssembly item.

1 Like