When I instantiate the SemaphoreSlim class I'd like to use a configuration key for the initialCount argument so as if the value needs to change we do not need to do a complete rebuild.
My current implementation is effectively:
public class Handler
{
private static SemaphoreSlim pool;
private static readonly object lockObject = new();
private static bool isInitialised;
public Handler(IConfiguration configuration)
{
if(isInitialised) return;
int poolSize = configuration.GetValue("PoolSize", 3);
lock(lockObject)
{
pool ??= new SemaphoreSlim(poolSize);
isInitialised = true;
}
}
}
I feel a little uncomfortable with this approach and I wouldn't say I am hugely confident that it is the right solution.
Is there a better way to do this?