How to add retry logic to ASP.NET Membership Provider for Azure SQL?

萝らか妹 提交于 2019-12-04 11:24:57

I think the best solution today would be to inherit from the providers available in System.Web.Providers (the ASP.NET Universal Providers assembly) and simply inject the retry policy for each public method:

public class MyDefaultProfileProvider : System.Web.Providers.DefaultProfileProvider
{
    private RetryPolicy retryPolicy;

    public MyDefaultProfileProvider()
    {
        var retryStrategy = new Incremental(5, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(2));
        this.retryPolicy = new RetryPolicy<SqlAzureTransientErrorDetectionStrategy>(retryStrategy);
        retryPolicy.Retrying += retryPolicy_Retrying;
    }

    private void retryPolicy_Retrying(object sender, RetryingEventArgs e)
    {
        // Log, whatever...
    }

    public override System.Web.Profile.ProfileInfoCollection GetAllProfiles(System.Web.Profile.ProfileAuthenticationOption authenticationOption, int pageIndex, int pageSize, out int totalRecords)
    {
        int tempTotalRecords = 0;
        var profiles = retryPolicy.ExecuteAction(() =>
        {
            return base.GetAllProfiles(authenticationOption, pageIndex, pageSize, out tempTotalRecords);
        });
        totalRecords = tempTotalRecords;
        return profiles;
    }

    ...
}

To know if it would be legal to reuse the code, you should look at the license.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!