You can try the WCF approach as mentioned or you could implement the connection pooling programmatically.
public interface IService
{
void Execute(Action<IDbConnection> command);
T Execute<T>(Func<IDbConnection, T> command);
}
public sealed class ConnectionManager : IService
{
public const int MaxConnections = 10;
private readonly string _connectionString;
private readonly SemaphoreSlim _workers = new SemaphoreSlim(0, MaxConnections);
public ConnectionManager(string connectionString)
{
_connectionString = connectionString;
}
public void Execute(Action<IDbConnection> command)
{
lock(_workers)
{
using(var connection = new OdbcConnection(_connectionString))
{
connection.Open();
command(connection);
}
}
}
public T Execute<T>(Func<IDbConnection, T> command)
{
lock(_workers)
{
using(var connection = new OdbcConnection(_connectionString))
{
connection.Open();
return command(connection);
}
}
}
}
The SemaphoreSlim
will prevent more than 10 connections from opening assuming the code that is called doesn't try anything sneaky.