如何使用托管代码处理Configuration Manager同步错误

若要处理同步查询中引发的Configuration Manager错误,请捕获 SmsQueryException 异常。 由于 SMS_Exception] 也会捕获此异常,因此可以在同一 catch 块中捕获它和 SmsConnectionException 异常。

如果SMS_Exception中捕获的异常是 SmsQueryException,则可以使用它来访问基础 __ExtendedExceptionSMS_ExtendedException。 由于托管 SMS 提供程序库不会包装这些异常,因此需要使用 System.Management 命名空间 ManagementException 对象来访问这些异常。

注意

为清楚起见,本文档中的大多数示例只是重新引发异常。 如果需要更丰富的异常信息,可以将它们替换为以下示例。

处理同步查询错误

  1. 编写代码以访问 SMS 提供程序。

  2. 使用以下示例代码捕获 SmsQueryExceptionSmsConnectionException 异常。

示例

以下 C# 示例函数尝试打开不存在 SMS_Package 的包。 在异常处理程序中,代码确定引发的错误类型并显示其信息。

有关调用示例代码的信息,请参阅调用Configuration Manager代码片段

public void ExerciseException(WqlConnectionManager connection)  
{  
    try  
    {  

        IResultObject package = connection.GetInstance(@"SMS_Package.PackageID='UNKNOWN'");  
        Console.WriteLine("Package Name: " + package["Name"].StringValue);  
        Console.WriteLine("Package Description: " + package["Description"].StringValue);  

    }  
    catch (SmsException e)  
    {  
        if (e is SmsQueryException)  
        {  
            SmsQueryException queryException = (SmsQueryException)e;  
            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 is SmsConnectionException)  
        {  
            Console.WriteLine("There was a connection error :" + ((SmsConnectionException)e).Message);  
            Console.WriteLine(((SmsConnectionException)e).ErrorCode);  
        }  
    }  
}  

示例方法具有以下参数:

参数 类型 说明
connection - WqlConnectionManager 与提供程序的有效连接。

编译代码

此 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错误

另请参阅

关于错误如何使用托管代码处理Configuration Manager异步错误