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


設定參數的值。Sets the value of a switch.

 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)


String String String String

參數的名稱。The name of the switch.

Boolean Boolean Boolean Boolean

參數的值。The value of the switch.



下面這行程式碼會將名Switch.AmazingLib.ThrowOnException為的交換器設定為true,以啟用舊版行為。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.switchname
  • 參數.程式庫.參數名稱Switch.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.

          <AppContextSwitchOverrides value="Libraries.FPLibrary.UseExactFloatingPointComparison=false" />
  • 藉由新增字串值,其名稱為登錄中索引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 Preview 4 開始提供。Available starting with .NET Core 3.0 Preview 4.
System.Net.Http.UseSocketsHttpHandler truefalsetrue or false 判斷HttpClient高階網路 api,例如使用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).