From c6750d3a622d1d27efa498ce38f35fcf87565d73 Mon Sep 17 00:00:00 2001 From: moooyo <42196638+moooyo@users.noreply.github.com> Date: Wed, 26 Mar 2025 19:34:59 +0800 Subject: [PATCH] [cmdpal] Fix windows service extension crash issue (#38166) repro step: change language to non-english language. stably reproducible. --------- Co-authored-by: Yu Leng (from Dev Box) --- .../Helpers/ServiceHelper.cs | 14 ++++++++++---- .../ServiceResult.cs | 19 ++++++++++++++++++- 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/Helpers/ServiceHelper.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/Helpers/ServiceHelper.cs index 6f036372fa..ca10db6a9c 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/Helpers/ServiceHelper.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/Helpers/ServiceHelper.cs @@ -10,7 +10,6 @@ using System.Linq; using System.ServiceProcess; using Microsoft.CmdPal.Ext.WindowsServices.Commands; using Microsoft.CmdPal.Ext.WindowsServices.Properties; -using Microsoft.CommandPalette.Extensions; using Microsoft.CommandPalette.Extensions.Toolkit; using Microsoft.Win32; using Windows.System; @@ -44,9 +43,14 @@ public static class ServiceHelper serviceList = servicesStartsWith.Concat(servicesContains); } - return serviceList.Select(s => + var result = serviceList.Select(s => { - var serviceResult = new ServiceResult(s); + var serviceResult = ServiceResult.CreateServiceController(s); + if (serviceResult == null) + { + return null; + } + ServiceCommand serviceCommand; CommandContextItem[] moreCommands; if (serviceResult.IsRunning) @@ -93,7 +97,9 @@ public static class ServiceHelper // ToolTipData = new ToolTipData(serviceResult.DisplayName, serviceResult.ServiceName), // IcoPath = icoPath, }; - }); + }).Where(s => s != null); + + return result; } // TODO GH #118 IPublicAPI contextAPI isn't used anymore, but we need equivalent ways to show notifications and status diff --git a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/ServiceResult.cs b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/ServiceResult.cs index 279e55dffd..ff7942a055 100644 --- a/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/ServiceResult.cs +++ b/src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/ServiceResult.cs @@ -17,7 +17,7 @@ public class ServiceResult public bool IsRunning { get; } - public ServiceResult(ServiceController serviceController) + private ServiceResult(ServiceController serviceController) { ArgumentNullException.ThrowIfNull(serviceController); @@ -26,4 +26,21 @@ public class ServiceResult StartMode = serviceController.StartType; IsRunning = serviceController.Status != ServiceControllerStatus.Stopped && serviceController.Status != ServiceControllerStatus.StopPending; } + + public static ServiceResult CreateServiceController(ServiceController serviceController) + { + try + { + var result = new ServiceResult(serviceController); + + return result; + } + catch (Exception) + { + // try to log the exception in the future + // retrieve properties from serviceController will throw exception. Such as PlatformNotSupportedException. + } + + return null; + } }