Cannot resolve scoped service from root provider ASP.NET Core


Cannot resolve scoped service from root provider ASP.NET Core

Today in this article, we will cover below aspects,

Issue Description

 

.NET Core runtime gives error,

 

C# 全选
System.InvalidOperationException- Cannot resolve scoped service ” from root provider.
SystemInvalidOperationException Cannot resolve scoped service ConfigurationTheCodeBuzzGuidelinesIUniqueIdService from root provider

 

Resolution

 

The error seems could occur at multiple places depending on your specific implementation while using the services in your code base.

 

As we know one can register the Services as Singleton or Scoped or Transient.

 

While scope service instances are created once per request it is often recommended they are resolved using a scoped instance of Application Builder. Then followed by using a service provider one can resolve the dependencies.

 

You might need to consider the below aspects also before resolving the issue,

 

  • How a lifetime of service instances is registered.
  • How the instance is initialized.
  • How the instance is resolved.

 

I was able to resolve the issue using the scoped service provider instance using the below code base. One can take a similar approach to resolve their specific issues.

C# 全选
var scope = app.ApplicationServices.CreateScope();
var service = scope.ServiceProvider.GetService<IUniqueIdService>();
Resolved SystemInvalidOperationException Cannot resolve scoped service from root provider 1

 

or you can use using the scope for the required DI as below,

 

image 2

 

If you need to resolve services within ConfigureServices() method then one can use the below approach too to resolve the instances,

 

1
2
3
4
services.AddSingleton(provider => new MapperConfiguration(cfg =>
          {
              cfg.AddProfile(new SourceMappingProfile(provider.CreateScope().ServiceProvider.GetService<IEmployeeType>()));
          }).CreateMapper());

 

That’s All!

 

Did I miss anything else in these resolution steps?

Did the above steps resolve your issue? Please sound off your comments below!

Happy Coding !!

 

 

参考地址:https://www.thecodebuzz.com/cannot-resolve-scoped-service-from-root-provider-asp-net-core/

 

版权声明:本文为YES开发框架网发布内容,转载请附上原文出处连接
张国生
上一篇:Devexpress TreeList等一段时间展开就会很卡
下一篇:MEF加载会缺少一些dll,
评论列表

发表评论

评论内容
昵称:
关联文章

Cannot resolve scoped service from root provider ASP.NET Core
.NET Core 深入理解依赖注入 services.AddTransient,services.AddScoped,services.AddSingleton
ASP.NET Core开发者学习路线图
ASP.NET Core 使用 LESS
【推荐】Razor文件编译 ASP.NET Core
asp.net - 在 ASP.NET Core MVC 中嵌套 TagHelper
asp.net core 支持多种身份认证方式
C# ASP.NET Core开发学生信息管理系统(一)
ASP.NET Core MVC中的路由约束
C# ASP.NET Core开发学生信息管理系统(二)
asp.net core MVC路由添加.html伪静态url时报错
Solution for "A Graphics Object cannot be Created from an Image that has an Indexed Pixel Format"
asp.net core 断点调试无法修改代码
ASP.NET MVC和ASP.NET Core MVC中获取当前URL/Controller/Action
ASP.NET Core官网教程,资料查找
asp.net core mvc修改cshtml试图热加载动态更新
.Net Core依赖注入
C# ASP.NET Core开发学生信息管理系统(三)
ASP.NET Core web API中使用Swagger/OpenAPI(Swashbuckle)
.NET5 ASP.NET Core 添加API限流

联系我们
联系电话:15090125178(微信同号)
电子邮箱:garson_zhang@163.com
站长微信二维码
微信二维码