Example #1
0
 private async Task <Device> TryGetDeviceAsync(AccessProvider accessProvider, string hostName, CancellationToken cancellationToken = new CancellationToken(), InformationCategory[] informationCategories = null, params InformationType[] informationTypes)
 {
     return(await Task.Run(async() =>
     {
         try
         {
             if (accessProvider != null)
             {
                 var wmiAccessService = await accessProvider.GetAccessAsync(hostName).ConfigureAwait(false);
                 if (wmiAccessService != null)
                 {
                     return await TryGetDeviceAsync(wmiAccessService, cancellationToken, informationCategories, informationTypes).ConfigureAwait(false);
                 }
             }
         }
         catch (OperationCanceledException operationCanceledException)
         {
             LogEventHandler.TaskIncompleted(operationCanceledException);
         }
         catch (Exception exception)
         {
             var wmiException = new WMIGeneralException(hostName, exception);
             LogEventHandler.Exception(wmiException);
         }
         return null;
     }, cancellationToken).ConfigureAwait(false));
 }
Example #2
0
 private Task <WMIAccessService> TryGetAccessAsync(WMIConnectionOption wmiConnectionOption)
 {
     return(Task.Run(async() =>
     {
         WMIAccessService connectedWMIConnectedAccessService = null;
         if (wmiConnectionOption != null)
         {
             try
             {
                 var wmiAccessService = new WMIAccessService();
                 var connected = await wmiAccessService.TryConnectAsync(wmiConnectionOption).ConfigureAwait(false);
                 if (connected)
                 {
                     connectedWMIConnectedAccessService = wmiAccessService;
                 }
             }
             catch (Exception exception)
             {
                 var endPoint = string.IsNullOrEmpty(wmiConnectionOption.EndPoint)
                     ? wmiConnectionOption.DeviceName
                     : wmiConnectionOption.EndPoint;
                 var wmiException = new WMIGeneralException(endPoint, exception);
                 LogEventHandler.Exception(wmiException);
             }
         }
         return connectedWMIConnectedAccessService;
     }));
 }
Example #3
0
 private void TrySetValue(object objectInstance, PropertyInfo propertyInfo, PropertyData propertyData)
 {
     try
     {
         propertyInfo.SetValue(objectInstance, propertyData?.Value);
     }
     catch (Exception)
     {
         try
         {
             if (propertyInfo != null)
             {
                 if (propertyInfo.ReflectedType != null && propertyData?.Type != null)
                 {
                     LogEventHandler.Warning($"{objectInstance.GetType().FullName}.{propertyData.Name} : should be type of {propertyData.Type}. Failed with value: {propertyData.Value}");
                 }
                 propertyInfo.SetValue(objectInstance, propertyData?.Value?.ToString());
             }
         }
         catch (Exception exception)
         {
             var wmiException = new WMIGeneralException(EndPoint, exception);
             LogEventHandler.Exception(wmiException);
         }
     }
 }
Example #4
0
 private async Task <Device> TryGetDeviceAsync(WMIAccessService wmiAccessService, CancellationToken cancellationToken = new CancellationToken(), InformationCategory[] informationCategories = null, params InformationType[] informationTypes)
 {
     if (wmiAccessService != null)
     {
         try
         {
             return(await Task.Run(async() =>
             {
                 try
                 {
                     if (wmiAccessService.Connected)
                     {
                         var device = await new Device()
                                      .WithWMIAccessService(wmiAccessService)
                                      .WithInformationCategories(informationCategories)
                                      .WithInformationTypes(informationTypes)
                                      .WithQueries(Queries)
                                      .InitializeAsync(cancellationToken)
                                      .ConfigureAwait(false);
                         return device;
                     }
                 }
                 catch (OperationCanceledException operationCanceledException)
                 {
                     LogEventHandler.TaskIncompleted(operationCanceledException);
                 }
                 catch (Exception exception)
                 {
                     var wmiException = new WMIGeneralException(wmiAccessService.EndPoint, exception);
                     LogEventHandler.Exception(wmiException);
                 }
                 return null;
             }, cancellationToken).ConfigureAwait(false));
         }
         catch (OperationCanceledException operationCanceledException)
         {
             LogEventHandler.TaskIncompleted(operationCanceledException);
         }
         catch (Exception)
         {
             if (!cancellationToken.IsCancellationRequested)
             {
                 throw;
             }
         }
     }
     return(null);
 }
Example #5
0
        private async Task <object> TryMapObjectAsync(ManagementBaseObject managementBaseObject, Type propertyType)
        {
            var tasks              = new List <Task>();
            var objectInstance     = Activator.CreateInstance(propertyType);
            var objectInstanceName = objectInstance?.GetType().Name;

            if (objectInstance != null)
            {
                foreach (var propertyInfo in objectInstance.GetType().GetProperties())
                {
                    tasks.Add(Task.Run(() =>
                    {
                        try
                        {
                            var propertyData = managementBaseObject?.Properties[propertyInfo.Name];
                            if (!(propertyData is null))
                            {
                                TrySetValue(objectInstance, propertyInfo, propertyData);
                                LogEventHandler.Information($"{EndPoint}: {objectInstanceName}: {propertyInfo.Name}: {propertyData.Value}");
                            }
                        }
                        catch (ManagementException exception)
                        {
                            if (exception.ErrorCode != ManagementStatus.NotFound)
                            {
                                var wmiException = new WMIGeneralException(EndPoint, exception);
                                LogEventHandler.Exception(wmiException);
                            }
                        }
                        catch (Exception exception)
                        {
                            var wmiException = new WMIGeneralException(EndPoint, exception);
                            LogEventHandler.Exception(wmiException);
                        }
                    }));
                }
            }

            await Task.WhenAll(tasks).ConfigureAwait(false);

            return(objectInstance);
        }
Example #6
0
        private void LogException(Exception exception)
        {
            var data = string.Empty;

            if (exception is ManagementException managementException)
            {
                var parameterInfo = managementException.ErrorInformation.Properties["ParameterInfo"].Value.ToString();
                if (!string.IsNullOrEmpty(parameterInfo))
                {
                    data += parameterInfo;
                }
            }

            if (string.IsNullOrEmpty(data))
            {
                data = exception.Message;
            }

            var wmiException = new WMIGeneralException(EndPoint, data, exception);

            LogEventHandler.Exception(wmiException);
        }