From 4597bbfb8981950eb7bd738bf1acdb34b7a91a35 Mon Sep 17 00:00:00 2001 From: H Vs Date: Thu, 29 Jun 2023 11:47:20 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Cache/EntityCacheHandler.cs | 8 +- .../Cache/IDurableEntityManager.cs | 2 +- .../Cache/IEntityCacheHandler.cs | 4 +- .../Common/CustomizeStopWatch.cs | 5 +- .../Dal/EfCoreImpl/EfCoreDataAccessor.cs | 186 +++++++++--------- HealthMonitor.Core/Dal/IDataAccessor.cs | 4 +- .../Redis/GpsCardOperatorRedisManager.cs | 22 +-- .../Pipeline/Aop/AopValueBase.cs | 2 +- .../Pipeline/Aop/CacheInterceptorContext.cs | 16 +- .../Aop/Delete/DeleteEntityCacheHisValue.cs | 6 +- .../Aop/Delete/DeleteEntityCacheValue.cs | 6 +- .../Aop/Query/AssertValidQueryHisValue.cs | 8 +- .../Aop/Query/AssertValidQueryValue.cs | 8 +- .../Aop/Query/GetOrInsertEntityCacheValue.cs | 12 +- .../Aop/Update/UpdateEntityCacheValue.cs | 2 +- .../Query/Extensions/OrderConditionsExt.cs | 2 +- HealthMonitor.Model/Cache/GpsDevicePerson.cs | 2 +- .../Entities/Base/EntityBase.cs | 2 +- .../Controllers/Base/GpsCardControllerBase.cs | 31 +-- HealthMonitor.WebApi/DbLog/EfCoreLogger.cs | 27 +-- .../CustomLoggingScopeHttpMessageHandler.cs | 10 +- HealthMonitor.WebApi/appsettings.test.json | 2 +- 22 files changed, 177 insertions(+), 190 deletions(-) diff --git a/HealthMonitor.Core/Cache/EntityCacheHandler.cs b/HealthMonitor.Core/Cache/EntityCacheHandler.cs index 74d0e4a..34fac50 100644 --- a/HealthMonitor.Core/Cache/EntityCacheHandler.cs +++ b/HealthMonitor.Core/Cache/EntityCacheHandler.cs @@ -65,8 +65,8 @@ namespace HealthMonitor.Core.Cache if (entity == null) return null; string id = _m_GetPrimaryKey.Invoke(entity, null) + ""; - if (!_mapper.TryRemove(id, out List> rels)) return null; - + //if (!_mapper.TryRemove(id, out List> rels)) return null; + if (!_mapper.TryRemove(id, out var rels)) return null; return rels.Select(e => e.Item1).Distinct(); } @@ -81,8 +81,8 @@ namespace HealthMonitor.Core.Cache } foreach (var id in _mapper.Keys) { - if (!_mapper.TryRemove(id, out List> rels)) continue; - + // if (!_mapper.TryRemove(id, out List> rels)) continue; + if (!_mapper.TryRemove(id, out var rels)) continue; var availableList = rels.Where(t => DateTime.Now.Subtract(t.Item2).TotalSeconds < DurableSecond).ToList(); _mapper.AddOrUpdate(id, _ => availableList, diff --git a/HealthMonitor.Core/Cache/IDurableEntityManager.cs b/HealthMonitor.Core/Cache/IDurableEntityManager.cs index 14a04fd..3a7a723 100644 --- a/HealthMonitor.Core/Cache/IDurableEntityManager.cs +++ b/HealthMonitor.Core/Cache/IDurableEntityManager.cs @@ -9,7 +9,7 @@ namespace HealthMonitor.Core.Cache /// /// /// - IEntityCacheHandler GetCacheHandler(Type entityType); + IEntityCacheHandler? GetCacheHandler(Type entityType); /// /// 获取缓存键 diff --git a/HealthMonitor.Core/Cache/IEntityCacheHandler.cs b/HealthMonitor.Core/Cache/IEntityCacheHandler.cs index 437b0f1..f5c5bb4 100644 --- a/HealthMonitor.Core/Cache/IEntityCacheHandler.cs +++ b/HealthMonitor.Core/Cache/IEntityCacheHandler.cs @@ -12,7 +12,7 @@ /// /// /// - IEnumerable GetEntitiesCache(string key); + IEnumerable? GetEntitiesCache(string key); /// /// 根据缓存键更新实体 @@ -32,7 +32,7 @@ /// 获取并移除指定实体主键映射的所有缓存键 /// /// - IEnumerable UnmapKeyFromEntity(object entity); + IEnumerable? UnmapKeyFromEntity(object entity); /// /// 清理失效(超时)的映射关系 diff --git a/HealthMonitor.Core/Common/CustomizeStopWatch.cs b/HealthMonitor.Core/Common/CustomizeStopWatch.cs index c11f7c5..cf46e3d 100644 --- a/HealthMonitor.Core/Common/CustomizeStopWatch.cs +++ b/HealthMonitor.Core/Common/CustomizeStopWatch.cs @@ -9,7 +9,7 @@ namespace HealthMonitor.Core.Common private readonly string _domain; private readonly ILogger _logger; - public string Content { get; set; } + public string Content { get; set; }=default!; public CustomizeStopWatch(string domain, ILogger logger) { @@ -24,9 +24,10 @@ namespace HealthMonitor.Core.Common { if (_sw != null) { - _logger.LogInformation($"统计时间[{_domain}],耗时 {_sw.Elapsed.TotalMilliseconds} 毫秒 {Content}"); + _logger.LogInformation("统计时间[{_domain}],耗时 {_sw.Elapsed.TotalMilliseconds} 毫秒 {Content}", _domain, _sw.Elapsed.TotalMilliseconds, Content); _sw.Stop(); } + GC.SuppressFinalize(this); } } } diff --git a/HealthMonitor.Core/Dal/EfCoreImpl/EfCoreDataAccessor.cs b/HealthMonitor.Core/Dal/EfCoreImpl/EfCoreDataAccessor.cs index c08dcff..9af1ffd 100644 --- a/HealthMonitor.Core/Dal/EfCoreImpl/EfCoreDataAccessor.cs +++ b/HealthMonitor.Core/Dal/EfCoreImpl/EfCoreDataAccessor.cs @@ -96,11 +96,11 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl var exp = BuildPrimaryKeyQueryExp(values); if (exp == null) return default!; - var entity = _context.Set().FirstOrDefault(exp); + var entity = _context!.Set().FirstOrDefault(exp); //var entity = _context.Set().Find(values); if (entity != null) _context.Entry(entity).State = EntityState.Detached; //取消实体跟踪 - return entity; + return entity!; } /// @@ -111,16 +111,16 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// 返回主键值为传入值的实体 public async Task GetByIDAsync(params object[] values) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; var exp = BuildPrimaryKeyQueryExp(values); - if (exp == null) return default; + if (exp == null) return default!; - var entity = await _context.Set().FirstOrDefaultAsync(exp); + var entity = await _context!.Set().FirstOrDefaultAsync(exp); //var entity = await _context.Set().FindAsync(values); if (entity != null) _context.Entry(entity).State = EntityState.Detached; //取消实体跟踪 - return entity; + return entity!; } /// @@ -130,8 +130,8 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public IQueryable GetAll() where T : class { - if (IsClose()) return default; - var result = _context.Set().AsNoTracking().Where(s => true); + if (IsClose()) return default!; + var result = _context!.Set().AsNoTracking().Where(s => true); return result; } @@ -143,8 +143,8 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public IQueryable GetMany(Expression> expression) where T : class { - if (IsClose()) return default; - var result = _context.Set().AsNoTracking().Where(expression); + if (IsClose()) return default!; + var result = _context!.Set().AsNoTracking().Where(expression); return result; } @@ -158,12 +158,12 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public IEnumerable Order(Func orderExpression, bool isASC = false) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; if (isASC) - return _context.Set().AsNoTracking().OrderBy(orderExpression); + return _context!.Set().AsNoTracking().OrderBy(orderExpression); else - return _context.Set().AsNoTracking().OrderByDescending(orderExpression); + return _context!.Set().AsNoTracking().OrderByDescending(orderExpression); } /// @@ -176,7 +176,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public List GetPageList(int pageSize, int pageIdx, Expression> expression, Func orderExpression) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; var datas = this.GetMany(expression); var result = datas.OrderBy(orderExpression).Skip(pageSize * (pageIdx - 1)).Take(pageSize); @@ -194,7 +194,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// 分页查询结果 public async Task> GetPageListAsync(int pageSize, int pageIdx, Expression> expression, Func orderExpression) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; var datas = this.GetMany(expression); var result = datas.OrderBy(orderExpression).Skip(pageSize * (pageIdx - 1)).Take(pageSize).AsQueryable(); @@ -243,23 +243,23 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public T AddWithIdentity(T model) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; T retModel; try { if (model != null) { - _context.Set().Add(model); + _context!.Set().Add(model); _context.SaveChanges(); retModel = model; } else - retModel = null; + retModel = null!; } catch (Exception) { - retModel = null; + retModel = null!; } return retModel; @@ -273,23 +273,23 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// public async Task AddWithIdentityAsync(T model) where T : class { - if (IsClose()) return default; + if (IsClose()) return default!; T retModel; try { if (model != null) { - _context.Set().Add(model); + _context!.Set().Add(model); await _context.SaveChangesAsync(); retModel = model; } else - retModel = null; + retModel = null!; } catch (Exception) { - retModel = null; + retModel = null!; } return retModel; @@ -307,14 +307,14 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - _context.Set().Add(model); + _context!.Set().Add(model); } public void Add(object model) { if (IsClose()) return; if (model == null) return; - _context.Add(model); + _context!.Add(model); } /// @@ -325,7 +325,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (models == null || models.Count() == 0) return; - _context.AddRange(models); + _context!.AddRange(models); } /// @@ -340,14 +340,14 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl if (model == null) return; try { - var entity = _context.Remove(model); + var entity = _context!.Remove(model); } catch (InvalidOperationException e) { // 若在追踪列表中已经存在另一个实体和该实体有相同主键(但不是同一个) // 则找到追踪列表里的实体把它标记为删除 Type modelType = typeof(T); - var key = _context.Model.FindEntityType(modelType).FindPrimaryKey(); + var key = _context!.Model.FindEntityType(modelType)!.FindPrimaryKey(); if (key == null) { throw e; @@ -361,8 +361,8 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl foreach (var p in props) { var clrProp = modelType.GetProperty(p.Name); - var val = clrProp.GetValue(model); - param[idx] = val; + var val = clrProp!.GetValue(model); + param[idx] = val!; idx++; } // 用主键找实体,标记为删除 @@ -379,7 +379,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - _context.Remove(model); + _context!.Remove(model); } /// @@ -390,7 +390,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (models == null || models.Count() == 0) return; - _context.RemoveRange(models); + _context!.RemoveRange(models); } /// @@ -403,7 +403,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - var entity = _context.Entry(model); + var entity = _context!.Entry(model); entity.State = EntityState.Modified; } @@ -419,7 +419,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - var entity = _context.Entry(model); + var entity = _context!.Entry(model); entity.Property(property).IsModified = true; } @@ -427,7 +427,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - _context.Update(model); + _context!.Update(model); } /// @@ -438,7 +438,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (models == null || models.Count() == 0) return; - _context.UpdateRange(models); + _context!.UpdateRange(models); } #endregion @@ -450,37 +450,33 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl /// 返回的数据表 public List CallProcedure(string procName, params DbParameter[] parameters) { - if (IsClose()) return default; + if (IsClose()) return default!; List ret = new List(); - var connection = _context.Database.GetDbConnection(); - DbDataReader reader = null; - DataTable table = null; - using (DbCommand cmd = connection.CreateCommand()) + var connection = _context!.Database.GetDbConnection(); + using DbCommand cmd = connection.CreateCommand(); + IDbContextTransaction tran = _context!.Database.CurrentTransaction!; + if (tran != null) { - IDbContextTransaction tran = _context.Database.CurrentTransaction; - if (tran != null) - { - cmd.Transaction = tran.GetDbTransaction(); - } + cmd.Transaction = tran.GetDbTransaction(); + } - cmd.CommandType = System.Data.CommandType.StoredProcedure; - cmd.CommandText = procName; - cmd.Parameters.AddRange(parameters); - _context.Database.OpenConnection(); - reader = cmd.ExecuteReader(); + cmd.CommandType = System.Data.CommandType.StoredProcedure; + cmd.CommandText = procName; + cmd.Parameters.AddRange(parameters); + _context.Database.OpenConnection(); + DbDataReader? reader = cmd.ExecuteReader(); + DataTable? table = ReaderToDataTable(reader); + ret.Add(table); + while (reader.NextResult()) + { table = ReaderToDataTable(reader); ret.Add(table); - while (reader.NextResult()) - { - table = ReaderToDataTable(reader); - ret.Add(table); - } - reader.Close(); - //context.Database.CloseConnection(); - - return ret; } + reader.Close(); + //context.Database.CloseConnection(); + + return ret; } /// @@ -495,33 +491,31 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl List ret = new List(); var connection = _context?.Database.GetDbConnection(); - DbDataReader reader = null; - DataTable table = null; - using (DbCommand cmd = connection!.CreateCommand()) + DbDataReader? reader = null; + DataTable? table = null; + using DbCommand? cmd = connection!.CreateCommand(); + IDbContextTransaction? tran = _context?.Database.CurrentTransaction; + if (tran != null) { - IDbContextTransaction tran = _context!.Database.CurrentTransaction; - if (tran != null) - { - cmd.Transaction = tran.GetDbTransaction(); - } + cmd.Transaction = tran.GetDbTransaction(); + } - cmd.CommandType = System.Data.CommandType.StoredProcedure; - cmd.CommandText = procName; - cmd.Parameters.AddRange(parameters); - await _context.Database.OpenConnectionAsync(); - reader = await cmd.ExecuteReaderAsync(); + cmd.CommandType = System.Data.CommandType.StoredProcedure; + cmd.CommandText = procName; + cmd.Parameters.AddRange(parameters); + await _context!.Database.OpenConnectionAsync(); + reader = await cmd.ExecuteReaderAsync(); + table = ReaderToDataTable(reader); + ret.Add(table); + while (reader.NextResult()) + { table = ReaderToDataTable(reader); ret.Add(table); - while (reader.NextResult()) - { - table = ReaderToDataTable(reader); - ret.Add(table); - } - reader.Close(); - //context.Database.CloseConnection(); - - return ret; } + reader.Close(); + //context.Database.CloseConnection(); + + return ret; } /// @@ -531,7 +525,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl public int Save() { if (IsClose()) return -1; - return _context.SaveChanges(); + return _context!.SaveChanges(); //以后需要解决并发异常 DbUpdateConcurrencyException } @@ -543,7 +537,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl public async Task SaveAsync() { if (IsClose()) return -1; - return await _context.SaveChangesAsync(); + return await _context!.SaveChangesAsync(); //以后需要解决并发异常 DbUpdateConcurrencyException } @@ -579,7 +573,7 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl //取消当前对象的跟踪 foreach (var e in models) { - _context.Entry(e).State = EntityState.Detached; + _context!.Entry(e).State = EntityState.Detached; } } @@ -587,19 +581,21 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl { if (IsClose()) return; if (model == null) return; - _context.Entry(model).State = EntityState.Detached; + _context!.Entry(model).State = EntityState.Detached; } private DataTable ReaderToDataTable(DbDataReader reader) { - if (IsClose()) return default; + if (IsClose()) return default!; DataTable table = new DataTable(); for (int i = 0; i < reader.FieldCount; i++) { - DataColumn column = new DataColumn(); - column.DataType = reader.GetFieldType(i); - column.ColumnName = reader.GetName(i); + DataColumn column = new() + { + DataType = reader.GetFieldType(i), + ColumnName = reader.GetName(i) + }; table.Columns.Add(column); } @@ -618,11 +614,11 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl private Expression> BuildPrimaryKeyQueryExp(params object[] values) where T : class { - if (values == null || values.Length == 0) return null; + if (values == null || values.Length == 0) return null!; - var keys = _context.Model.FindEntityType(typeof(T)).FindPrimaryKey(); + var keys = _context!.Model.FindEntityType(typeof(T))!.FindPrimaryKey(); var vals = values; - if (keys.Properties.Count > 1) + if (keys!.Properties.Count > 1) { vals = (vals[0] + "").Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries); if (vals.Length != keys.Properties.Count) throw new ArgumentOutOfRangeException("主键参数数量不匹配"); @@ -630,12 +626,12 @@ namespace HealthMonitor.Core.Dal.EfCoreImpl var parameter = Expression.Parameter(typeof(T)); var v = StringHelper.ConvertToType(vals[0] + "", keys.Properties[0].ClrType); - var exp = Expression.Equal(Expression.Property(parameter, keys.Properties[0].PropertyInfo), Expression.Constant(v, keys.Properties[0].ClrType)); + var exp = Expression.Equal(Expression.Property(parameter, keys.Properties[0].PropertyInfo!), Expression.Constant(v, keys.Properties[0].ClrType)); for (int i = 1; i < vals.Length; i++) { v = StringHelper.ConvertToType(vals[i] + "", keys.Properties[i].ClrType); - var right = Expression.Equal(Expression.Property(parameter, keys.Properties[i].PropertyInfo), Expression.Constant(v, keys.Properties[i].ClrType)); + var right = Expression.Equal(Expression.Property(parameter, keys.Properties[i].PropertyInfo!), Expression.Constant(v, keys.Properties[i].ClrType)); exp = Expression.AndAlso(exp, right); } return Expression.Lambda>(exp, parameter); diff --git a/HealthMonitor.Core/Dal/IDataAccessor.cs b/HealthMonitor.Core/Dal/IDataAccessor.cs index 8b046a2..ca8ec21 100644 --- a/HealthMonitor.Core/Dal/IDataAccessor.cs +++ b/HealthMonitor.Core/Dal/IDataAccessor.cs @@ -25,7 +25,7 @@ namespace HealthMonitor.Core.Dal /// /// /// - T GetFirstOrDefault(Expression> expression) where T : class; + T? GetFirstOrDefault(Expression> expression) where T : class; Task GetFirstOrDefaultAsync(Expression> expression) where T : class; @@ -212,7 +212,7 @@ namespace HealthMonitor.Core.Dal /// 存储过程名 /// 参数 /// 返回的数据表集合 - Task> CallProcedureAsync(string procName, params DbParameter[] parameters); + Task?> CallProcedureAsync(string procName, params DbParameter[] parameters); /// /// 提交对数据进行的处理,如无处理返回-1 diff --git a/HealthMonitor.Core/Operator/Redis/GpsCardOperatorRedisManager.cs b/HealthMonitor.Core/Operator/Redis/GpsCardOperatorRedisManager.cs index 4af5b7b..5cbefec 100644 --- a/HealthMonitor.Core/Operator/Redis/GpsCardOperatorRedisManager.cs +++ b/HealthMonitor.Core/Operator/Redis/GpsCardOperatorRedisManager.cs @@ -148,7 +148,7 @@ namespace HealthMonitor.Core.Operator.Redis /// public OperateCacheItem GetDelayOperator(DelayOperateTypeEnum type, string key) { - switch (type) + switch (type) { case DelayOperateTypeEnum.Insert: return RedisHelper.HGet(GpsCardRedisKey_Insert, key); @@ -159,7 +159,7 @@ namespace HealthMonitor.Core.Operator.Redis case DelayOperateTypeEnum.Delete: return RedisHelper.HGet(GpsCardRedisKey_Delete, key); } - return null; + return null!; } /// @@ -242,8 +242,8 @@ namespace HealthMonitor.Core.Operator.Redis public Dictionary GetBulkInsertOperators() { var operators = Remove(DelayOperateTypeEnum.Insert); - PendingInsertOperators = operators; - return operators; + PendingInsertOperators = operators!; + return operators!; } /// @@ -253,8 +253,8 @@ namespace HealthMonitor.Core.Operator.Redis public Dictionary GetBulkUpdateOperators() { var operators = Remove(DelayOperateTypeEnum.Update); - PendingUpdateOperators = operators; - return operators; + PendingUpdateOperators = operators!; + return operators!; } /// @@ -264,8 +264,8 @@ namespace HealthMonitor.Core.Operator.Redis public Dictionary GetBulkDeleteOperators() { var operators = Remove(DelayOperateTypeEnum.Delete); - PendingDeleteOperators = operators; - return operators; + PendingDeleteOperators = operators!; + return operators!; } public void PushFailureInsertOperators(IEnumerable operators) @@ -292,7 +292,7 @@ namespace HealthMonitor.Core.Operator.Redis var results = RedisHelper.StartPipe(p => p.LRange(Failure_GpsCardRedisKey_Insert, 0, count - 1).LTrim(Failure_GpsCardRedisKey_Insert, count, -1)); if (results.Length != 2) throw new InvalidOperationException("Redis pipe occur errors"); - var operators = (results[0] as string[]).Select(e => JsonConvert.DeserializeObject(e)); + var operators = (results[0] as string[])!.Select(e => JsonConvert.DeserializeObject(e)); return operators; } @@ -302,7 +302,7 @@ namespace HealthMonitor.Core.Operator.Redis var results = RedisHelper.StartPipe(p => p.LRange(Failure_GpsCardRedisKey_Update, 0, count - 1).LTrim(Failure_GpsCardRedisKey_Update, count, -1)); if (results.Length != 2) throw new InvalidOperationException("Redis pipe occur errors"); - var operators = (results[0] as string[]).Select(e => JsonConvert.DeserializeObject(e)); + var operators = (results[0] as string[])!.Select(e => JsonConvert.DeserializeObject(e)); return operators; } @@ -312,7 +312,7 @@ namespace HealthMonitor.Core.Operator.Redis var results = RedisHelper.StartPipe(p => p.LRange(Failure_GpsCardRedisKey_Delete, 0, count - 1).LTrim(Failure_GpsCardRedisKey_Delete, count, -1)); if (results.Length != 2) throw new InvalidOperationException("Redis pipe occur errors"); - var operators = (results[0] as string[]).Select(e => JsonConvert.DeserializeObject(e)); + var operators = (results[0] as string[])!.Select(e => JsonConvert.DeserializeObject(e)); return operators; } } diff --git a/HealthMonitor.Core/Pipeline/Aop/AopValueBase.cs b/HealthMonitor.Core/Pipeline/Aop/AopValueBase.cs index 4619dc7..da728eb 100644 --- a/HealthMonitor.Core/Pipeline/Aop/AopValueBase.cs +++ b/HealthMonitor.Core/Pipeline/Aop/AopValueBase.cs @@ -2,7 +2,7 @@ { public abstract class AopValueBase : IValue { - public IValue Next { get; set; } + public IValue Next { get; set; } = default!; public abstract Task Invoke(CacheInterceptorContext context); diff --git a/HealthMonitor.Core/Pipeline/Aop/CacheInterceptorContext.cs b/HealthMonitor.Core/Pipeline/Aop/CacheInterceptorContext.cs index cde62b1..eefc0e2 100644 --- a/HealthMonitor.Core/Pipeline/Aop/CacheInterceptorContext.cs +++ b/HealthMonitor.Core/Pipeline/Aop/CacheInterceptorContext.cs @@ -8,34 +8,34 @@ namespace HealthMonitor.Core.Pipeline.Aop /// /// Aop上下文 /// - public AspectContext AopContext { get; set; } + public AspectContext AopContext { get; set; } = default!; /// /// Aop管道阀门 /// - public AspectDelegate AopDelegate { get; set; } + public AspectDelegate AopDelegate { get; set; } = default!; /// /// 日志 /// - public ILogger Logger { get; set; } + public ILogger Logger { get; set; } = default!; /// /// 被代理方法的返回类型 /// - public Type ReturnType { get; set; } + public Type ReturnType { get; set; } = default!; /// /// 被代理方法的引用的实体类型 /// - public Type EntityType { get; set; } + public Type EntityType { get; set; } = default!; /// /// 被代理方法是否返回列表 /// - public bool IsReturnEnumerable { get; set; } + public bool IsReturnEnumerable { get; set; } = default!; /// /// 被代理方法是否为异步方法 /// - public bool IsAsyncMethod { get; set; } + public bool IsAsyncMethod { get; set; } = default!; /// /// 服务请求关联标识 /// - public string RequestId { get; set; } + public string RequestId { get; set; } = default!; } } diff --git a/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheHisValue.cs b/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheHisValue.cs index 7dd6efb..71cd91c 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheHisValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheHisValue.cs @@ -22,7 +22,7 @@ namespace HealthMonitor.Core.Pipeline.Aop.Delete } var durableManager = context.AopContext.ServiceProvider.GetService(); - var cacheHandler = durableManager.GetCacheHandler(entityType); + var cacheHandler = durableManager?.GetCacheHandler(entityType); //若实体类型不支持缓存,则退出 if (cacheHandler == null || cacheHandler.DurableSecond <= 0) { @@ -30,10 +30,10 @@ namespace HealthMonitor.Core.Pipeline.Aop.Delete return; } - string id = entityType.GetMethod(nameof(IEntity.GetPrimaryKey)).Invoke(param, null) + ""; + string id = entityType!.GetMethod(nameof(IEntity.GetPrimaryKey))?.Invoke(param, null) + ""; if (!string.IsNullOrEmpty(id)) { - string key = durableManager.CalcHistoryCacheKey(entityType, id, paramImei, paramDate); + string key = durableManager!.CalcHistoryCacheKey(entityType, id, paramImei!, paramDate); if (!string.IsNullOrEmpty(key)) { cacheHandler.DeleteEntityCache(key); diff --git a/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheValue.cs b/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheValue.cs index 46bd5a6..5924907 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Delete/DeleteEntityCacheValue.cs @@ -20,7 +20,7 @@ namespace HealthMonitor.Core.Pipeline.Aop.Delete } var durableManager = context.AopContext.ServiceProvider.GetService(); - var cacheHandler = durableManager.GetCacheHandler(entityType); + var cacheHandler = durableManager?.GetCacheHandler(entityType); //若实体类型不支持缓存,则退出 if (cacheHandler == null || cacheHandler.DurableSecond <= 0) { @@ -28,10 +28,10 @@ namespace HealthMonitor.Core.Pipeline.Aop.Delete return; } - string id = entityType.GetMethod(nameof(IEntity.GetPrimaryKey)).Invoke(param, null) + ""; + string id = entityType!.GetMethod(nameof(IEntity.GetPrimaryKey))?.Invoke(param, null) + ""; if (!string.IsNullOrEmpty(id)) { - string key = durableManager.CalcCacheKey(entityType, id); + string key = durableManager!.CalcCacheKey(entityType, id); if (!string.IsNullOrEmpty(key)) { cacheHandler.DeleteEntityCache(key); diff --git a/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryHisValue.cs b/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryHisValue.cs index ee20bb4..fb6e012 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryHisValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryHisValue.cs @@ -32,20 +32,20 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query var entityType = returnType; bool isEnumerable = false; - if (returnType.GetInterface(typeof(IEnumerable).Name) != null) + if (returnType?.GetInterface(typeof(IEnumerable).Name) != null) { entityType = returnType.GenericTypeArguments.FirstOrDefault(); isEnumerable = true; } - context.EntityType = entityType; - context.ReturnType = returnType; + context.EntityType = entityType!; + context.ReturnType = returnType!; context.IsAsyncMethod = isAsync; context.IsReturnEnumerable = isEnumerable; context.RequestId = context.AopContext.Parameters.Length > 3 ? context.AopContext.Parameters[3] + "" : ""; using (var scope = context.Logger.BeginScope(new Dictionary { ["RequestId"] = context.RequestId })) - using (new CustomizeStopWatch($"查询历史aop[{entityType.Name}]", context.Logger)) + using (new CustomizeStopWatch($"查询历史aop[{entityType?.Name}]", context.Logger)) { await InvokeNextAsync(context); } diff --git a/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryValue.cs b/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryValue.cs index 38bbaed..17c6913 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Query/AssertValidQueryValue.cs @@ -32,20 +32,20 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query var entityType = returnType; bool isEnumerable = false; - if (returnType.GetInterface(typeof(IEnumerable).Name) != null) + if (returnType?.GetInterface(typeof(IEnumerable).Name) != null) { entityType = returnType.GenericTypeArguments.FirstOrDefault(); isEnumerable = true; } - context.EntityType = entityType; - context.ReturnType = returnType; + context.EntityType = entityType!; + context.ReturnType = returnType!; context.IsAsyncMethod = isAsync; context.IsReturnEnumerable = isEnumerable; context.RequestId = context.AopContext.Parameters.Length > 1 ? context.AopContext.Parameters[1] + "" : ""; using (var scope = context.Logger.BeginScope(new Dictionary { ["RequestId"] = context.RequestId })) - using (new CustomizeStopWatch($"查询aop[{entityType.Name}]", context.Logger)) + using (new CustomizeStopWatch($"查询aop[{entityType?.Name}]", context.Logger)) { await InvokeNextAsync(context); } diff --git a/HealthMonitor.Core/Pipeline/Aop/Query/GetOrInsertEntityCacheValue.cs b/HealthMonitor.Core/Pipeline/Aop/Query/GetOrInsertEntityCacheValue.cs index 18313f7..ca82f91 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Query/GetOrInsertEntityCacheValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Query/GetOrInsertEntityCacheValue.cs @@ -9,7 +9,7 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query { public class GetOrInsertEntityCacheValue : AopValueBase { - private readonly static MethodInfo M_TaskFromResult = typeof(Task).GetMethod(nameof(Task.FromResult)); + private readonly static MethodInfo M_TaskFromResult = typeof(Task).GetMethod(nameof(Task.FromResult))!; public override async Task Invoke(CacheInterceptorContext context) { @@ -18,11 +18,11 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query var isAsync = context.IsAsyncMethod; var isEnumerable = context.IsReturnEnumerable; var param = context.AopContext.Parameters[0]; - string key = null; + string? key = null; //读取缓存 var durableManager = context.AopContext.ServiceProvider.GetService(); - var cacheHandler = durableManager.GetCacheHandler(entityType); + var cacheHandler = durableManager?.GetCacheHandler(entityType); //若实体类型不支持缓存,则退出 if (cacheHandler == null || cacheHandler.DurableSecond <= 0) { @@ -30,8 +30,8 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query return; } - if (param is string) key = durableManager.CalcCacheKey(entityType, param.ToString()); - else if (param is GeneralParam) key = durableManager.CalcCacheKey(entityType, (param as GeneralParam)); + if (param is string) key = durableManager?.CalcCacheKey(entityType, param.ToString()!); + else if (param is GeneralParam) key = durableManager?.CalcCacheKey(entityType, (param as GeneralParam)!); if (string.IsNullOrEmpty(key)) { @@ -45,7 +45,7 @@ namespace HealthMonitor.Core.Pipeline.Aop.Query { context.Logger.LogInformation($"击中实体缓存{key}"); - returnValue = isEnumerable ? cache : cache.FirstOrDefault(); + returnValue = isEnumerable ? cache : cache.FirstOrDefault()!; if (isAsync) //&& methodReturnType == typeof(Task<>).MakeGenericType(returnType)) { //反射获取Task<>类型的返回值,相当于Task.FromResult(value); diff --git a/HealthMonitor.Core/Pipeline/Aop/Update/UpdateEntityCacheValue.cs b/HealthMonitor.Core/Pipeline/Aop/Update/UpdateEntityCacheValue.cs index d5fd172..d2d0ae8 100644 --- a/HealthMonitor.Core/Pipeline/Aop/Update/UpdateEntityCacheValue.cs +++ b/HealthMonitor.Core/Pipeline/Aop/Update/UpdateEntityCacheValue.cs @@ -28,7 +28,7 @@ namespace HealthMonitor.Core.Pipeline.Aop.Update return; } - string id = entityType.GetMethod(nameof(IEntity.GetPrimaryKey)).Invoke(param, null) + ""; + string id = entityType!.GetMethod(nameof(IEntity.GetPrimaryKey))?.Invoke(param, null) + ""; if (!string.IsNullOrEmpty(id)) { string key = durableManager.CalcCacheKey(entityType, id); diff --git a/HealthMonitor.Core/Query/Extensions/OrderConditionsExt.cs b/HealthMonitor.Core/Query/Extensions/OrderConditionsExt.cs index fdba30c..0ce9f65 100644 --- a/HealthMonitor.Core/Query/Extensions/OrderConditionsExt.cs +++ b/HealthMonitor.Core/Query/Extensions/OrderConditionsExt.cs @@ -14,7 +14,7 @@ namespace HealthMonitor.Core.Query.Extensions try { var parameter = Expression.Parameter(typeof(T)); - Expression propertySelector = Expression.Property(parameter, orderinfo.Key); + Expression propertySelector = Expression.Property(parameter, orderinfo.Key!); //需要用Expression.Convert转换为object表达式,不然值类型的排序会报错 var orderby = Expression.Lambda>(Expression.Convert(propertySelector, typeof(object)), parameter); diff --git a/HealthMonitor.Model/Cache/GpsDevicePerson.cs b/HealthMonitor.Model/Cache/GpsDevicePerson.cs index 854b192..ad5904b 100644 --- a/HealthMonitor.Model/Cache/GpsDevicePerson.cs +++ b/HealthMonitor.Model/Cache/GpsDevicePerson.cs @@ -33,7 +33,7 @@ namespace HealthMonitor.Model.Cache // 用于存储真实值的字段 - private bool gender; + // private bool gender; //public int Age //{ diff --git a/HealthMonitor.Util/Entities/Base/EntityBase.cs b/HealthMonitor.Util/Entities/Base/EntityBase.cs index 7473579..026fe8a 100644 --- a/HealthMonitor.Util/Entities/Base/EntityBase.cs +++ b/HealthMonitor.Util/Entities/Base/EntityBase.cs @@ -109,7 +109,7 @@ namespace HealthMonitor.Util.Entities.Base var limitedProperties = properties.Where(e => e.GetCustomAttributes(typeof(StringLengthAttribute), false).Length > 0).ToList(); limitedProperties.ForEach(e => { - var attr = (e.GetCustomAttributes(typeof(StringLengthAttribute), false) as StringLengthAttribute[])[0]; + var attr = (e.GetCustomAttributes(typeof(StringLengthAttribute), false) as StringLengthAttribute[])![0]; int max = attr.MaximumLength; int min = attr.MinimumLength; var strValue = e.GetValue(this) + ""; diff --git a/HealthMonitor.WebApi/Controllers/Base/GpsCardControllerBase.cs b/HealthMonitor.WebApi/Controllers/Base/GpsCardControllerBase.cs index b80c229..d603ee5 100644 --- a/HealthMonitor.WebApi/Controllers/Base/GpsCardControllerBase.cs +++ b/HealthMonitor.WebApi/Controllers/Base/GpsCardControllerBase.cs @@ -3,33 +3,18 @@ using HealthMonitor.Core.Cache; using HealthMonitor.Core.Dal; using HealthMonitor.Core.Operator; using HealthMonitor.Core.Query; +using HealthMonitor.Core.Query.Extensions; +using HealthMonitor.Util.Common.Operator; using HealthMonitor.Util.Entities.Base; using HealthMonitor.Util.Models; -using HealthMonitor.WebApi.Controllers.Base; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; -using Microsoft.Extensions.Logging; using Newtonsoft.Json; -using System; -using System.Collections.Generic; using System.ComponentModel.DataAnnotations; -using System.Linq; -using System.Threading.Tasks; -using HealthMonitor.Core.Aop; -using HealthMonitor.Core.Cache; -using HealthMonitor.Core.Dal; -using HealthMonitor.Core.Dal.Factory; -using HealthMonitor.Core.Operator; -using HealthMonitor.Core.Query; -using HealthMonitor.Core.Query.Extensions; -using HealthMonitor.Util.Common; -using HealthMonitor.Util.Common.Operator; -using HealthMonitor.Util.Entities.Base; -using HealthMonitor.Util.Models; namespace HealthMonitor.WebApi.Controllers.Base { - [Produces("application/json")] + [Produces("application/json")] [Route("api/GpsCard/[controller]/[action]")] [ApiController] public abstract class GpsCardControllerBase : DefaultControllerBase where T : GpsCardEntityBase @@ -76,13 +61,13 @@ namespace HealthMonitor.WebApi.Controllers.Base /// [HttpPost] [QueryCacheInterceptor] - public async virtual Task GetFirst([FromBody] GeneralParam conditions, [FromHeader] string requestId) + public async virtual Task GetFirst([FromBody] GeneralParam conditions, [FromHeader] string requestId) { AssertModelStateIsValid(); var parser = new QueryExpressionParser(); - var expression = parser.ParserConditions(conditions.Filters); - var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys).Take(1).ToListAsync(); + var expression = parser.ParserConditions(conditions.Filters!); + var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(1).ToListAsync(); return list.Count > 0 ? list[0] : null; } @@ -100,8 +85,8 @@ namespace HealthMonitor.WebApi.Controllers.Base AssertModelStateIsValid(); var parser = new QueryExpressionParser(); - var expression = parser.ParserConditions(conditions.Filters); - var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys).Take(100).ToListAsync(); + var expression = parser.ParserConditions(conditions.Filters!); + var list = await _dataAccessor.GetMany(expression).OrderConditions(conditions.OrderBys!).Take(100).ToListAsync(); return list; } diff --git a/HealthMonitor.WebApi/DbLog/EfCoreLogger.cs b/HealthMonitor.WebApi/DbLog/EfCoreLogger.cs index b4c50b1..6afd268 100644 --- a/HealthMonitor.WebApi/DbLog/EfCoreLogger.cs +++ b/HealthMonitor.WebApi/DbLog/EfCoreLogger.cs @@ -17,49 +17,54 @@ namespace HealthMonitor.WebApi.DbLog _categoryName = categoryName; } - public IDisposable BeginScope(TState state) - { - //ScopeStack.Push(state.ToString()); - return new NoopDisposable(); - } + public IDisposable? BeginScope(TState state) where TState : notnull + { + return new NoopDisposable(); + } + + //public IDisposable BeginScope(TState state) + //{ + // //ScopeStack.Push(state.ToString()); + // return new NoopDisposable(); + //} public bool IsEnabled(LogLevel logLevel) { return logLevel >= EfCoreLogProvider.LogThisAndAbout; } - public void Log(LogLevel logLevel, EventId eventId, TState state, Exception exception, Func formatter) + public void Log(LogLevel logLevel, EventId eventId, TState state, Exception? exception, Func formatter) { switch (logLevel) { case LogLevel.Trace: case LogLevel.Debug: { - var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t'); + var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t'); Serilog.Log.Debug(content); } break; case LogLevel.Information: { - var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t'); + var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t'); Serilog.Log.Information(content); } break; case LogLevel.Warning: { - var content = formatter(state, exception).Replace(Path.DirectorySeparatorChar, '\t'); + var content = formatter(state, exception!).Replace(Path.DirectorySeparatorChar, '\t'); Serilog.Log.Warning(content); } break; case LogLevel.Error: - Serilog.Log.Error(formatter(state, exception)); + Serilog.Log.Error(formatter(state, exception!)); break; case LogLevel.Critical: - Serilog.Log.Fatal(formatter(state, exception)); + Serilog.Log.Fatal(formatter(state, exception!)); break; } } diff --git a/HealthMonitor.WebApi/HttpLog/CustomLoggingScopeHttpMessageHandler.cs b/HealthMonitor.WebApi/HttpLog/CustomLoggingScopeHttpMessageHandler.cs index 307afed..c78c403 100644 --- a/HealthMonitor.WebApi/HttpLog/CustomLoggingScopeHttpMessageHandler.cs +++ b/HealthMonitor.WebApi/HttpLog/CustomLoggingScopeHttpMessageHandler.cs @@ -45,17 +45,17 @@ namespace HealthMonitor.WebApi.HttpLog public static readonly EventId PipelineEnd = new EventId(101, "RequestPipelineEnd"); } - private static readonly Func _beginRequestPipelineScope = + private static readonly Func _beginRequestPipelineScope = LoggerMessage.DefineScope( "HTTP {HttpMethod} {Uri} {CorrelationId}"); - private static readonly Action _requestPipelineStart = + private static readonly Action _requestPipelineStart = LoggerMessage.Define( LogLevel.Information, EventIds.PipelineStart, "Start processing HTTP request {HttpMethod} {Uri} [Correlation: {CorrelationId}]"); - private static readonly Action _requestPipelineEnd = + private static readonly Action _requestPipelineEnd = LoggerMessage.Define( LogLevel.Information, EventIds.PipelineEnd, @@ -64,7 +64,7 @@ namespace HealthMonitor.WebApi.HttpLog public static IDisposable BeginRequestPipelineScope(ILogger logger, HttpRequestMessage request) { var correlationId = GetCorrelationIdFromRequest(request); - return _beginRequestPipelineScope(logger, request.Method, request.RequestUri, correlationId); + return _beginRequestPipelineScope(logger, request.Method, request.RequestUri!, correlationId)!; } public static void RequestPipelineStart(ILogger logger, HttpRequestMessage request) @@ -72,7 +72,7 @@ namespace HealthMonitor.WebApi.HttpLog if (logger.IsEnabled(LogLevel.Trace)) { var correlationId = GetCorrelationIdFromRequest(request); - _requestPipelineStart(logger, request.Method, request.RequestUri, correlationId, null); + _requestPipelineStart(logger, request.Method, request.RequestUri!, correlationId, null); } } diff --git a/HealthMonitor.WebApi/appsettings.test.json b/HealthMonitor.WebApi/appsettings.test.json index 75bd525..4b13777 100644 --- a/HealthMonitor.WebApi/appsettings.test.json +++ b/HealthMonitor.WebApi/appsettings.test.json @@ -35,7 +35,7 @@ "ConnectionStrings": { // 测试环境内网 "GpsCard_Connection_String": "server=172.19.42.40;port=3305;database=gps_card;uid=root;pwd=telpo#1234;CharSet=utf8;MinimumPoolSize=10;MaximumPoolSize=1000;SslMode=none", - "HealthMonitor_Connection_String": "server=172.19.42.40;port=3305;database=health_monitor;uid=root;pwd=telpo#1234;CharSet=utf8;MinimumPoolSize=10;MaximumPoolSize=1000;SslMode=none", + "HealthMonitor_Connection_String": "server=172.19.42.40;port=3305;database=health_monitor;uid=root;pwd=telpo#1234;CharSet=utf8;MinimumPoolSize=10;MaximumPoolSize=1000;SslMode=none" // 测试环境公网 //"GpsCard_Connection_String": "server=139.224.254.18;port=3305;database=gps_card;uid=root;pwd=telpo#1234;CharSet=utf8;MinimumPoolSize=10;MaximumPoolSize=1000;SslMode=none", // "HealthMonitor_Connection_String": "server=139.224.254.18;port=3305;database=health_monitor;uid=root;pwd=telpo#1234;CharSet=utf8;MinimumPoolSize=10;MaximumPoolSize=1000;SslMode=none"