Entity Framework - SQL Azure Retry Policy

前端 未结 5 1418
春和景丽
春和景丽 2020-12-31 05:20

Could anyone guide me how to implement a retry policy with EF to SQL Azure, please.

5条回答
  •  梦毁少年i
    2020-12-31 06:02

    I am using the Transiet Fault Handling Framework, provided in lue of a better solution by EF team.

    • Add the binary, or the project in the link above to your solution, and add the reference to your project.
    • Instantiate a retry policy with suitable parameters:

        var retryPolicy = new RetryPolicy(
                10, 
                TimeSpan.FromSeconds(0.5), 
                TimeSpan.FromSeconds(2)
        ) { FastFirstRetry = true };
    
    • Use your retry policy object for any atomic work on the context.

        using(var context = new ... )
        {
            ...//Maybe you do something to the database...
            retryPolicy.ExecuteAction(() => context.SaveChanges());
        }
    

提交回复
热议问题