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

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Shed Magnet
  • 211
  • 2
  • 7
  • 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. – arfbtwn Feb 19 '16 at 12:19

3 Answers3

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.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
hemant gautam
  • 791
  • 8
  • 9
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.

ambidexterous
  • 832
  • 12
  • 21
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.