C# net core程序调试错误集(持续更新)

C#程序调试错误集

1.依赖注入错误System.InvalidOperationException: Unable to resolve service for type 'xxx' while attempting to activate 'xxx'.

1.1 出错现象

System.InvalidOperationException: Unable to resolve service for type 'IBMS.Infrastruct.UoW.UnitOfWork' while attempting to activate 'IBMS.WEBAPI.Controllers.ValueController'.
出错图片如下:

1.1.1原因是net core在调用ValueController的时候,发现UnitOfWork没有进行依赖注入。

1.2 出错现象

System.InvalidOperationException: Unable to resolve service for type 'IBMS.Infrastruct.Context.IPBoxContext' while attempting to activate 'IBMS.Infrastruct.UoW.UnitOfWork'.
出错图片如下:

1.2.1 原因是net core在调用UnitOfWork的时候,发现IPBoxContext没有进行依赖注入。

1.3 解决方法

在startup.cs中的ConfigureServices方法中进行依赖注入

      services.AddDbContext<IIPBoxContext, IPBoxContext>(options =>
      options.UseMySql(Configuration.GetConnectionString("MySqlConnection")));
      services.AddScoped<IIPBoxRepository, IPBoxRepository>();
      services.AddScoped(typeof(UnitOfWork));//注入工作单元
      services.AddScoped(typeof(IPBoxContext));

注意:IPBoxContext进行AddDbContext注入数据上下文之后,仍需要注入services.AddScoped(typeof(IPBoxContext))。

2. 未创建实例错误

2.1 出错现象Object reference not set to an instance of an object.

出错图片如下:

2.1.1 原因是没有创建数据库上下文对象

2.2 出错现象System.ArgumentNullException: Value cannot be null.

出错图片如下:

2.2.1 原因是没有创建数据库上下文对象

2.3 解决方法

在服务层创建一个实例的仓储,并传入其对应的数据上下文。

    public class IPBoxServices : ServicesBase<IPBox>, IIPBoxServices
    {
      public IPBoxContext DbContext { get; set; } = null;
      public IPBoxServices(IPBoxContext dbContext)
      {
          DbContext = dbContext;
      }
    
      #region 字段
      private IPBoxRepository _IPBoxRepository = null;
      #endregion
    
      #region 操作类属性
      public IPBoxRepository IPBoxRepository => _IPBoxRepository ?? (_IPBoxRepository = new IPBoxRepository(DbContext));
      #endregion

    }

备注:这里最好是在RepositoryBase里直接通过数据库工厂模式创建一个数据库的上下文,这样不会让仓储层泄露到应用层。当然以上方法基本没什么很大影响。

3.传入EF Core 拉姆达(lambda)表达式为空报错

3.1 出错现象

System.ArgumentNullException: Value cannot be null. at lambda_method(Closure , Object , Object[] )
出错图片如下

3.1.1 原因

获取列表数据时传入lambda表达式为空。具体如下。
AlarmList = _alarmServices.GetList();

3.2解决方法

当lambda表达式为空时,直接返回列表数据。问题得到有效解决

           if (predicate == null)
           {
               return  _dbSet.ToList();
           }

整个代码块如下:

        public List<TEntity> GetList(Expression<Func<TEntity, bool>> predicate = null, string ordering = "", bool isNoTracking = true)
        {
           if (predicate == null)
           {
               return  _dbSet.ToList();
           }
            var data = isNoTracking ? _dbSet.Where(predicate).AsNoTracking() : _dbSet.Where(predicate);
            if (!string.IsNullOrEmpty(ordering))
            {
                data = data.OrderByBatch(ordering);
            }
            return data.ToList();
        }

4.泛型类约束出错

4.1 什么是泛型类约束

where TEntity : class 

表示TEntity必须是一个类。

4.2 出错现象

错误 CS0311 类型“TEntity”不能用作泛型类型或方法“DbContextExtensions.UpdatePerfect(DbContext, params TEntity[])”中的类型参数“TEntity”。没有从“TEntity”到“IBMS.Domain.Core.Models.IEntity”的隐式引用转换。

4.2.1 解释

仓储层的TEntity不能用做扩展层的TEntity。因为一个是需要包含IEntity,而一个是没有包含

4.3 出错原因

实际的Entity调用中用了IEntity字段,但是仓储层中没有包含IEntity。

4.4 解决

仓储层包含IEntity的泛型约束即可。

where TEntity : class,IEntity

问题得到圆满解决。

5.无法将'System.Boolean'类型转化到 'System.SByte'类型

5.1 错误提示

System.InvalidCastException: Unable to cast object of type 'System.Boolean' to type 'System.SByte'.

5.2 错误原因

数据库mysql设置了tinyint(1)类型(mysql会自动将其转化成bool类型),而C#中是byte类型。导致bool与byte (tinyint)之间无法转化。

5.3 解决方法

在mysql连接字符串中加入TreatTinyAsBoolean=false;这样,mysql不会将tinyint转化成bool类型。即可解决问题

6.log4Net已经没法满足Net Core 3.1了,可以生成包,但是依赖过低了,无法打包发布。智能能够生成VS工具里面运行。

6.1 错误提示

6.2 错误原因

依赖包过低,该插件也没在维护。

6.3 解决方法

换用Nlog。

原文地址:https://www.cnblogs.com/JerryMouseLi/p/11052009.html