如何使用 Managed 程式碼處理 Configuration Manager 非同步錯誤

若要處理在非同步查詢期間引發的 Configuration Manager 錯誤,請測試RunWorkerCompletedEventArgs傳遞至 SmsBackgroundWorker.QueryProcessorCompleted 事件處理常式的參數 Error Exception 屬性。 如果不是 null[錯誤] ,表示發生例外狀況,您可以使用 [錯誤] 來探索原因。

如果 Error 是 SmsQueryException,您可以使用它來取得基礎 __ExtendedException 或 SMS_ExtendedException。 由於託管 SMS 提供者程式庫並未包裝這些例外狀況,因此您必須使用 System.Management 命名空間 ManagementException 物件來存取它們。

處理非同步查詢錯誤

  1. 建立非同步查詢。

  2. 在非同步查詢 SmsBackgroundWorker.QueryProcessorCompleted 事件處理常式中,實作下列範例中的程式碼。

  3. 執行非同步查詢。 若要測試例外狀況處理常式,請將格式錯誤的查詢字串傳遞,例如 Select & from &&&QueryProcessorBase.ProcessQuery 方法。

範例

下列範例會實作 SmsBackgroundWorker.QueryProcessorCompleted 事件處理常式。

如需呼叫範例程式碼的詳細資訊,請參閱 呼叫 Configuration Manager 程式碼片段。

void bw1_QueryProcessorCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    if (e.Error != null)
    {
        Console.WriteLine("There was an Error");
        if (e.Error is SmsQueryException)
        {
            SmsQueryException queryException = (SmsQueryException)e.Error;
            Console.WriteLine(queryException.Message);

            // Get either the __ExtendedStatus or SMS_ExtendedStatus object and display various properties.
            ManagementException mgmtExcept = queryException.InnerException as ManagementException;

            if (mgmtExcept != null)
            {
                if (string.Equals(mgmtExcept.ErrorInformation.ClassPath.ToString(), "SMS_ExtendedStatus", StringComparison.OrdinalIgnoreCase) == true)
                {
                    Console.WriteLine("Configuration Manager provider exception");
                }

                else if (string.Equals(mgmtExcept.ErrorInformation.ClassPath.ToString(), "__ExtendedStatus", StringComparison.OrdinalIgnoreCase) == true)
                {
                    Console.WriteLine("WMI exception");
                }
                Console.WriteLine(mgmtExcept.ErrorCode.ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["ParameterInfo"].ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["Operation"].ToString());
                Console.WriteLine(mgmtExcept.ErrorInformation["ProviderName"].ToString());
            }

        }
        if (e.Error is SmsConnectionException)
        {
            Console.WriteLine("There was a connection error :" + ((SmsConnectionException)e.Error).Message);
            Console.WriteLine(((SmsConnectionException)e.Error).ErrorCode);
        }
    }

    Console.WriteLine("Done...");
}

範例方法具有下列參數:

參數 Type 描述
sender - Object 事件的來源。
e - RunWorkerCompletedEventArgs 事件資料。

如需詳細資訊,請參閱 RunWorkerCompletedEventArgs 類別。

正在編譯程式碼

此 C# 範例需要:

命名空間

系統

System.Collections.Generic

System.Text

Microsoft.ConfigurationManagement.ManagementProvider

Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine

System.Management

System.ComponentModel

組件

Microsoft.ConfigurationManagement.ManagementProvider

adminui.wqlqueryengine

System.Management

健全的程式設計

如需錯誤處理的詳細資訊,請參閱 關於 Configuration Manager 錯誤。

另請參閱

關於錯誤