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'.
出错图片以下:
code

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'.
出错图片以下:
blog

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

相关文章
相关标签/搜索