简体   繁体   English

如何在ASP.NET Core中动态解决InstancePerLifetimeScope依赖关系?

[英]How to dynamically resolve InstancePerLifetimeScope dependency in ASP.NET Core?

I have an ASP.NET Core project. 我有一个ASP.NET Core项目。 I want to dynamically resolve a "one instance per request" dependency inside my other dependencies. 我想动态解析其他依赖项中的“每个请求一个实例”依赖项。

I have registered a dependency using Autofac as an InstancePerLifetimeScope dependency in my Startup class: 我已经在我的Startup类中使用Autofac作为InstancePerLifetimeScope依赖项注册了一个依赖项:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    var builder = new ContainerBuilder();
    builder.RegisterType<MyDependency>().AsImplementedInterfaces().InstancePerLifetimeScope();
    return new AutofacServiceProvider(builder.Build());
}

When I use this dependency directly in controller's constructor, it works as expected - it is a new instance per request.: 当我直接在控制器的构造函数中使用此依赖项时,它会按预期工作-它是每个请求的新实例。

public MyController(IMyDependency dependency)
{
}

I want to achieve the same in one of the dependent classes. 我想在其中一个依赖类中实现相同的目的。 Dependency is dynamic, so I want to resolve that from IServiceProvider: 依赖关系是动态的,因此我想从IServiceProvider解决此问题:

public class MyDeepDeepDependency
{
    public MyDeepDeepDependency(IServiceProvider serviceProvider)
    {
        var dep = serviceProvider.GetService(typeof(IMyDependency));
    }
}

However, a "dep" instance is the same across all requests. 但是,所有请求中的“ dep”实例均相同。
I assume there is a new scope created per request, and controller is resolved from a new scope. 我假定每个请求都有一个新的作用域,并且控制器是从一个新的作用域解析的。 When resolving IServiceProvider, I always get a root IServiceProvider instead of a request one. 解决IServiceProvider时,我总是得到一个根IServiceProvider而不是一个请求。

Is there a way to resolve IServiceProvider specific for a request? 有没有一种方法可以解决针对请求的IServiceProvider? I think it's the same as HttpContext.RequestServices in controller, but I don't want to pass the reference down through all my classes. 我认为它与控制器中的HttpContext.RequestServices相同,但是我不想将引用向下传递给所有类。

Is there any other way to resolve a dynamic dependency once per request? 还有其他方法可以解决每个请求一次的动态依赖关系吗?

I ended up injecting IHttpContextAccessor: 我最终注入了IHttpContextAccessor:

public class MyDeepDeepDependency
{
    public MyDeepDeepDependency(IHttpContextAccessor contextAccessor)
    {
        var dep = contextAccessor.HttpContext.RequestServices.GetService(typeof(IMyDependency));
    }
}

暂无
暂无

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

相关问题 ASP.NET Core将DbContext依赖性解析为初始迁移 - ASP.NET Core resolve DbContext dependency to init migrations 如何在 ASP.NET Core 中注册此依赖项? - How to register this dependency in ASP.NET Core? 如何在ASP.NET Core MVC中基于控制器上的属性来解决依赖关系 - How to resolve a dependency based on attributes on a controller in ASP.NET Core MVC 如何从Asp.net核心依赖注入中注册的子类解析父类实例 - How to resolve parent class instance from registered child class in Asp.net core dependency injection ASP.NET Core Web API - 如何在依赖约束之外解析检测到的包版本 - ASP.NET Core Web API - How to resolve Detected package version outside of dependency constraint 如何将InstancePerLifetimeScope对象注入到ASP.NET之外的SingleInstance对象中? - How can I inject an InstancePerLifetimeScope object into a SingleInstance object outside of ASP.NET? 如何使用面向 .net 框架的结构图在 asp.net core 中配置 hangfire 并解决双向依赖 - How to configure hangfire in asp.net core using structuremap targeting .net framework and resolve bi-directional dependency 如何在IHttpcontextAccessor的ASP.NET Core 2.0中使用依赖项注入 - How to use dependency injection in ASP.NET Core 2.0 for IHttpcontextAccessor ASP.NET Core 依赖注入 - 如何创建实例? - ASP.NET Core dependency injection - How to create instances? 如何从 ASP.NET Core 3.1 中的 EntityFrameworkCore 3 中删除依赖项 - How to remove dependency from EntityFrameworkCore 3 in ASP.NET core 3.1
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM