[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) <yuleng@microsoft.com>
This commit is contained in:
moooyo
2025-03-26 19:34:59 +08:00
committed by GitHub
parent 37836c656d
commit c6750d3a62
2 changed files with 28 additions and 5 deletions

View File

@@ -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

View File

@@ -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;
}
}