"scoped instances" in Microsoft.Extensions.ObjectPool.DependencyInjection #4515
Replies: 3 comments 2 replies
-
@sebastienros @geeknoid can you help? |
Beta Was this translation helpful? Give feedback.
-
Re "is the implementation incomplete" — I thought maybe this library defines some class like public sealed class PooledObject<TService> : IDisposable
{
public TService Instance { get; }
public void Dispose(); // returns to the pool
} and then registers PooledObject<TService> as a scoped service so that it can be injected to constructors in a service scope, and the object is returned to the pool when the scope is disposed. ObjectPoolServiceCollectionExtensions doesn't register anything like that though, and the library never calls ObjectPool<TService>.Get or ObjectPool<TService>.Return. |
Beta Was this translation helpful? Give feedback.
-
Maybe it should say /// <summary>
/// Adds an <see cref="ObjectPool{TService}"/> from which instances of <typeparamref name="TService"/> can be borrowed.
/// </summary> |
Beta Was this translation helpful? Give feedback.
-
The documentation of the Microsoft.Extensions.ObjectPool.ObjectPoolServiceCollectionExtensions.AddPooled methods mentions "scoped instances":
However, this is the only mention of "scoped" in the Microsoft.Extensions.ObjectPool.DependencyInjection project. Those methods register the ObjectPool<TService> service as a singleton; and when the DefaultObjectPool<TService> instance is created, its DependencyInjectionPooledObjectPolicy<TService, TImplementation> captures a reference to the IServiceProvider, which presumably is the root rather than any IServiceScope.ServiceProvider. If a scoped service were somehow injected to a pooled object, and the object kept a reference to the service, then it would not be safe to return the pooled object to the singleton pool, where it could outlive the service.
What then is the meaning of "scoped instances" here; is it just a documentation error, or is the implementation incomplete?
Beta Was this translation helpful? Give feedback.
All reactions