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

时间:2019-06-19 16:30:32   收藏:0   阅读:1399

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


评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!