Exemplo n.º 1
0
 /// <summary>
 /// 加载缓存
 /// </summary>
 /// <exception cref=""></exception>
 private static void Load()
 {
     try
     {
         cache = new RedisModule();
     }
     catch (Exception ex)
     {
         LogModule.Error(ex.Message);
     }
 }
Exemplo n.º 2
0
 /// <summary>
 /// 实体映射
 /// </summary>
 /// <typeparam name="F">来源</typeparam>
 /// <typeparam name="T">目标</typeparam>
 /// <param name="Entity">来源数据</param>
 /// <returns></returns>
 public T Mapper <F, T>(F Entity)
 {
     try
     {
         return(OMM.GetMapper <F, T>(OwnDefaultMapConfigs).Map(Entity));
     }
     catch (Exception ex)
     {
         LogModule.Error("EmitMapper映射异常:" + ex);
         return(default(T));
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 访问数据库集合
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dbNme"></param>
 /// <param name="collectionName"></param>
 /// <returns></returns>
 public IMongoCollection <T> MongoCollection <T>(string dbNme, string collectionName)
 {
     try
     {
         var db  = _Client.GetDatabase(dbNme);
         var col = db.GetCollection <T>(collectionName);
         return(col);
     }
     catch (Exception ex)
     {
         LogModule.Error("MongoDB 访问数据库集合" + dbNme + "->" + collectionName + "失败:" + ex);
         return(null);
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 修改第一条
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dbNme"></param>
 /// <param name="collectionName"></param>
 /// <param name="query"></param>
 /// <param name="update"></param>
 /// <returns></returns>
 public int UpdateOne <T>(string dbNme, string collectionName, FilterDefinition <T> filter, UpdateDefinition <T> update)
 {
     try
     {
         var db  = _Client.GetDatabase(dbNme);
         var col = db.GetCollection <T>(collectionName);
         var u   = col.UpdateOne(filter, update);
         return(u.MatchedCount.ToInt());
     }
     catch (Exception ex)
     {
         LogModule.Error("MongoDB 修改数据库" + dbNme + "->" + collectionName + "失败:" + ex);
         return(0);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 插入多条数据
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dbNme"></param>
 /// <param name="collectionName"></param>
 /// <param name="t"></param>
 /// <returns></returns>
 public int Insert <T>(string dbNme, string collectionName, List <T> t)
 {
     try
     {
         var db  = _Client.GetDatabase(dbNme);
         var col = db.GetCollection <T>(collectionName);
         col.InsertMany(t);
         return(1);
     }
     catch (Exception ex)
     {
         LogModule.Error("MongoDB 添加数据库" + dbNme + "->" + collectionName + "失败:" + ex);
         return(0);
     }
 }
Exemplo n.º 6
0
 /// <summary>
 /// 查询
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dbNme"></param>
 /// <param name="collectionName"></param>
 /// <param name="bsonDocument">查询条件,为NULL时查询全部</param>
 public List <T> Query <T>(string dbNme, string collectionName, BsonDocument bsonDocument = null)
 {
     try
     {
         var db  = _Client.GetDatabase(dbNme);
         var col = db.GetCollection <T>(collectionName);
         bsonDocument = bsonDocument ?? new BsonDocument();
         var document = col.Find <T>(bsonDocument);
         return(document.ToList());
     }
     catch (Exception ex)
     {
         LogModule.Error("MongoDB 查询数据库" + dbNme + "->" + collectionName + "失败:" + ex);
         return(new List <T>());
     }
 }
Exemplo n.º 7
0
 /// <summary>
 /// 删除
 /// </summary>
 /// <typeparam name="T"></typeparam>
 /// <param name="dbNme"></param>
 /// <param name="collectionName"></param>
 /// <param name="bsonDocument">NULL 删除全部</param>
 /// <returns></returns>
 public int Delete <T>(string dbNme, string collectionName, BsonDocument bsonDocument = null)
 {
     try
     {
         var db  = _Client.GetDatabase(dbNme);
         var col = db.GetCollection <T>(collectionName);
         bsonDocument = bsonDocument ?? new BsonDocument();
         var d = col.DeleteMany(bsonDocument);
         return(d.DeletedCount.ToInt());
     }
     catch (Exception ex)
     {
         LogModule.Error("MongoDB 删除数据库" + dbNme + "->" + collectionName + "失败:" + ex);
         return(0);
     }
 }
Exemplo n.º 8
0
        public void RecvMessage(string _QueueName, Func <string, bool> _Func, QLKind _QLKind = QLKind.TeamEnd)
        {
            try
            {
                var _Factory = new ConnectionFactory
                {
                    HostName = HostName,
                    UserName = UserName,
                    Password = Password
                };

                using (var _Conn = _Factory.CreateConnection())
                {
                    using (var _Channel = _Conn.CreateModel())
                    {
                        //保证消息的不丢失(相对的)
                        bool durable = true;
                        _Channel.QueueDeclare(_QueueName, durable, false, false, null);
                        _Channel.BasicQos(0, 1, false);

                        var _Consumer = new QueueingBasicConsumer(_Channel);
                        _Channel.BasicConsume(_QueueName, false, _Consumer);

                        while (true)
                        {
                            var _Deliver = _Consumer.Queue.Dequeue();
                            var _Message = Encoding.UTF8.GetString(_Deliver.Body);

                            LogModule.Info("MQModule_RBMQ->RecvMessage:" + _QueueName + "\r\n" + _Message);
                            //处理业务
                            if (_Func(_Message) == false)
                            {
                                if (_QLKind == QLKind.TeamEnd)
                                {
                                    SendMessage(_QueueName, _Message);
                                }
                            }
                            _Channel.BasicAck(_Deliver.DeliveryTag, false);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogModule.Info("MQModule_RBMQ->RecvMessage:" + ex);
            }
        }
Exemplo n.º 9
0
 public MongoModule(MongoUrl mongoUrl)
 {
     try
     {
         if (MongoUrl == null)
         {
             MongoUrl = mongoUrl;
         }
         _Client      = new MongoClient(MongoUrl);
         IsConnection = true;
     }
     catch (Exception ex)
     {
         IsConnection = false;
         LogModule.Error("MongoDB连接失败," + _ConnectionString + ex);
     }
 }
Exemplo n.º 10
0
 public MongoModule(string connectionString)
 {
     connectionString = string.IsNullOrEmpty(connectionString) ? _ConnectionString : connectionString;
     try
     {
         if (MongoUrl == null)
         {
             var mongoUrl = new MongoUrl(_ConnectionString);
             MongoUrl = mongoUrl;
         }
         _Client      = new MongoClient(MongoUrl);
         IsConnection = true;
     }
     catch (Exception ex)
     {
         IsConnection = false;
         LogModule.Error("MongoDB连接失败," + connectionString + ex);
     }
 }
Exemplo n.º 11
0
        public void SendMessage(string _QueueName, string _QueueMessage)
        {
            try
            {
                //创建工厂
                var _Factory = new ConnectionFactory
                {
                    HostName = HostName,
                    UserName = UserName,
                    Password = Password
                };

                //建立连接
                using (var _Conn = _Factory.CreateConnection())
                {
                    using (var _Channel = _Conn.CreateModel())
                    {
                        //声明队列
                        _Channel.QueueDeclare(_QueueName, true, false, false, null);

                        //设置持久化
                        var properties = _Channel.CreateBasicProperties();
                        properties.Persistent   = true;
                        properties.DeliveryMode = 2;

                        //发送到RabbitMQ Server
                        _Channel.BasicPublish("", _QueueName, properties, Encoding.UTF8.GetBytes(_QueueMessage));
                        LogModule.Info("MQModule_RBMQ->SendMessage:" + _QueueName + "\r\n" + _QueueMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                LogModule.Info("MQModule_RBMQ->SendMessage:" + ex);
            }
        }