-
Notifications
You must be signed in to change notification settings - Fork 317
A design issue with scoping and how it disposes transients without intent or graphing of lifestyles #558
Comments
That's not how scopes work and it's not what the test is doing. Scopes are explicit things in this DI container. You can't register things in a scope, but you can resolve things out of it. The scope is basically a container lifetime. I general, we dispose transient disposable objects, when you resolve out of a scope and that scope dies, so does the transient disposable object. Here's the discussion on transient disposables for more context on how it works #456. In summary resolved instances are per scope are managed per scope/container lifetime.
Can you be more specific? I'm not groking why there are any issues with threading as the scope isn't ambient in this container. |
Aaah, that is where my confusion is coming in. Would it be correct then to assume that the creation of a scope creates a |
and then:
Why is it this the case? Any transients inside of this will be disposed, your docs say not to inject scoped dependencies but I am sure people will think it is OK for transients. Should we not simply dispose the lot? |
I am still not clear on how using nested scopes would work. How would you track state for this? I was looking at the autofac implementation of this and did not notice anything so was a little in the dark as to how I go about this. I have hacked a scope reference onto an AsyncLocal but I dont think this is right and it prevents me from nesting them(so I would have to give it some more thought). I will create child containers though when this happens and see how I go. |
Why on earth would you expose a |
The concept of a |
Singletons are tied to the root container always. They are also only disposed if the container was responsible for creating them.
Scopes aren't nested, they're all top level.
Basically, it's another universe. You can't add registrations to scopes and it isn't a container builder.
Autofact works well because it pretty much mirrors the scope idea in this container. The scope itself is a lifetime and instances are managed there.
The service provider tracks the lifetime of resolved instances within that scope.
Are you talking about the autofac implementation?
There's actually been lots of discussion about our conforming container in the community (just search for conforming container and ASP.NET Core). There's a proposal for an alternate approach for containers that are incompatible: aspnet/Mvc#5403. Here's the example for Castle Windsor https://github.com/dotnetjunkie/Missing-Core-DI-Extensions/tree/master/src/SampleApplication.CastleWindsor |
@davidfowl - I was hitting a fairly high number of WTF's per minute until you sent me this. It is now less so but still there. I still don't believe scopes should override the original intent of the registration. I also dont believe registration should be fault tolerant. Give me time to absorb what you have sent me, I am closing this issue for now but trust me I will be back. Thanks fella. |
Autofac pre-dates Microsoft DI by over 8 years, so I'm pretty sure the relationship goes the other way around: Autofac works well because Microsoft DI pretty much mirrors the scope idea from Autofac. |
@dotnetjunkie:1, @davidfowl:0 ;) |
Hi
I am trying to implement a
IServiceProvider
for the v4 release of Castle Windsor. I invite you to consider this test:Why would a transient would get disposed in a scoped block at the parent level? If the registration API's intent is to register a transient at the parent level then surely the onus is on the calling code to dispose/release it unless it is encapsulated inside an outer type with a scoped lifestyle?
Here is an example of what I would expect:
With some output:
To summarise, I think your test is wrong. It introduces lifecycle
side effects
on transients when running in a scoped block without intent or encapsulation. Now before you get angry and close this issue or try to defend your code please consider the implications of tracking scope state across threads, or parent/child scoping which is impossible with this design. What scares me even more, is a whole heap of IoC implementations just went: "Yip, this is ok!". It isn't. Look forward to your response.Kindest respect.
Gav
The text was updated successfully, but these errors were encountered: