首页 新闻 会员 周边 捐助

c# mongo反序列化值元祖valuetuple时报异常System.FormatException

0
[已解决问题] 解决于 2019-10-30 10:05

正常插入,但是读取时报异常

消息: 
    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; }
        }
雙孖星的主页 雙孖星 | 初学一级 | 园豆:146
提问于:2019-10-26 10:13
< >
分享
最佳答案
0

因为组件不支持值类型序列化,需要自定义序列化器,具体实现可以参考我的代码https://gitee.com/mushr/Helpers2/blob/master/Helpers.MongoDbHelper/ValueTupleSerializer.cs

雙孖星 | 初学一级 |园豆:146 | 2019-10-30 10:04

没看明白怎么使用的

十色 | 园豆:210 (菜鸟二级) | 2022-03-16 10:56
其他回答(1)
0

public (bool, List<string>) TitleFilters { get; set; } =(false, new List<string>()); 试一下

flyfishing | 园豆:943 (小虾三级) | 2019-10-26 22:30

结果是一样

支持(0) 反对(0) 雙孖星 | 园豆:146 (初学一级) | 2019-10-28 09:08

@雙孖星: 那就老老实实的 写个
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; }
}

支持(0) 反对(0) flyfishing | 园豆:943 (小虾三级) | 2019-10-28 11:44

@flyfishing: 我看了源码 原来是不支持

支持(0) 反对(0) 雙孖星 | 园豆:146 (初学一级) | 2019-10-29 20:00
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册