using Microsoft.Extensions.ObjectPool;
static void Main(string[] args)
var poolProvider = new DefaultObjectPoolProvider { MaximumRetained = 2 };
ObjectPool<Channel> pool = poolProvider.Create(new ChannelPooledObjectPolicy());
var connection0 = pool.Get();
var connection1 = pool.Get();
var connection2 = pool.Get();
pool.Return(connection0);
pool.Return(connection1);
pool.Return(connection2);
var connection3 = pool.Get();
public class ChannelPooledObjectPolicy : IPooledObjectPolicy<Channel>
public bool Return(Channel obj)
public class Channel : IDisposable
private static int instanceId = 0;
public int InstanceId { get; } = instanceId++;
Console.WriteLine($"Instance {this.InstanceId} created");
Console.WriteLine($"Instance {this.InstanceId} disposed");