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

Posted jerrymouseli

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C# net core程序调试错误集(持续更新)相关的知识,希望对你有一定的参考价值。

C#程序调试错误集


1.依赖注入错误An unhandled exception has occurred while executing the request.

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))。


以上是关于C# net core程序调试错误集(持续更新)的主要内容,如果未能解决你的问题,请参考以下文章