The DistributedLock.FileSystem package offers distributed locks based on file handles/locks. For example:
var lockFileDirectory = new DirectoryInfo(Environment.CurrentDirectory); // choose where the lock files will live
var @lock = new FileDistributedLock(lockFileDirectory, "MyLockName");
await using (var handle = await @lock.TryAcquireAsync())
{
if (handle != null) { /* I have the lock */ }
}
- The
FileDistributedLock
class implements theIDistributedLock
interface. - The
FileDistributedSynchronizationProvider
class implements theIDistributedLockProvider
interface.
Because they are based on files, these locks are used to coordinate between processes on the same machine (as opposed to across machines). In some cases, it may be possible to coordinate across machines by specifying the path of a networked file. However, this should be tested because the network file system may not truly support locking.
FileDistributedLock
s can be constructed either from a base DirectoryInfo
and a name
, which will cause it to create a file based on name
in the specified directory. If you know exactly which file you'd like to lock on, you can pass a FileInfo
instead.
Because of how exclusive file handles work in .NET, the acquire operation cannot truly block. If waiting to acquire a lock that is not available, the implementation will periodically sleep and retry until the lease can be taken or the acquire timeout elapses. Because of this, these locks are maximally efficient when using TryAcquire
semantics with a timeout of zero.
File-based locks have no additional configuration options.