Skip to content

Latest commit

 

History

History
114 lines (86 loc) · 5.33 KB

how-to-handle-configuration-manager-asynchronous-errors-by-using-managed-code.md

File metadata and controls

114 lines (86 loc) · 5.33 KB
title titleSuffix description ms.date ms.subservice ms.service ms.topic ms.assetid author ms.author manager ms.localizationpriority ms.collection ms.reviewer
Handle Asynchronous Errors by Using Managed Code
Configuration Manager
To handle a Configuration Manager error raised during an asynchronous query, test the RunWorkerCompletedEventArgs parameter Error Exception property passed to the SmsBackgroundWorker.QueryProcessorCompleted event handler.
09/20/2016
sdk
configuration-manager
how-to
1a37a005-07cd-476e-a744-fa345f3232c7
Banreet
banreetkaur
apoorvseth
low
tier3
mstewart,aaroncz

How to Handle Configuration Manager Asynchronous Errors by Using Managed Code

To handle a Configuration Manager error that is raised during an asynchronous query, you test the RunWorkerCompletedEventArgs parameter Error Exception property that is passed to the SmsBackgroundWorker.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.

  2. In the asynchronous query SmsBackgroundWorker.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 QueryProcessorBase.ProcessQuery method.

Example

The following example implements a SmsBackgroundWorker.QueryProcessorCompleted event handler.

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 RunWorkerCompletedEventArgs Class.

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.

See Also

About errors