Guest User

Untitled

a guest
Dec 17th, 2018
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.98 KB | None | 0 0
  1. You can do what I did, and implement your own interface for it. Here is my code:
  2.  
  3.  
  4. public interface IDistributedLockProvider
  5.     {        //
  6.         // Summary:
  7.         //     Acquires the lock asynchronously, failing with System.TimeoutException if the
  8.         //     wait times out using (await myLock.AcquireAsync(...)) { // we have the lock }
  9.         //     // dispose releases the lock
  10.         //
  11.         // Parameters:
  12.         //   timeout:
  13.         //     How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
  14.         //
  15.         //   cancellationToken:
  16.         //     Specifies a token by which the wait can be canceled
  17.         //
  18.         // Returns:
  19.         //     An System.IDisposable "handle" which can be used to release the lock
  20.         IDisposable Acquire(Type Parent, string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
  21.         IDisposable Acquire(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
  22.         //
  23.         // Summary:
  24.         //     Acquires the lock asynchronously, failing with System.TimeoutException if the
  25.         //     wait times out using (await myLock.AcquireAsync(...)) { // we have the lock }
  26.         //     // dispose releases the lock
  27.         //
  28.         // Parameters:
  29.         //   timeout:
  30.         //     How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
  31.         //
  32.         //   cancellationToken:
  33.         //     Specifies a token by which the wait can be canceled
  34.         //
  35.         // Returns:
  36.         //     An System.IDisposable "handle" which can be used to release the lock
  37.         Task<IDisposable> AcquireAsync(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken));
  38.         //
  39.         // Summary:
  40.         //     Attempts to acquire the lock synchronously. Usage: using (var handle = myLock.TryAcquire(...))
  41.         //     { if (handle != null) { /* we have the lock! */ } } // dispose releases the lock
  42.         //     if we took it
  43.         //
  44.         // Parameters:
  45.         //   timeout:
  46.         //     How long to wait before giving up on acquiring the lock. Defaults to 0
  47.         //
  48.         //   cancellationToken:
  49.         //     Specifies a token by which the wait can be canceled
  50.         //
  51.         // Returns:
  52.         //     An System.IDisposable "handle" which can be used to release the lock, or null
  53.         //     if the lock was not taken
  54.         bool TryAcquire(string LockName, out IDisposable loc, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken));
  55.         //
  56.         // Summary:
  57.         //     Attempts to acquire the lock asynchronously. Usage: using (var handle = await
  58.         //     myLock.TryAcquireAsync(...)) { if (handle != null) { /* we have the lock! */
  59.         //     } } // dispose releases the lock if we took it
  60.         //
  61.         // Parameters:
  62.         //   timeout:
  63.         //     How long to wait before giving up on acquiring the lock. Defaults to 0
  64.         //
  65.         //   cancellationToken:
  66.         //     Specifies a token by which the wait can be canceled
  67.         //
  68.         // Returns:
  69.         //     An System.IDisposable "handle" which can be used to release the lock, or null
  70.         //     if the lock was not taken
  71.         Task<IDisposable> TryAcquireAsync(string LockName, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken));
  72.     }
  73.  
  74.  
  75.  
  76. The implementation is pretty simple
  77.  
  78. public class DistributedLockProvider : IDistributedLockProvider
  79.     {
  80.         private ToolsDB db;
  81.         public DistributedLockProvider(ToolsDB DB)
  82.         {
  83.             this.db = DB;
  84.         }
  85.  
  86.         private IDbConnection getConnection
  87.         {
  88.             get
  89.             {
  90.                 switch (db.Database.Connection.State)
  91.                 {
  92.                     case ConnectionState.Closed:
  93.                         db.Database.Connection.Open();
  94.                         break;
  95.                     case ConnectionState.Broken:
  96.                         db.Database.Connection.Close();
  97.                         db.Database.Connection.Open();
  98.                         break;
  99.                     case ConnectionState.Connecting:
  100.                     case ConnectionState.Executing:
  101.                     case ConnectionState.Fetching:
  102.                     case ConnectionState.Open:
  103.                         break;
  104.                 }
  105.                 return db.Database.Connection;
  106.             }
  107.         }
  108.  
  109.         //
  110.         // Summary:
  111.         //     Acquires the lock synchronously, failing with System.TimeoutException if the
  112.         //     wait times out using (myLock.Acquire(...)) { // we have the lock } // dispose
  113.         //     releases the lock
  114.         //
  115.         // Parameters:
  116.         //   timeout:
  117.         //     How long to wait before giving up on acquiring the lock. Defaults to System.Threading.Timeout.InfiniteTimeSpan
  118.         //
  119.         //   cancellationToken:
  120.         //     Specifies a token by which the wait can be canceled
  121.         //
  122.         // Returns:
  123.         //     An System.IDisposable "handle" which can be used to release the lock
  124.         public IDisposable Acquire(Type Type, string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
  125.         {
  126.             string lockName = string.Join("_", Type.Namespace, Type.Name, LockName);
  127.             var L = new SqlDistributedLock(lockName, getConnection);
  128.             return L.Acquire(timeout, cancellationToken);
  129.         }
  130.         public IDisposable Acquire(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
  131.         {
  132.             var L = new SqlDistributedLock(LockName, getConnection);
  133.             return L.Acquire(timeout, cancellationToken);
  134.         }
  135.  
  136.         public Task<IDisposable> AcquireAsync(string LockName, TimeSpan? timeout = null, CancellationToken cancellationToken = default(CancellationToken))
  137.         {
  138.             var L = new SqlDistributedLock(LockName, getConnection);
  139.             return L.AcquireAsync(timeout, cancellationToken);
  140.         }
  141.  
  142.         public bool TryAcquire(string LockName, out IDisposable loc, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
  143.         {
  144.             var L = new SqlDistributedLock(LockName, getConnection);
  145.             loc = L.TryAcquire(timeout, cancellationToken);
  146.             return loc != null;
  147.         }
  148.  
  149.         public Task<IDisposable> TryAcquireAsync(string LockName, TimeSpan timeout = default(TimeSpan), CancellationToken cancellationToken = default(CancellationToken))
  150.         {
  151.             var L = new SqlDistributedLock(LockName, getConnection);
  152.             return L.TryAcquireAsync(timeout, cancellationToken);
  153.         }
  154.     }
Advertisement
Add Comment
Please, Sign In to add comment