System.InvalidCastException: 'Unable to cast object of type 'MongoDB.Bson.Serialization.Serializers.ObjectSerializer' to type 'MongoDB.Bson.Serialization.IBsonDocumentSerializer'.'

We’re upgrading some parts of our software to the latest drivers (2.28) from quite old ones (2.9.2).
We’re using fully dynamic model search/count as we let our customers build their own.
Since the upgrade this (simplified) code throws “System.InvalidCastException: ‘Unable to cast object of type ‘MongoDB.Bson.Serialization.Serializers.ObjectSerializer’ to type ‘MongoDB.Bson.Serialization.IBsonDocumentSerializer’.’”

var coll = _mongoDB.GetCollection<dynamic>( "SomeCollectin" );
IFindFluent<dynamic,dynamic> filterfind = coll.Find( _ => true );
return filterfind.CountDocuments();

Anything we should be doing different with this new drivers?

Hi @Gianandrea_Terzi,

One difference from the older version of the driver is that the ObjectSerializer is now strict about what types are allowed to be serialized or deserialized, so you have to configure the allowedTypes argument of ObjectSerializer. But the exception you got is not related to allowedTypes. We’ll need to investigate this further to identify the issue, I created CSHARP-5293 for this. If there’s any more info you could provide or expand on the snippet that throws the exception that would be helpful.

Thanks,

Rishit.