Unhandled exception while casting object

Good day!

We experience difficulties after upgrading MongoDB.Driver to v2.19. In our project, we use the DevExtreme.AspNet.Data package to execute data queries (especially, grouping without expanding), and the following exception occurs

Unhandled exception. System.InvalidCastException: Unable to cast object of type 'MongoDB.Bson.Serialization.BsonClassMapSerializer`1[DevExtreme.AspNet.Data.Types.AnonType`2[System.Int32,System.String]]' to type 'MongoDB.Bson.Serialization.IBsonSerializer`1[System.Object]'.
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.CreateCollectionPipelineDefinition()
at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToExecutableQueryTranslators.ExecutableQuery`3.Execute(IClientSessionHandle session, CancellationToken cancellationToken)
at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](ExecutableQuery`2 executableQuery, CancellationToken cancellationToken)
at MongoDB.Driver.Linq.Linq3Implementation.MongoQueryProvider`1.Execute[TResult](ExecutableQuery`2 executableQuery)
at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.Execute()
at MongoDB.Driver.Linq.Linq3Implementation.MongoQuery`2.GetEnumerator()

at System.Linq.Enumerable.SelectEnumerableIterator`2.MoveNext()

at DevExtreme.AspNet.Data.GroupHelper`1.Group(IEnumerable`1 data, GroupingInfo groupInfo)
at DevExtreme.AspNet.Data.GroupHelper`1.Group(IEnumerable`1 data, IEnumerable`1 groupInfo)
at DevExtreme.AspNet.Data.RemoteGrouping.RemoteGroupTransformer.Run(Type sourceItemType, IEnumerable`1 flatGroups, Int32 groupCount, IReadOnlyList`1 totalSummary, IReadOnlyList`1 groupSummary)
at DevExtreme.AspNet.Data.DataSourceLoaderImpl`1.ExecRemoteGroupingAsync(Boolean remotePaging, Boolean suppressGroups, Boolean suppressTotals)
at DevExtreme.AspNet.Data.DataSourceLoaderImpl`1.LoadAsync()
at DevExtreme.AspNet.Data.DataSourceLoader.Load[T](IQueryable`1 source, DataSourceLoadOptionsBase options)

In the exception, DevExtreme.AspNet.Data.Types.AnonType is our custom implementation of an anonymous type to execute LINQ projections over System.Linq.Expressions.Expression(s) under the specified data shaping configuration.

We tried the team recommendations from here:

The ObjectSerializer has been changed to only allow deserialization of types that are considered safe.

var objectSerializer = new ObjectSerializer(type => ObjectSerializer.DefaultAllowedTypes(type) || type.FullName.StartsWith("<>f__AnonymousType") || type.FullName.StartsWith("DevExtreme.AspNet.Data"));
BsonSerializer.RegisterSerializer(objectSerializer);

However, it looks that explicitly registered type serializers are not considered while translating AstPipeline in Linq3Implementation.
Switching back to the LinqProvider.V2 provider works, but this is not an option.

I am unable to upload a ZIP file with the project here, so here is a direct link to download it from our server (43 kb) - https://downloads.devexpress.com/DXperience/MongoDB/MongoDriverSampleLinq3.zip

The archive contains three different projects showing the same issue (only the configuration of the references is slightly different in the projects for your convenience) along with our source code for your reference:

The “MongoDriverSample” dependency project - an entry point for all configurations that defines a data model that corresponds some collection, plus initializer that drops / creates the related collection and populates it with some data.

  • MongoDriverSampleNuGet. All references (MongoDB.Driver + DevExtreme.AspNet.Data) are from NuGet (4 kb);
  • MongoDriverSampleEmbeddedSimplified: A locally embedded / linked and extremely trimmed DevExtreme.AspNet.Data project source, sufficient to reproduce the issue (90 kb);
  • MongoDriverSampleEmbedded: Requires invoking a nested bat file to clone the full DevExtreme.AspNet.Data repository (5 kb / 4 mb).

Could you please take a look at this scenario and provide guidance on why it fails and how to proceed? Let us know if there is anything else we can provide/clarify to get the dedicated response.

@James_Kovacs my apologies for mentioning you directly. I see you took part in similar discussions before. May I ask you to leave a comment in this post?

The issue has been resolved in MongoDb C# Driver v2.24.1

See this thread for more information: https://jira.mongodb.org/browse/CSHARP-4976

Thanks!

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.