unitycontainer / microsoft-dependency-injection

Unity.Microsoft.DependencyInjection package
Apache License 2.0
63 stars 36 forks source link

Question about ServiceProvider default lifetime #39

Closed kshi219 closed 5 years ago

kshi219 commented 5 years ago

Hi,

What is the motivation is behind using ExternallyControlledLifetimeManager in the constructor of ServiceProvider? Is it intended that the constructed service provider be explicitly injected by the user into the container with a some chosen lifetime?

Thanks!

ENikS commented 5 years ago

It is registered several times by different interfaces. To prevent multiple disposals external lifetime is used

kshi219 commented 5 years ago

I see. So in our use case we ran into an issue where unless we explicitly held onto a reference of the service provider returned by BuildServiceProvider, like by injecting it into our unity container with ContainerControlledLifeTimeManager, the ServiceProvider would be garbage collected and cause runtime issues. Which led to to this question about what the intended usage pattern should be.

ENikS commented 5 years ago

I would need a code sample to verify your scenario and to fix the issue

kshi219 commented 5 years ago

Hi, was away over the weekend, will make one up soon

kshi219 commented 5 years ago

40 seems to have a repro attached that captures the essence of what we saw, will not be making another one up, thanks!