Compartilhar via


How to Handle Configuration Manager Asynchronous Errors by Using Managed Code

Applies To: System Center Configuration Manager 2007, System Center Configuration Manager 2007 R2, System Center Configuration Manager 2007 R3, System Center Configuration Manager 2007 SP1, System Center Configuration Manager 2007 SP2

To handle a Microsoft System Center Configuration Manager 2007 error that is raised during an asynchronous query, you test the RunWorkerCompletedEventArgs parameter Error Exception property that is passed to the QueryProcessorCompleted event handler. If Error is not null, an exception has occurred and you use Error to discover the cause.

If Error is an SmsQueryException, you can use it to get to the underlying __ExtendedException or SMS_ExtendedException. Because the managed SMS Provider library does not wrap these exceptions you will need to use the System.Management namespace ManagementException object to access them.

To handle an asynchronous query error

  1. Create an asynchronous query by using the code in How to Perform an Asynchronous Configuration Manager Query by Using Managed Code.

  2. In the asynchronous query QueryProcessorCompleted event handler, implement the code in the following example.

  3. Run the asynchronous query. To test the exception handler, pass a badly formed query string such as Select & from &&& to the QueryProcessor.ProcessQuery method.

Example

The following example implements a QueryProcessorCompleted event handler. Use it to replace the equivalent method in How to Perform an Asynchronous Configuration Manager Query by Using Managed Code.

For information about calling the sample code, see Calling Configuration Manager Code Snippets.

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...");
}

The example method has the following parameters:

Parameter Type Description

sender

  • Object

The source of the event.

e

  • RunWorkerCompletedEventArgs

The event data.

For more information, see the RunWorkerCompletedEventArgs Class (https://go.microsoft.com/fwlink/?LinkId=111728).

Compiling the Code

This C# example requires:

Namespaces

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

Robust Programming

For more information about error handling, see About Configuration Manager Errors.

Security

For more information about securing Configuration Manager applications, see About Securing Configuration Manager Applications.

See Also

Concepts

Configuration Manager Errors