Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- You can do what I did, and implement your own interface for it. Here is my code:
- public interface IDistributedLockProvider
- { //
- // Summary:
- // Acquires the lock asynchronously, failing with System.TimeoutException if the
- // wait times out using (await myLock.AcquireAsync(...)) { // we have the lock }
- // // dispose releases the lock
- //
- // Parameters:
- // timeout:
- // How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
- //
- // cancellationToken:
- // Specifies a token by which the wait can be canceled
- //
- // Returns:
- // An System.IDisposable "handle" which can be used to release the lock
- IDisposable Acquire(Type Parent, string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
- IDisposable Acquire(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
- //
- // Summary:
- // Acquires the lock asynchronously, failing with System.TimeoutException if the
- // wait times out using (await myLock.AcquireAsync(...)) { // we have the lock }
- // // dispose releases the lock
- //
- // Parameters:
- // timeout:
- // How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
- //
- // cancellationToken:
- // Specifies a token by which the wait can be canceled
- //
- // Returns:
- // An System.IDisposable "handle" which can be used to release the lock
- Task<IDisposable> AcquireAsync(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
- //
- // Summary:
- // Attempts to acquire the lock synchronously. Usage: using (var handle = myLock.TryAcquire(...))
- // { if (handle != null) { /* we have the lock! */ } } // dispose releases the lock
- // if we took it
- //
- // Parameters:
- // timeout:
- // How long to wait before giving up on acquiring the lock. Defaults to 0
- //
- // cancellationToken:
- // Specifies a token by which the wait can be canceled
- //
- // Returns:
- // An System.IDisposable "handle" which can be used to release the lock, or null
- // if the lock was not taken
- bool TryAcquire(string LockName, out IDisposable loc, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken));
- //
- // Summary:
- // Attempts to acquire the lock asynchronously. Usage: using (var handle = await
- // myLock.TryAcquireAsync(...)) { if (handle != null) { /* we have the lock! */
- // } } // dispose releases the lock if we took it
- //
- // Parameters:
- // timeout:
- // How long to wait before giving up on acquiring the lock. Defaults to 0
- //
- // cancellationToken:
- // Specifies a token by which the wait can be canceled
- //
- // Returns:
- // An System.IDisposable "handle" which can be used to release the lock, or null
- // if the lock was not taken
- Task<IDisposable> TryAcquireAsync(string LockName, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken));
- }
- The implementation is pretty simple
- public class DistributedLockProvider : IDistributedLockProvider
- {
- private ToolsDB db;
- public DistributedLockProvider(ToolsDB DB)
- {
- this.db = DB;
- }
- private IDbConnection getConnection
- {
- get
- {
- switch (db.Database.Connection.State)
- {
- case ConnectionState.Closed:
- db.Database.Connection.Open();
- break;
- case ConnectionState.Broken:
- db.Database.Connection.Close();
- db.Database.Connection.Open();
- break;
- case ConnectionState.Connecting:
- case ConnectionState.Executing:
- case ConnectionState.Fetching:
- case ConnectionState.Open:
- break;
- }
- return db.Database.Connection;
- }
- }
- //
- // Summary:
- // Acquires the lock synchronously, failing with System.TimeoutException if the
- // wait times out using (myLock.Acquire(...)) { // we have the lock } // dispose
- // releases the lock
- //
- // Parameters:
- // timeout:
- // How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
- //
- // cancellationToken:
- // Specifies a token by which the wait can be canceled
- //
- // Returns:
- // An System.IDisposable "handle" which can be used to release the lock
- public IDisposable Acquire(Type Type, string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- string lockName = string.Join("_", Type.Namespace, Type.Name, LockName);
- var L = new SqlDistributedLock(lockName, getConnection);
- return L.Acquire(timeout, cancellationToken);
- }
- public IDisposable Acquire(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- var L = new SqlDistributedLock(LockName, getConnection);
- return L.Acquire(timeout, cancellationToken);
- }
- public Task<IDisposable> AcquireAsync(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
- {
- var L = new SqlDistributedLock(LockName, getConnection);
- return L.AcquireAsync(timeout, cancellationToken);
- }
- public bool TryAcquire(string LockName, out IDisposable loc, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
- {
- var L = new SqlDistributedLock(LockName, getConnection);
- loc = L.TryAcquire(timeout, cancellationToken);
- return loc != null;
- }
- public Task<IDisposable> TryAcquireAsync(string LockName, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
- {
- var L = new SqlDistributedLock(LockName, getConnection);
- return L.TryAcquireAsync(timeout, cancellationToken);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment