C# Driver Mongo DB BsonSerializationException when using IAggregateFluentExtensions.GraphLookup() with GUIDs

Hi there I’ve a problem with Guid and C# driver. When I try to do a GraphLookup like this:
var objlist = await _collection
.Aggregate()
.Match(rel=>rel.IsRoot && rel.NodeId == NodeId)
.GraphLookup<NodeRelation, string, string, string, NodeRelation, IEnumerable, object>(
from: _collection,
connectFromField: “_id”,
connectToField: “ParentId”,
startWith: “$_id”,
@as: “Structure”, depthField: “stage”).ToListAsync();
I get an error like below. _id, NodeId and ParentId are Guid fields. I use BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard)); at startup and with this I was able to add data. But this part fails.
Error:
MongoDB.Bson.BsonSerializationException: GuidSerializer cannot deserialize a Guid when GuidRepresentation is Unspecified.
at MongoDB.Bson.Serialization.Serializers.GuidSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context)
at MongoDB.Bson.Serialization.Serializers.DynamicDocumentBaseSerializer1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args) at MongoDB.Bson.Serialization.Serializers.SerializerBase1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.DeserializeDiscriminatedValue(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.ObjectSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Bson.Serialization.Serializers.EnumerableSerializerBase2.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.Operations.AggregateOperation1.CursorDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.Operations.AggregateOperation1.AggregateResultDeserializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer1 serializer, BsonDeserializationContext context) at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol1.ProcessResponse(ConnectionId connectionId, CommandMessage responseMessage)
at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol1.SendMessageAndProcessResponseAsync(CommandRequestMessage message, Int32 responseTo, IConnection connection, CancellationToken cancellationToken) at MongoDB.Driver.Core.WireProtocol.CommandUsingCommandMessageWireProtocol1.ExecuteAsync(IConnection connection, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Servers.Server.ServerChannel.ExecuteProtocolAsync[TResult](IWireProtocol1 protocol, ICoreSession session, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.RetryableReadOperationExecutor.ExecuteAsync[TResult](IRetryableReadOperation1 operation, RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.ReadCommandOperation1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken) at MongoDB.Driver.Core.Operations.AggregateOperation1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
at MongoDB.Driver.Core.Operations.AggregateOperation1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken) at MongoDB.Driver.OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation1 operation, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.AggregateAsync[TResult](IClientSessionHandle session, PipelineDefinition2 pipeline, AggregateOptions options, CancellationToken cancellationToken)
at MongoDB.Driver.MongoCollectionImpl1.UsingImplicitSessionAsync[TResult](Func2 funcAsync, CancellationToken cancellationToken)
at MongoDB.Driver.IAsyncCursorSourceExtensions.ToListAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)

Any ideas or suggestions?

Hi @MarcReiter

Welcome to the forum :slight_smile:
Can you show the way that NodeRelation is defined?

Hi Ferdinando, it’s defined like this. I also tried adding an attribute ```
[BsonRepresentation(BsonType.Standard)]

as I wrote i do `BsonSerializer.RegisterSerializer(new GuidSerializer(GuidRepresentation.Standard));` on startup.
public class NodeRelation
{
    public Guid Id { get; set; }
    public Guid NodeId { get; set; }
    public Guid ParentId { get; set; }
    public Guid RootId { get; set; }
    public Int64 Order { get; set; }
    public bool IsRoot { get; set; }
    public bool IsLink { get; set; }
    public bool IsVirtual { get; set; }
    public bool IsFSONode { get; set; }
}

Thank you very much. I managed to reproduce your error, I’ll investigate and get back to you :slight_smile:

Hi @MarcReiter

I managed to find the reason for this. The issue is that internally the GraphLookup is using the ObjectSerializer, that has his own guidSerializer instance that does not have its GuidRepresentation specified. The solution to this is registering your own ObjectSerializer like this:

var objectSerializer = new ObjectSerializer(BsonSerializer.LookupDiscriminatorConvention(typeof(object)),
  GuidRepresentation.Standard);
BsonSerializer.RegisterSerializer(objectSerializer);

I understand this is not straightforward, and I’ll create a ticket to improve the experience for these kinds of situations, but I hope this will help you for now.

Hi @papafe,

thanks for your reply. That’s exactly doing the trick. Nevermind it’s no problem if we know how to do. Compared to other systems GUID are doing well with the few tricks. Also great support by you and your company!
By the help of GraphLookup I was able to reduce responstime from about 600ms to about 35ms!!!
I’ll keep going while I have to learn a lot, but its fun so far!
Regards marc

2 Likes

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