Finn Krewer
Finn Krewer

Reputation: 63

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

We have a database of ASP.net sqlMembershipProvider based users on an Azure SQL database. It has become apparent that the out of the box sqlMembershipProvider 4.0 does not have the required retry logic for Azure SQL connections which can drop out due to throttling or can expire.

It is possible to implement our own membership provider that has this functionality but it would have to be exactly the same database interactions as the standard sqlMembershipProvider 4.0 in order to work with the existing users in our database. However for that it would require looking into the source of the original sqlMembershipProvider 4.0, this code has not been released into the open since version 2.0 so my question is:

What is the lowest effort way to get retry logic into the sqlMembershipProvider? And or would that be reflecting the code of sqlMembershipProvider 4.0 in System.web.security.sqlMembershipProvider and creating a custom membershipProvider that has the same functionality as the sqlMembershipProvider but uses retry logic such as that of Microsofts TransientFaultHandling ReliableSqlConnection? Would it be legal to do such a thing (reflecting and creating similar code but with added functionality) given Microsoft's license for asp.net libraries eg. System.Web?

Upvotes: 4

Views: 578

Answers (1)

Sandrino Di Mattia
Sandrino Di Mattia

Reputation: 24895

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.

Upvotes: 3

Related Questions