/// <summary> /// Creates a lock with name <paramref name="lockName"/>, using the given /// <paramref name="connectionString"/> to connect to the database via the strategy /// specified by <paramref name="connectionStrategy"/> /// </summary> public SqlDistributedLock(string lockName, string connectionString, SqlDistributedLockConnectionStrategy connectionStrategy) : this(lockName, CreateInternalLock(lockName, connectionString, connectionStrategy)) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException("connectionString"); } }
internal static IInternalSqlDistributedLock CreateInternalLock(string lockName, string connectionString, SqlDistributedLockConnectionStrategy connectionStrategy) { switch (connectionStrategy) { case SqlDistributedLockConnectionStrategy.Default: case SqlDistributedLockConnectionStrategy.Connection: return(new OwnedConnectionDistributedLock(lockName: lockName, connectionString: connectionString)); case SqlDistributedLockConnectionStrategy.Transaction: return(new OwnedTransactionSqlDistributedLock(lockName: lockName, connectionString: connectionString)); case SqlDistributedLockConnectionStrategy.OptimisticConnectionMultiplexing: return(new OptimisticConnectionMultiplexingSqlDistributedLock(lockName: lockName, connectionString: connectionString)); case SqlDistributedLockConnectionStrategy.Azure: return(new AzureSqlDistributedLock(lockName: lockName, connectionString: connectionString)); default: throw new ArgumentException(nameof(connectionStrategy)); } }
/// <summary> /// Creates a semaphore with name <paramref name="semaphoreName"/> that can be acquired up to <paramref name="maxCount"/> /// times concurrently. Uses the given <paramref name="connectionString"/> to connect to the database via the strategy /// specified by <paramref name="connectionStrategy"/> /// </summary> public SqlDistributedSemaphore(string semaphoreName, int maxCount, string connectionString, SqlDistributedLockConnectionStrategy connectionStrategy) : this(semaphoreName, maxCount, name => SqlDistributedLock.CreateInternalLock(name, connectionString, connectionStrategy)) { if (string.IsNullOrEmpty(connectionString)) { throw new ArgumentNullException(nameof(connectionString)); } }