目录

  • c#程序调试错误集
  • 1.依赖注入错误an unhandled exception has occurred while executing the request.
      • 1.1.1原因是net core在调用valuecontroller的时候,发现unitofwork没有进行依赖注入。
      • 1.2.1 原因是net core在调用unitofwork的时候,发现ipboxcontext没有进行依赖注入。

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