zoukankan      html  css  js  c++  java
  • 使用Rabbitmq.client反序列化包含Mongo.Bson.ObjectId属性实体类时抛异常

        
    原因分析:
       队列中存储的objectId属性是字符串,反序列化字符串转换成objectid类型时报错
     
     
    解决方法:
       1、定义ObjectIdConverter属性类,反序列化时将字符串转换成ObjectId
       
      class ObjectIdConverter : JsonConverter
        {
            public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
            {
                serializer.Serialize(writer, value.ToString());
            }
    
            /// <summary>
            //  反序列化时将字符串转换成ObjectId类型
            /// </summary>
            /// <param name="reader"></param>
            /// <param name="objectType"></param>
            /// <param name="existingValue"></param>
            /// <param name="serializer"></param>
            /// <returns></returns>
            public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
            {
               return new ObjectId(serializer.Deserialize(reader).ToString());
            }
    
            public override bool CanConvert(Type objectType)
            {
                return typeof(ObjectId).IsAssignableFrom(objectType);
            }
        }
       2、将该属性类应用到ObjectId属性上
         [BsonId]
            [JsonConverter(typeof(ObjectIdConverter))]
            public ObjectId Id { get; set; }
     



  • 相关阅读:
    JavaScript 类私有方法的实现
    sublime小程序插件
    显示引擎innodb状态详解
    JAVA学习资料大全
    mongo-aggregate命令详解
    PHP error_reporting
    mongo基本命令
    php56升级后php7 mcrypt_encrypt 报错
    docker 基础命令
    敏捷建模:增强沟通和理解
  • 原文地址:https://www.cnblogs.com/gossip/p/4505570.html
Copyright © 2011-2022 走看看