Share via


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

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

如果 ErrorSmsQueryException,您可以使用它來取得基礎 __ExtendedExceptionSMS_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錯誤

另請參閱

關於錯誤