解决方式一: 使用IServiceScopeFactory 创建我们的上下文对象: 解决方式二: 使用IServiceProvider 创建我们的上下文对象: 参考:https://www.thecodebuzz.com/cannot-consume-scoped-service-from-singleton-ihostedservice/
找到原因了,集成测试所测试的 web api 项目的 Program 中使用了AddDbContextPool,而在集成测试的Blog...
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: WebApplication9.Models.BgServerTest': Cannot consume scoped service 'WebApplication9.Models....
描述: InvalidOperationException: Error while validating the service descriptor ‘ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: kafka_api_producer_consumer.TheCodeBuzzConsumer’: Cannot consume scoped service ‘SQLDBService.Context.EmployeeContext’ from singleton...
System.AggregateException:“Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Microsoft.Extensions.Hosting.IHostedService Lifetime: Singleton ImplementationType: WebApplication9.Models.BgServerTest': Cannot consume scoped service 'WebApplication9.Models...
作为Comate,我很乐意帮助你解答关于“cannot resolve scoped service”的问题。下面我将根据你的提示,分点进行回答: 1. 解释什么是“scoped service” 在依赖注入(Dependency Injection, DI)框架中,如ASP.NET Core,服务(Services)通常被分为三种生命周期类型:Singleton、Scoped和Transient。 Scoped Service:Scoped服务在...
在常规的Controller代码中,这通常不会有问题,因为ASP .NET Core会自动为每个请求创建一个新的scope。然而,如果在StartupConfigure方法或者在中间件中直接从IServiceProvider解析DbContext,将会遇到这个问题,因为在这个上下文中并没有创建scope。 为了解决这个问题,需要在LoggingMiddleware 中取消依赖注入context。而是在Invoke...
yyy 被注册为 singleton ,yyy 通过构造函数注入了 xxx ,结果引发该异常,将 yyy 也注册为 scoped ...
Scoped – they’re created once per the request (connection) Transient are created every time you request them from the DI container You should almost never consume scoped service or transient service from a singleton. You should also avoid consuming transient service from a scoped service. ...
.net core 依赖注入,运行报错 Cannot consume scoped 'xxx' service from singleton 'yyy' 这是因为 xxx 的生命周期是 AddScoped 注入的,而 yyy 的生命周期是 AddSingleton ,然后 yyy 这个单例的对象中,它又依赖了xxx 也就是说,单例注入的对象中依赖了 AddScoped 注入的对象。