AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) AppContext.SetSwitch(String, Boolean) Method

定义

设置开关的值。Sets the value of a switch.

public:
 static void SetSwitch(System::String ^ switchName, bool isEnabled);
public static void SetSwitch (string switchName, bool isEnabled);
static member SetSwitch : string * bool -> unit
Public Shared Sub SetSwitch (switchName As String, isEnabled As Boolean)

参数

switchName
String String String String

开关的名称。The name of the switch.

isEnabled
Boolean Boolean Boolean Boolean

开关的值。The value of the switch.

异常

示例

下面的代码行设置一个名Switch.AmazingLib.ThrowOnExceptiontrue的开关,后者启用了旧行为。The following line of code sets a switch named Switch.AmazingLib.ThrowOnException to true, which enables a legacy behavior. 然后,库可以通过调用TryGetSwitch方法来检查库使用者是否已设置开关的值。The library can then check whether a library consumer has set the value of the switch by calling the TryGetSwitch method.

AppContext.SetSwitch("Switch.AmazingLib.ThrowOnException", true);
AppContext.SetSwitch("Switch.AmazingLib.ThrowOnException", True)

注解

AppContext类使库编写器可为其用户提供新功能的统一退出机制。The AppContext class enables library writers to provide a uniform opt-out mechanism for new functionality for their users. 它在组件之间建立松耦合的协定,以便与选择退出请求进行通信。It establishes a loosely-coupled contract between components in order to communicate an opt-out request. 对现有功能进行更改时,此功能通常很重要。This capability is typically important when a change is made to existing functionality. 相反,已有新功能隐式选择加入。Conversely, there is already an implicit opt-in for new functionality.

方法由应用程序(或库)调用,以声明依赖库定义的开关的值( Boolean始终是值)。 SetSwitchThe SetSwitch method is called by an application (or a library) to declare the value of a switch (which is always a Boolean value) that a dependent library defines. 开关始终是隐式false的,它提供新的行为。The switch is always implicitly false, which provides the new behavior. 通过设置开关来true启用它,它提供了旧行为。Setting the switch to true enables it, which provides the legacy behavior. 将开关显式设置为false也可提供新的行为。Explicitly setting the switch to false also provides the new behavior. 然后,依赖库可以通过调用TryGetSwitch方法来检查开关的值。The dependent library can then check the value of the switch by calling the TryGetSwitch method.

备注

使用一致的开关名称格式是有益的,因为它们是由库公开的正式协定。It's beneficial to use a consistent format for switch names, since they are a formal contract exposed by a library. 以下是两种明显的格式。The following are two obvious formats.

  • Switch.namespace.switchnameSwitch.namespace.switchname
  • Switch.library.switchnameSwitch.library.switchname

对于在 .NET Framework 上运行的应用程序,除了以编程方式设置开关的值外,还可以设置以下内容:For applications running on the .NET Framework, in addition to setting the value of a switch programmatically, it can also be set:

  • 通过在应用程序配置文件的 <运行时 >部分中将开关名称和值添加到 <AppContextSwitchOverrides >元素。By adding the switch name and value to the <AppContextSwitchOverrides> element in the <runtime> section of an application configuration file. 例如,下面定义了一个名Libraries.FPLibrary.UseExactFloatingPointComparison为的开关,其值为。 FalseFor example, the following defines a switch named Libraries.FPLibrary.UseExactFloatingPointComparison whose value is False.

    
    <configuration>
       <runtime>
          <AppContextSwitchOverrides value="Libraries.FPLibrary.UseExactFloatingPointComparison=false" />
       </runtime>
    </configuration>
    
  • 添加一个 string 值,其 name 为注册表中的HKLM\SOFTWARE\Microsoft\.NETFramework\AppContext键的开关的名称。By adding a string value whose name is the name of the switch to the HKLM\SOFTWARE\Microsoft\.NETFramework\AppContext key in the registry. 它的值必须是可以Boolean Boolean.Parse通过方法分析的的字符串表示形式; 也就是说,它必须是 "true"、"true"、"false" 或 "false"。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".

如果switchName已存在,则该isEnabled参数将覆盖其值。If switchName already exists, its value is overwritten by the isEnabled argument. 即,对SetSwitch方法的最新调用将重写在注册表、应用程序配置文件中定义的值,或者在以前对SetSwitch方法的调用中重写。That is, the most recent call to the SetSwitch method overrides the value defined in the registry, in an app configuration file, or by previous calls to the SetSwitch method.

AppContext. SetSwitch 和 .NET CoreAppContext.SetSwitch and .NET Core

.Net Core 仅支持通过SetSwitch方法进行编程调用。.NET Core supports programmatic calls with the SetSwitch method only. 支持以下开关:The following switches are supported:

开关Switch Values 说明Description
System.Net.Http.SocketsHttpHandler.Http2Support truefalsetrue or false 指示是否启用(true)或禁用(false)对 HTTP/2 协议的支持。Indicates whether support for the HTTP/2 protocol is enabled (true) or disabled (false). 默认为禁用。The default is disabled. 在首次使用HttpClient之前,必须设置此开关。The switch must be set before the first use of HttpClient. 从 .NET Core 3.0 预览版4开始提供。Available starting with .NET Core 3.0 Preview 4.
System.Net.Http.UseSocketsHttpHandler truefalsetrue or false 确定HttpClient高级网络 api,如 use System.Net.Http.SocketsHttpHandlertrue)或System.Net.Http.HttpClientHandlerfalse)。Determines whether high-level networking APIs such as HttpClient use System.Net.Http.SocketsHttpHandler (true) or System.Net.Http.HttpClientHandler (false).

适用于

另请参阅