简体   繁体   English

ServiceProvider.GetService 为瞬态实例工厂中的作用域服务返回相同的实例

[英]ServiceProvider.GetService returns the same instance for scoped service in transient instance factory

In asp.net core 3.1 project, I have the following controller (ctor):在 asp.net core 3.1 项目中,我有以下控制器(ctor):

public MyController(IMyService myService) { }

The IMyService interface and MyService IMyService接口和MyService

public interface IMyService { }

public class MyService : IMyService { }

Where the IService is registered in my Startup.cs as follows: IService在我的Startup.cs注册如下:

services.AddScoped<MyService>();
services.AddTransient(sp =>
                      {
                          var service = sp.GetRequiredService<MyService>();
                          // do something on the service...
                          return service as IMyService;
                      });

However, when I'm doing multiple requests, I see that only the first request is initiating a new instance of the MyService (ie, when I'm putting a breakpoint in the MyService constructor, it will break only on the first request).但是,当我执行多个请求时,我看到只有第一个请求正在启动MyService的新实例(即,当我在MyService构造函数中放置断点时,它只会在第一个请求时中断)。

The thing that bugs me the most, is that if I'm changing the services.AddTransient(sp => ... line in the Startup.cs file to services.AddScoped(sp => ... - everything works and the instance is initiated every request.最让我烦恼的是,如果我将Startup.cs文件中的services.AddTransient(sp => ...行更改为services.AddScoped(sp => ... - 一切正常,实例每次请求都会发起。

How can I fix the issue so when I'm getting services inside the instance factory, it will be gathered by the correct scope of the request?我该如何解决这个问题,以便当我在实例工厂中获取服务时,它将由请求的正确范围收集?

Following @GuruStron comment, I found out that I have the following line in my Program.cs , although I'm not really using Unity container in my project:在@GuruStron 评论之后,我发现我的Program.cs有以下行,尽管我并没有在我的项目中真正使用 Unity 容器:

.UseUnityServiceProvider()

after I removed this line, everything works as expected.删除此行后,一切都按预期工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ServiceProvider.GetService() 显示错误 - ServiceProvider.GetService() showing error 由于 serviceProvider.GetService,无法添加 singleton 服务<irabbitmqconsumer> () 返回 null</irabbitmqconsumer> - Unable to add a singleton service due to serviceProvider.GetService<IRabbitMQConsumer>() returning null 无论如何在.net核心中使用带有反射的ServiceProvider.GetService()? - is there anyway to use ServiceProvider.GetService() with reflection in .net core? 服务提供者.GetService<type> 每次使用都抛出异常</type> - ServiceProvider.GetService<Type> throw exception for every usage 作用域/瞬态依赖项注入确保返回接口和实现的相同实例 - Scoped/transient dependency injection ensuring that same instance for interface and implementation is returned .Net Core 2.0控制台程序在运行`serviceProvider.GetService&#39;时具有空值 <Application> ();`? - .Net core 2.0 console program got null value when running `serviceProvider.GetService<Application>();`? 返回通用实例的工厂方法 - Factory method that returns generic instance ASP.NET Core DI:如果将作用域服务注册为服务类型和实现类型,则解析相同的实例 - ASP.NET Core DI: Resolve same instance if scoped service is registered both as service type and implementation type 工厂模式返回相同实例 - Factory pattern returning same instance Blazor 服务器是否可以在中间件中注入作用域服务并在组件中注入相同的实例? - Is it possible in Blazor Server to inject scoped service in middleware and inject that same instance in component?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM