正常插入,但是读取时报异常
消息:
System.FormatException : An error occurred while deserializing the TitleFilters property of class Czkj.AlibabaTools.XUnitTestProject.CopyBllTest+B: Value class System.ValueTuple`2[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.List`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] cannot be deserialized.
---- MongoDB.Bson.BsonSerializationException : Value class System.ValueTuple`2[[System.Boolean, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e],[System.Collections.Generic.List`1[[System.String, System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]], System.Private.CoreLib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e]] cannot be deserialized.
堆栈跟踪:
BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
BsonClassMapSerializer`1.DeserializeClass(BsonDeserializationContext context)
BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
IBsonSerializerExtensions.Deserialize[TValue](IBsonSerializer`1 serializer, BsonDeserializationContext context)
CursorBatchDeserializationHelper.DeserializeBatch[TDocument](RawBsonArray batch, IBsonSerializer`1 documentSerializer, MessageEncoderSettings messageEncoderSettings)
FindCommandOperation`1.CreateCursorBatch(BsonDocument commandResult)
FindCommandOperation`1.CreateCursor(IChannelSourceHandle channelSource, BsonDocument commandResult)
FindCommandOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
FindOperation`1.ExecuteAsync(RetryableReadContext context, CancellationToken cancellationToken)
FindOperation`1.ExecuteAsync(IReadBinding binding, CancellationToken cancellationToken)
OperationExecutor.ExecuteReadOperationAsync[TResult](IReadBinding binding, IReadOperation`1 operation, CancellationToken cancellationToken)
MongoCollectionImpl`1.ExecuteReadOperationAsync[TResult](IClientSessionHandle session, IReadOperation`1 operation, ReadPreference readPreference, CancellationToken cancellationToken)
MongoCollectionImpl`1.UsingImplicitSessionAsync[TResult](Func`2 funcAsync, CancellationToken cancellationToken)
IAsyncCursorSourceExtensions.FirstOrDefaultAsync[TDocument](IAsyncCursorSource`1 source, CancellationToken cancellationToken)
CopyBllTest.InsertValueTupleTest() 行 63
--- End of stack trace from previous location where exception was thrown ---
----- Inner Stack Trace -----
BsonClassMapSerializer`1.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
IBsonSerializer.Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
IBsonSerializerExtensions.Deserialize(IBsonSerializer serializer, BsonDeserializationContext context)
BsonClassMapSerializer`1.DeserializeMemberValue(BsonDeserializationContext context, BsonMemberMap memberMap)
插入后的mongo数据显示如下
{
"_id" : ObjectId("5db39eee71649a6e78275013"),
"TitleFilters" : {
"Item1" : false,
"Item2" : null
}
}
代码如下
[Fact]
public async Task InsertValueTupleTest()
{
//BsonClassMap.RegisterClassMap<B>();
var client = new MongoClient();
var db = client.GetDatabase("alibaba_web");
var testColloction = db.GetCollection<B>("testB");
var filter = Builders<B>.Filter.Empty;
await testColloction.DeleteManyAsync(filter);
var input = new B();
await testColloction.InsertOneAsync(input);
var ret = await testColloction.Find(filter).FirstOrDefaultAsync();//这行报错
Assert.NotNull(ret);
}
public class B
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string id { get; set; }
//[BsonElement]
public (bool, List<string>) TitleFilters { get; set; }
}
因为组件不支持值类型序列化,需要自定义序列化器,具体实现可以参考我的代码https://gitee.com/mushr/Helpers2/blob/master/Helpers.MongoDbHelper/ValueTupleSerializer.cs
没看明白怎么使用的
public (bool, List<string>) TitleFilters { get; set; } =(false, new List<string>()); 试一下
结果是一样
@雙孖星: 那就老老实实的 写个
Class C
{
public bool Item1 {get;set;}
public List<string> Item2 {get;set;}
}
class B
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string id { get; set; }
public C TitleFilters { get; set; }
}
@flyfishing: 我看了源码 原来是不支持