-
-
Notifications
You must be signed in to change notification settings - Fork 124
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
ExportFactory throws container disposed exception #380
Comments
…ution requests, issue dadhi#380.
@yallie Hello, To better understand the issue (as well as #378 ) I wanted to get back to the reason behind using In this setup in #378 you will get the singleton |
Hi Maksim!
I believe it all came from the old MEF setup. The app had a container hierarchy: root application container and child request-level containers.
I think that singleton service uses its scoped dependency once, during the initialization. Other singletons use [Export(typeof(ISessionManager)), PartCreationPolicy(CreationPolicy.Shared)]
class SessionManager : ISessionManager
{
[Import]
private ExportFactory<IDbHelper> DbHelperFactory { get; set; }
// this method is always called from a request scope
public void ValidateSession(ISession session)
{
using (var export = DbHelperFactory.CreateExport())
{
// The db helper is put in the current request scope
// it creates the connection, starts the transaction, etc.
// when disposed, it commits the transaction and closes the connection
var db = export.Value;
db.ExecuteSql("select count(*) from sessions where ...");
}
}
} This setup worked with DryIoc v2, v3 and v4.7.3. |
Does it mean that the dependency should be created in the current opened request scope when used first from the singleton.. or the scope itself does not matter, and the new scope maybe opened just to enable the dependency creation (just like the |
I thought it was the behavior of previous DryIoc versions.
Yes, probably :) |
I don't remember in detail in respect of this case, but maybe and there were reasons to change that.
Yes, it is as simple as To explain this, let's get back and see how is decorated singleton is injected under the scope (in pseudocode): scope => new Decorator(
scope.GetRootOrSelf()
.Resolve<IDecorated>()) We are getting the root (top parent container) from the scope first, then we are using it to Resolve a singleton, because its lifespan is match for the lifespan of te container, and not for the shorter scope. In turn, it means the On the other side root => new Decorated(
root.OpenScope(disposeWithParent: true)
.Resolve<AScopedDependency>()) So Btw, the scopes behave differently if you switch to the ambient scope context. |
Hi Maksim, thanks for the explanation!
Ok, so the lifetime of the singleton dependencies matches the singleton itself. Totally makes sense. Anyway, for singletons that access their dependencies more than once, the app uses
You mean |
Yes, I mean this, and I would rather avoid using conext because it is the shared state context where the Current opened scope may be replaced by another one by third party. It means that in For now, I think it is better to revert my change in v4.7.4 until I fully can grasp the consequences and possible alternatives. |
Oh, ok. Wouldn't try use it then :)
The change in v4.7.4 addressed the issue of inconsistent resolution where undecorated service |
Now I am not sure. Would love to not revert - it is always confusing. |
@yallie Is it still an issue or it could be closed? |
Hello Maksim, I believe it's OK now! :) By the way, looks like Appveyor doesn't show all test results. |
@yallie Hi. Thanks for confirming. For some reason AV displays only the XUnit tests and not the NUnit tests, but the latter should be visible in the console output. |
This issue continues #378 topic. Here is the code:
It works with MEF when using container hierarchy (one root application container, and each request creates a child container for non-singleton exports), and it works with DryIoc v4.7.3, but fails with DryIoc v4.7.4.
The text was updated successfully, but these errors were encountered: