Dotnet: AppContextSwitchOverrides doesn't apply for Asp.Net project

Created on 25 Jul 2018  路  5Comments  路  Source: microsoft/dotnet

I have an Asp.Net project which I'm trying to upgrade it to .NET 4.7.2. In order to keep the old behavior of .NET because the "new" one breaks a lot of things, I find a need to use AppContextSwitchOverrides

Problem here is that putting the switches in web.config doesn't work at all. Asp.Net simply ignores the setting. I'm able to verify that the same setting does work for Console apps.

Searching on Stackoverflow gave me many results in which developers ran into the same problem without any proper answers: https://stackoverflow.com/search?q=AppContextSwitchOverrides

There are two problems for me here: the first is that I prefer using web.config over using code to set the switches because of its flexibility. I can change or add more switches without recompiling my app.

The second problem is that I want to set the "Switch.System.IdentityModel.DisableCngCertificates" switch to true so that System.IdentityModel will use the "old" behavior:

image

However, even when I set the switch in code in Application_Start, System.IdentityModel.LocalAppContextSwitches.DisableCngCertificates is still false.

What should I do to get those switches work for me?

Most helpful comment

This worked fine for us, notice ServiceModel vs. IdentityModel:

<add key="AppContext.SetSwitch:Switch.System.IdentityModel.DisableCngCertificates" value="true" />

Got the key from the Reference source.

All 5 comments

For web.config you should use this syntax:

<appSettings>
   <add name="AppContext.SetSwitch:System.Xml.DontThrowOnInvalidSurrogatePairs" value="true" />
</appSettings>

I don't believe we have good documentation about how to use the AppContext overrides from web.config -- I will follow-up on that.

Thank you :) I used

  <appSettings>
    <add key="AppContext.SetSwitch:Switch.System.ServiceModel.DisableCngCertificates" value="true" />
  </appSettings>

and used

AppContext.TryGetSwitch("Switch.System.ServiceModel.DisableCngCertificates", out bool isEnabled);

to check. The isEnabled parameter is set to true correctly now. However, checking System.IdentityModel.LocalAppContextSwitches.DisableCngCertificates value using the Watch window in debug mode still gives me "false". That's weird.

This worked fine for us, notice ServiceModel vs. IdentityModel:

<add key="AppContext.SetSwitch:Switch.System.IdentityModel.DisableCngCertificates" value="true" />

Got the key from the Reference source.

@stroborobo thank you! You are my hero. Somehow I looked at the wrong source file and used that ServiceModel instead of IdentityModel 馃槩

@thuannguy can you describe the "old" and "new" behaviour, or point me towards a reference so I can read up.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

srofed picture srofed  路  8Comments

jonreis picture jonreis  路  9Comments

omid3098 picture omid3098  路  9Comments

Redsandro picture Redsandro  路  11Comments

pingzing picture pingzing  路  13Comments