次の方法で共有


マネージド コードを使用して非同期エラー Configuration Manager処理する方法

非同期クエリ中に発生するConfiguration Manager エラーを処理するには、SmsBackgroundWorker.QueryProcessorCompleted イベント ハンドラーに渡されるパラメーター Error Exception プロパティをテストRunWorkerCompletedEventArgsします。 Error が ではないnull場合は、例外が発生し、Error を使用して原因を検出します。

ErrorSmsQueryException の場合は、それを使用して基になる __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...");  
}  

このメソッドの例には、次のパラメーターがあります。

パラメーター 説明
sender - Object イベントのソース。
e - RunWorkerCompletedEventArgs イベント データ。

詳細については、「 RunWorkerCompletedEventArgs クラス」を参照してください。

コードのコンパイル

この C# の例では、次のものが必要です。

名前空間

System

System.Collections.Generic

System.text

Microsoft。ConfigurationManagement.ManagementProvider

Microsoft。ConfigurationManagement.ManagementProvider.WqlQueryEngine

System.Management

System.ComponentModel

Assembly

microsoft.configurationmanagement.managementprovider

adminui.wqlqueryengine

System.Management

堅牢なプログラミング

エラー処理の詳細については、「Configuration Manager エラーについて」を参照してください。

関連項目

エラーについて