如何使用托管代码处理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...");  
}  

示例方法具有以下参数:

参数 类型 说明
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错误

另请参阅

关于错误