Skip to content
This repository has been archived by the owner on Dec 19, 2018. It is now read-only.

Create a scoped service provider for the call to Configure #1106

Merged
merged 2 commits into from
Jun 19, 2017

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Jun 18, 2017

  • This allows scoped dependencies to be injected into the Configure method.
    It means you can resolve the DbContext or any other scoped service without
    the hassle of the CreateScope boiler plate. As a side effect, it also makes
    Startup.Configure a bit more testable.

Inspired by issues like aspnet/Mvc#6407 and code like in our template that needs to create a scope in order to run db init logic.

One downside of this change. If you hold onto scoped services past the Configure method, they'll explode later. While this would have exploded by default in the new templates, it now works.

- This allows scoped dependencies to be injected into the Configure method.
It means you can resolve the DbContext or any other scoped service without
the hassle of the CreateScope boiler plate. As a side effect, it also makes
Startup.Configure a bit more testable.

public class DisposableService : IDisposable
{
public bool Disposed { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Were you going to use this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea, but it's impossible to get to at the instance. I can remove it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixing it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🆙 📅

@davidfowl
Copy link
Member Author

@pakrym one question I had, do we have a DI spec test that create "nested scopes". Scopes aren't nested in our container but will anything blow up if somebody was creating a scope on top of an existing scoped IServiceProvider?

app.ApplicationServices = startup.ConfigureServicesDelegate(serviceCollection);
startup.ConfigureDelegate(app);

var instance = (StartupWithScopedServices)startup.StartupInstance;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could've just registered factory and stored last created object.

@divega
Copy link

divega commented Jun 18, 2017

Should we also have a DI spec test showing IDisposable services get disposed at the right level with nested scopes? E.g. a variation of DisposingScopeDisposesService

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants