Open Closed

Implementation Of Abp Distributed Locking Document #6134


User avatar
0
Teknosol created
  • ABP Framework version: 7.1.1
  • UI Type: **Angular **/ MVC / Blazor WASM / Blazor Server
  • Database System: EF Core (SQL Server, Oracle, MySQL, PostgreSQL, etc..) / MongoDB
  • Tiered (for MVC) or Auth Server Separated (for Angular): yes/no
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

Hi, To include the Lock system in our project, we followed the procedures in the Distributed Locking document provided to us by ABP. https://docs.abp.io/en/abp/latest/Distributed-Locking The following operations were performed; 1-First of all, we added the Volo.Abp.DistributedLocking package to our project from the Nuget Package Manager. https://prnt.sc/v60w6NZd2fG1 2- In the next step, the necessary configuration adjustments were made in the HttpApiHostModule.cs class in the project. https://prnt.sc/g-WBBlUUEMye To prevent errors on the RedisDistributedSynchronizationProvider class, “DistributedLock.Redis” package was downloaded with version 1.0.2. 3- In the next step, we implemented the “IAbpDistributedLock” interface in our method. https://prnt.sc/TNF6wAJeo-i9 However, when we run the above method and want to test Distributed Lock, the method does not work correctly and we get the following error; https://prnt.sc/1PipGiS1htFc


11 Answer(s)
  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I will check it

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I could not reproduce the problem, could you share a test project with me? I will check it out. my email is shiwei.liang@volosoft.com

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    I can't run your project because it needs remote service.

    Then I moved the test code to OnPreApplicationInitializationAsync method, and it worked as expected. I think this may be related to your Redis version. I'm using the version 7.0.4

  • User Avatar
    0
    Teknosol created

    Hello, The problem continues. Could you please let me know a time you have available? We want to demonstrate the problem on the spot by organizing meetings such as Zoom, Teams, Meet.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Ok, join the meeting: https://us05web.zoom.us/j/84476788133?pwd=OexABNPrNizuDNTc4uE9Aswo60Wnun.1

  • User Avatar
    0
    Teknosol created

    What hours are you available today? Let's schedule a meeting for today.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    I'm available now.

  • User Avatar
    0
    Teknosol created

    Hi,

    I'm available now.

    I scheduled a meeting for 20 minutes later. The Meeting Link should have arrived in your e-mail address. Thank you.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Will it work if you install the StackExchange.Redis version 2.7.4 to the HttpApi.Host project?

  • User Avatar
    0
    Teknosol created

    Hi, Thank you for your support, the lock system is working. However, we made it usable as I shared in the screenshot. We need to use it in all entities in our project as create and update, which causes code repetition. I wonder if you have a solution for this?

    Note: The reason we need this code is that when the method is run more than once at the same time, it registers to the database without going through validations.

  • User Avatar
    0
    liangshiwei created
    Support Team Fullstack Developer

    Hi,

    Do not throw exceptions, this may cause the lock to not be released

    We need to use it in all entities in our project as create and update, which causes code repetition. I wonder if you have a solution for this?

    You can create a generic domain service:

    public class LockEntityManager<TEntity> where TEntity: .... , ITransientDependency
    {
        private readonly IRepository<TEntity> _reopsitory;
        private readonly IAbpDistributedLock _distributedLock;
        private readonly IEntityCheck<TEntity> _entityCheck;
        
        ...
        
        public async Task<TEntity> CreateAsync(TEntity entity, string lockName)
        {
           await using (var handle = 
                             await _distributedLock.TryAcquireAsync(lockName))
            {
                if (handle != null)
                {
                   await _entityCheck.CreateValidAsync(entity);
                   .....
                }
            }   
        }
        
        public async Task<TEntity> UpdateAsync(TEntity entity, string lockName)
        {
           .....
        }
        
    }
    
    public interface IEntityCheck<TEntity> where TEntity: ....
    {
         Task CreateValidAsync(Tentity entity);
         
         Task UpdateValidAsync(Tentity entity);
    }
    
    public class TestEntityCheck : IEntityCheck<Test>, ITransientDependency
    {
          .....
    }
    
    
    
    public class TestAppService : ...
    {
          private readonly LockEntityManager<Test> _testEntityManager;
          
          ....
    }
    
Made with ❤️ on ABP v9.1.0-preview. Updated on December 05, 2024, 12:19