Skip to content

Fixing reading of multiple app context switches from a single AppContextSwitchOverrides configuration field.#3960

Open
tetolv wants to merge 1 commit intodotnet:mainfrom
tetolv:AppContextSwitchOverrides_fix
Open

Fixing reading of multiple app context switches from a single AppContextSwitchOverrides configuration field.#3960
tetolv wants to merge 1 commit intodotnet:mainfrom
tetolv:AppContextSwitchOverrides_fix

Conversation

@tetolv
Copy link

@tetolv tetolv commented Feb 12, 2026

Description

SqlAppContextSwitchManager was unable to correctly parse multiple switches from the single configuration field. In case of such sample configuration:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section type="Microsoft.Data.SqlClient.AppContextSwitchOverridesSection, Microsoft.Data.SqlClient" name="AppContextSwitchOverrides"/>
  </configSections>
  <AppContextSwitchOverrides value="Switch.Microsoft.Data.SqlClient.EnableUserAgent=true;Switch.Microsoft.Data.SqlClient.UseCompatibilityProcessSni=true;System.Globalization.Invariant=false;"/>
</configuration>

following parsing code :

            string[] switches = appContextSwitches.Value?.Split('=', ';');
            ...
            for (int i = 0; i < switches.Length / 2; i++)
            {
                try
                {
                    AppContext.SetSwitch(switches[i], Convert.ToBoolean(switches[i + 1]));
                    SqlClientEventSource.Log.TryTraceEvent("<sc.{0}.{1}|INFO> Successfully assigned the AppContext switch '{2}'={3}.",
                                                           TypeName, methodName, switches[i], switches[i + 1]);
                }
                ...
            }

, on the second iteration would confuse switch key with a value and would try to convert key name to bool, which would of course fail.

One additional fix is for proper handling of configuration field, if it's content (sequence of switch=value pairs) starts, or ends with semicolon.

Testing

Changes seems trivial, bet unittests could be added, if deemed necessary.

@tetolv tetolv marked this pull request as ready for review February 13, 2026 16:38
@tetolv tetolv requested a review from a team as a code owner February 13, 2026 16:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant