-
-
Notifications
You must be signed in to change notification settings - Fork 122
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
Retrieve instance from scope #549
Comments
Could you create a simple repro, preferably something like a console app that I can run ? |
@seesharper You can reproduce this issue with an example solution: |
@seesharper I added a second example, more complex than the first one. |
For possible other developers, seesharper proposed the following solution:
But in combination with registering the class above also as scoped and passing the serviceFactory explicitly in the compositionRoot:
This is needed because the container can't resolve it own interface (the exception I mentioned in the original post). @seesharper Thank you again for the (very fast) help you provided! |
Hi,
We have a solution where most of the container resolves happen through constructor injection.
But for some resolves, we have code like this:
As you can see, at runtime we need instances resolved from the container based on a Type. Therefor we hold a static reference to the container (ContainerManager.Container) and request an instance with GetInstance(). But that static reference to the container is not the scope.
When making an api call, this specific code (needed for db access) throws an error because the IGenericRepository implementation has the dbContext (scoped) in the constructor. Since the code is requesting an instance of IGenericRepository from the root container, no scope exists.
My question:
Is there a way to retrieve the current scope which we can inject in the constructor? I already tried to add 'Scope scope' as a constructor parameter, hoping it would be resolved but it throws an exception.
If that would be possible, the above code could be rewritten to:
I know this is possible with Unity. The acting container (~scope) registers itself so it can be contructor injected.
Can you help me with this problem?
Thanks!
The text was updated successfully, but these errors were encountered: