Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net 4.6 AppContextSwitchOverrides not setting switches declared in config

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

Any ideas on how I get this to work would be greatly appreciated.

like image 741
Shed Magnet Avatar asked Sep 14 '15 14:09

Shed Magnet


1 Answers

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.

like image 118
ambidexterous Avatar answered Sep 20 '22 15:09

ambidexterous