简体   繁体   English

DryIoc-ASP.Net Core-每个请求的范围上下文

[英]DryIoc - ASP.Net Core - Scope context per request

I want to use DryIoc in my application (ASP.Net Core 2.2). 我想在我的应用程序(ASP.Net Core 2.2)中使用DryIoc。 I want to use the same instance of services during execution of the call of one API service. 我想在执行一个API服务的调用期间使用相同的服务实例。 (During Http call / scoped context). (在Http调用期间/作用域上下文中)。 If it was in ASP.Net WebApi, I would like to have a scope per http context. 如果它位于ASP.Net WebApi中,则我希望每个http上下文都有一个作用域。 In the DryIoc documentation, it is possible to use AsyncExecutionFlowScopeContext for WebApi. 在DryIoc文档中,可以将AsyncExecutionFlowScopeContext用于WebApi。

But, with ASP.Net Core, I don't really understand how to use and declare a scope per request. 但是,对于ASP.Net Core,我并不真正了解如何使用和声明每个请求的作用域。

My code is based on this sample: https://github.com/dadhi/DryIoc/tree/master/samples/DryIoc.AspNetCore.Sample 我的代码基于以下示例: https : //github.com/dadhi/DryIoc/tree/master/samples/DryIoc.AspNetCore.Sample

If I tried to resolve a service declared as ScopeService (Reuse.Scoped) I have the issue : Unable to resolve IScopedService IsResolutionCall from Container without Scope. 如果我尝试解析声明为ScopeService(Reuse.Scoped)的服务,则会出现问题:无法从没有范围的容器解析IScopedService IsResolutionCall。

For your information, the declaration of my container is like in the sample: 供您参考,我的容器的声明类似于示例:

public IServiceProvider ConfigureServices(IServiceCollection services)
{
    services.AddMvc()

    // Enables controllers to be resolved by DryIoc, OTHERWISE resolved by infrastructure
    .AddControllersAsServices();

    var container = new Container(rules => rules.With(
        propertiesAndFields: request => 
        request.ServiceType.Name.EndsWith("Controller")
        ? PropertiesAndFields.Properties()(request)
        : null)
        .WithCaptureContainerDisposeStackTrace());

    Container = container;

    return container.WithDependencyInjectionAdapter(services,
        throwIfUnresolved: type => type.Name.EndsWith("Controller"))
        // Your registrations are defined in CompositionRoot class
        .ConfigureServiceProvider<CompositionRoot>();
}

For the registration: 对于注册:

public CompositionRoot(IRegistrator r)
{
    r.Register<ISingletonService, SingletonService>(Reuse.Singleton);
    r.Register<ITransientService, TransientService>(Reuse.Transient);
    r.Register<IScopedService, ScopedService>(Reuse.Scoped);
 }

And if I try to do this in one service: 如果我尝试在一项服务中执行此操作:

var myScopedServvie = Container.Resolve<IScopedService>();

I have the exception: ContainerException: Unable to resolve IScopedService IsResolutionCall from Container without Scope with Rules with {CaptureContainerDisposeStackTrace} with Made={PropertiesAndFields=} 我有一个例外:ContainerException:无法通过带有{CaptureContainerDisposeStackTrace}和Made = {PropertiesAndFields =}的规则的无范围容器从容器解析IScopedService IsResolutionCall

So my question: how can I open a scope for each request of my API? 所以我的问题是:如何为我的API的每个请求打开作用域? I can see some tests where there is: 我可以看到一些测试:

(var scope = container.OpenScope())
...

But I don't know how to use this code in my application. 但是我不知道如何在我的应用程序中使用此代码。

Thanks for your help. 谢谢你的帮助。

A scope will be automatically open per each web request by Asp .Net Core framework, you don't need to bother. Asp .Net Core框架会根据每个Web请求自动打开一个范围,您无需费心。

If you want to test the container setup, then you may OpenScope manually in the respective test, and resolve from the returned scope. 如果要测试容器设置,则可以在相应的测试中手动使用OpenScope ,然后从返回的范围中进行解析。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM