Exemplo n.º 1
0
 /// <summary>
 /// 自定义消息处理函数示例
 /// </summary>
 /// <param name="ch"></param>
 /// <param name="ea"></param>
 private void MessageEventHandler(object ch, BasicDeliverEventArgs ea)
 {
     Messager messageBody = JsonConvert.DeserializeObject <Messager>(Encoding.UTF8.GetString(ea.Body));
 }
Exemplo n.º 2
0
        public static void SendMessage(DBContextBase dbContext, Messager CurrentMessager)
        {
            //********************消息队列******************************

            var entries = dbContext.ChangeTracker.Entries();

            // 如果默认不发送,则启动允许列表
            if (FaSongPz == 0)
            {
                entries = entries.Where(m => DuiXingList.Contains(m.Entity.GetType().Name, StringComparer.InvariantCultureIgnoreCase));
            }
            else if (FaSongPz == 1) // 如果配置默认全部发送,则启动过滤列表
            {
                entries = entries.Where(m => !DuiXingList.Contains(m.Entity.GetType().Name.ToUpper()));
            }

            // 获取新增实体
            var addedEntries = entries.Where(m => m.State == EntityState.Added);

            foreach (DbEntityEntry entity in addedEntries)
            {
                // 创建一个Messager实体
                MessageEntity messageEntity = new MessageEntity();
                messageEntity.EntityName = entity.Entity.GetType().Name;
                messageEntity.Type       = OperationType.ADDED;
                messageEntity.Entity     = entity.Entity;

                // 反射出entity中的所有属性
                var properties = entity.Entity.GetType().GetProperties();
                foreach (var propertie in properties)
                {
                    // 获取主键
                    if (propertie.GetCustomAttributes(true).Any(m => m.GetType() == typeof(KeyAttribute)))
                    {
                        messageEntity.KeyNameList.Add(propertie.Name);
                    }
                }

                // 记录当前值
                foreach (var pName in entity.CurrentValues.PropertyNames)
                {
                    messageEntity.CurrentValues.Add(pName, entity.CurrentValues[pName]);
                }

                // 保存到当前待发送队列
                CurrentMessager.AddedEntityList.Add(messageEntity);
                CurrentMessager.EntityNameList.Add(messageEntity.EntityName);
                CurrentMessager.IsPublish = true;
            }

            // 获取删除实体
            var deletedEntries = entries.Where(m => m.State == EntityState.Deleted);

            foreach (var entity in deletedEntries)
            {
                // 创建一个Messager实体
                MessageEntity messageEntity = new MessageEntity();
                messageEntity.EntityName = entity.Entity.GetType().Name;
                messageEntity.Type       = OperationType.DELETED;
                messageEntity.Entity     = entity.Entity;

                // 反射出entity中的所有属性
                var properties = entity.Entity.GetType().GetProperties();
                foreach (var propertie in properties)
                {
                    // 获取主键
                    if (propertie.GetCustomAttributes(true).Any(m => m.GetType() == typeof(KeyAttribute)))
                    {
                        messageEntity.KeyNameList.Add(propertie.Name);
                    }
                }

                // 记录历史值
                foreach (var pName in entity.OriginalValues.PropertyNames)
                {
                    messageEntity.OriginalValues.Add(pName, entity.OriginalValues[pName]);
                }

                // 保存到当前待发送队列
                CurrentMessager.DeletedEntityList.Add(messageEntity);
                CurrentMessager.EntityNameList.Add(messageEntity.EntityName);
                CurrentMessager.IsPublish = true;
            }

            // 获取修改实体
            var modifiedEntries = entries.Where(m => m.State == EntityState.Modified);

            foreach (var entity in modifiedEntries)
            {
                // 创建一个Messager实体
                MessageEntity messageEntity = new MessageEntity();
                messageEntity.EntityName = entity.Entity.GetType().Name;
                messageEntity.Type       = OperationType.MODIFIED;
                messageEntity.Entity     = entity.Entity;

                // 反射出entity中的所有属性
                var properties = entity.Entity.GetType().GetProperties();
                foreach (var propertie in properties)
                {
                    // 获取主键
                    if (propertie.GetCustomAttributes(true).Any(m => m.GetType() == typeof(KeyAttribute)))
                    {
                        messageEntity.KeyNameList.Add(propertie.Name);
                    }
                }

                // 记录当前值
                foreach (var pName in entity.CurrentValues.PropertyNames)
                {
                    messageEntity.CurrentValues.Add(pName, entity.CurrentValues[pName]);
                }

                // 记录历史值
                foreach (var pName in entity.OriginalValues.PropertyNames)
                {
                    messageEntity.OriginalValues.Add(pName, entity.OriginalValues[pName]);
                }

                // 保存到当前待发送队列
                CurrentMessager.ModifiedEntityList.Add(messageEntity);
                CurrentMessager.EntityNameList.Add(messageEntity.EntityName);
                CurrentMessager.IsPublish = true;
            }

            //***********************消息队列***************************
        }