Layered Solution: Distributed Locking

Distributed locking is a mechanism that enables multiple instances of an application to coordinate and synchronize access to shared resources. It is particularly useful in scenarios where multiple instances need to ensure that only one instance can access a resource at a time. For more information, refer to the Distributed Locking document.

Distributed Locking in Layered Solutions

The layered solution template does not include the distributed lock package by default unless it's a Tiered or Public Website application. To use distributed locking, you can add the Volo.Abp.DistributedLock package to your project. This package provides a distributed lock mechanism that works with Redis. You can inject the IAbpDistributedLock service to acquire and release locks. Below is an example of using distributed locking in your application:

public class MyService : ITransientDependency
{
    private readonly IAbpDistributedLock _distributedLock;

    public MyService(IAbpDistributedLock distributedLock)
    {
        _distributedLock = distributedLock;
    }
    
    public async Task MyMethodAsync()
    {
        await using (var handle = await _distributedLock.TryAcquireAsync("MyLockName"))
        {
            if (handle != null)
            {
                // your code that access the shared resource
            }
        }   
    }
}

Contributors


Last updated: December 31, 2024 Edit this page on GitHub

Was this page helpful?

Please make a selection.

To help us improve, please share your reason for the negative feedback in the field below.

Please enter a note.

Thank you for your valuable feedback!

Please note that although we cannot respond to feedback, our team will use your comments to improve the experience.

In this document
Community Talks

Layered vs Modular vs Microservices... Which one is best for you?

09 Jan, 17:00
Online
Register Now
Mastering ABP Framework Book
Mastering ABP Framework

This book will help you gain a complete understanding of the framework and modern web application development techniques.

Learn More