I have a property in my MongoDB document which is the base class for the event raised from IdentityServer
public Event Event { get; set; } = null!;
the Event
is an abstract class that cannot be instantiated and have some derived types implemented in the IdentityServer package,
I got this exception when trying to get data:
System.FormatException: An error occurred while deserializing the Event property of class XXX: Cannot create an abstract class.
---> System.MemberAccessException: Cannot create an abstract class.
at System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(Type type)
at MongoDB.Bson.Serialization.BsonClassMap.<GetCreator>b__118_2()
at MongoDB.Bson.Serialization.BsonClassMap.CreateInstance()
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
at MongoDB.Bson.Serialization.BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
at MongoDB.Bson.Serialization.Serializers.SerializerBase`1.MongoDB.Bson.Serialization.IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
to avoid this issue I've tried adding [BsonKnownTypes(typeof(X),typeof(Y))]
attribute to my property,
regarding that, if any new derived type is added to a future version of IdeneityServer
which is not included in my BsonKnownTypes
, is this the best solution? what else can I do?