.Net 4.6 AppContextSwitchOverrides not setting switches declared in config
Asked Answered
S

3

15

I am trying to set the compatibility switch "Switch.System.Xml.IgnoreEmptyKeySequences" from an entry in an app.config (or web.config) file, but the override appears to be ignored. To remove the possibility of any strange configuration my existing project I have created a brand new .Net 4.6 Web Forms project (and associated test project) in VS2015.

I am following the microsoft guidance for AppContext switches https://msdn.microsoft.com/en-us/library/mt298997(v=vs.110).aspx and https://msdn.microsoft.com/en-us/library/mt270286(v=vs.110).aspx

The app.config file is as follows:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.Xml.IgnoreEmptyKeySequences=true"/>
  </runtime>
</configuration>

The code I am using to read the value is:

bool valueWasFound;
bool valueFromContext;

string switchString = "Switch.System.Xml.IgnoreEmptyKeySequences";

valueWasFound = AppContext.TryGetSwitch(switchString, out valueFromContext);

And yet I consistently get false for both valueWasFound and valueFromContext.

I have tried this with other switch values with the same result.

I find that if I set the switch in code using

AppContext.SetSwitch("Switch.System.Xml.IgnoreEmptyKeySequences", true);

Then the switch is set as anticipated (i.e. I get true for both valueWasFound and valueFromContext).

But I would very much prefer to set this in the App.Config / web.config

Shelf answered 14/9, 2015 at 14:26 Comment(1)
I'm seeing the same behaviour here, but with the Switch.System.IdentityModel.DisableMultipleDNSEntriesInSANCertificate switch. Setting it in Web.config (IIS application) is ignored and TryGetSwitch yields the same result as you have, setting it in code appears to work.Nihi
C
1

I observed similar behavior with following setting in .NNET framework 4.6.2 <AppContextSwitchOverrides value="Switch.System.IO.UseLegacyPathHandling=true"/> Strange thing was that it changed the behavior when running normally but fails when we try to run a UTC.

Cardiograph answered 23/9, 2016 at 7:22 Comment(0)
P
1

Alternative is to add the switch to registry. It does seem to work.

MSDN documentation:

By adding a string value whose name is the name of the switch to the HKLM\SOFTWARE\Microsoft.NETFramework\AppContext key in the registry. Its value must be the string representation of a Boolean that can be parsed by the Boolean.Parse method; that is, it must be "True", "true", "False", or "false". If the runtime encounters any other value, it ignores the switch.

In my case I did this

Value name: Switch.System.IdentityModel.DisableMultipleDNSEntriesInSANCertificate

Value data: true

enter image description here

The downside is that it applies to all apps on the machine. In my book registry settings are even less preferred than something hardcoded in the code, so I will stick with the programmatical approach.

This trick is still neat though just to quickly try something out.

Ptisan answered 11/7, 2017 at 16:14 Comment(0)
T
0

You should be modifying App.config in your solution explorer. This file will get renamed to YourAppName.exe.config and should be in your binaries folder. Removing or renaming that file will cause this switch override to have no effect. (You can add that file manually after building as well)

I believe you must've put App.config next to your exe manually which will have improper name.

Theodoratheodore answered 29/9, 2015 at 22:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.