public Owned <TService, IDisposable> Rent() { var lease = _scopePool.Rent(); var service = lease.Resource.ServiceProvider.GetRequiredService <TService>(); var disposable = Disposable.New((this, lease), state => { var(self, lease1) = state; var scope = lease1.Resource; var service1 = scope.ServiceProvider.GetRequiredService <TService>(); if (self._canReuseHandler.Invoke(service1)) { lease1.Dispose(); // Return scope back to the pool } else { scope.Dispose(); // Dispose the scope } }); return(new Owned <TService, IDisposable>(service, disposable)); }