Skip to content

Multiple nested scope registrations #2351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
aspnet-hello opened this issue Jan 1, 2018 · 7 comments
Closed

Multiple nested scope registrations #2351

aspnet-hello opened this issue Jan 1, 2018 · 7 comments
Milestone

Comments

@aspnet-hello
Copy link

From @ghost on Friday, November 13, 2015 3:18:13 PM

When doing multiple nested scopes through IServiceScopeFactory.CreateScope(), you get nested IServiceProvider instances. It doesn't, however, give access to registrations for each scope.

Therefore if you have scopes which are created further down the pipeline (with some service data only accessible from that step in the pipeline, which isn't available at the root), you can't really register services to use in there.

The sought-after structure being:
Scope (root)
- Register (root)
- Resolve (root level)
<- Scope (level 1)
- Register (level 1)
- Resolve (root level + level 1)
<- Scope (level 2)
- Register (level 2)
- Resolve (root level + level 1 + level 2)

I don't know if there's an alternative way of achieving this, but so far the documentation is scarce.

Copied from original issue: aspnet/DependencyInjection#327

@aspnet-hello
Copy link
Author

From @Eilon on Friday, January 22, 2016 5:27:13 PM

Moving this to Backlog as we are not planning to make any changes for this at this time.

@aspnet-hello
Copy link
Author

From @dazinator on Thursday, July 13, 2017 10:57:56 AM

I am looking at how to achieve the following.

As well as an IServiceProvider at the Application and at the RequestServices (scoped) level, I'd like have an IServiceProvider for each separate tenant of my application. The thing I can't figure out is, if I build an IServiceProvider for each tenant, how do I chain this IServiceProvider to a parent IServiceProvider such as request services - so that resolution fallsback from the tenant container to RequestServices?

@aspnet-hello
Copy link
Author

From @dazinator on Thursday, July 13, 2017 11:04:09 AM

It would be really good if I could register some sort of generic delegate into the child container to be invoked as a fallback for whenever a service wasn't registered - in there I could defer resolution to an alternate IServiceProvider (i.e the parent one).. Is this possible?

@t03apt
Copy link

t03apt commented Mar 27, 2018

When doing multiple nested scopes through IServiceScopeFactory.CreateScope(), you get nested IServiceProvider instances. It doesn't, however, give access to registrations for each scope.

For example AutoFac has this implemented.
See: http://autofaccn.readthedocs.io/en/latest/lifetime/working-with-scopes.html#adding-registrations-to-a-lifetime-scope

using(var scope = container.BeginLifetimeScope(
  builder =>
  {
    builder.RegisterType<Override>().As<IService>();
    builder.RegisterModule<MyModule>();
  }))
{
  // The additional registrations will be available
  // only in this lifetime scope.
}

@huysentruitw
Copy link
Contributor

Can we agree that this issue is closely related to #2355 ?

@davidfowl
Copy link
Member

We have no plans to add support to child containers. We recommend you use a 3rd party container for this (like autofac)

@huysentruitw
Copy link
Contributor

Yeah, too hard probably. I tried to create a pull-request for this, but it seems like we would need to refactor a very large part in order to allow this.

@ghost ghost locked as resolved and limited conversation to collaborators Dec 4, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants