asp.net-coreado.netretry-logicdata-accessmicrosoft-data-sqlclient

Microsoft.Data.SqlClient SqlConfigurableRetryLogicConnection in Config Not Applying


I am trying to use an app.config file to apply default Retry Logic to Microsoft.Data.SqlClient following the steps MS advise but when I break into the code and look at the RetryLogicProvider that has been applied to a new instance of SqlConnection it is just the default provider it has ignored the config file settings.

We have a .NET Core Web App targeting .NET 6.0 that references a .NET 6.0 Class library that contains the data access code and references the latest 5.1.1 Microsoft.Data.SqlClient package. We use a mixture of classic ADO.NET and Dapper although the code I am testing with is classic ADO.NET.

I have tried adding the below to both the Web.Config and a new App.Config in the data access class library. Neither seem to have any effect. MS did mention an AppContext safety switch however this seemed to only be required for older versions of SqlClient.

To test if the retry logic is working I am taking the target DB offline and using Profiler to view connection attempts according to the Retry logic. Instead I am just seeing an immediate failure, perhaps my testing methodology is wrong?

<configuration>
    <configSections>
        <section name="SqlConfigurableRetryLogicConnection"
                type="Microsoft.Data.SqlClient.SqlConfigurableRetryConnectionSection, Microsoft.Data.SqlClient"/>
        <section name="SqlConfigurableRetryLogicCommand"
                type="Microsoft.Data.SqlClient.SqlConfigurableRetryCommandSection, Microsoft.Data.SqlClient"/>
    </configSections>
    <SqlConfigurableRetryLogicConnection retryMethod="CreateExponentialRetryProvider"
                    numberOfTries="5" deltaTime="00:00:03" maxTime="00:00:45"/>
    <SqlConfigurableRetryLogicCommand retryMethod="CreateIncrementalRetryProvider"
                numberOfTries="4" deltaTime="00:00:02" maxTime="00:00:30"/>
</configuration>

ADO.NET code below

using (var conn = new SqlConnection(Configuration.ConnectionString))
        using (var cmd = new SqlCommand("User_GET", conn))
        {
            //conn.RetryLogicProvider checking this when breakpoint fires
            conn.Open();

Solution

  • You don't have to add the content to web.config ,just move app.config file to your .NET Core Web App project(startup project)

    I just tried with your .config file :

    enter image description here

    When I debug:

    enter image description here

    The parameters are correspond to

    <SqlConfigurableRetryLogicConnection retryMethod="CreateExponentialRetryProvider"
                        numberOfTries="5" deltaTime="00:00:03" maxTime="00:00:45"/>
    

    in your .config file