Skip to content

Commit c6750d3

Browse files
moooyoYu Leng (from Dev Box)
and
Yu Leng (from Dev Box)
authored
[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) <[email protected]>
1 parent 37836c6 commit c6750d3

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/Helpers/ServiceHelper.cs

+10-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
using System.ServiceProcess;
1111
using Microsoft.CmdPal.Ext.WindowsServices.Commands;
1212
using Microsoft.CmdPal.Ext.WindowsServices.Properties;
13-
using Microsoft.CommandPalette.Extensions;
1413
using Microsoft.CommandPalette.Extensions.Toolkit;
1514
using Microsoft.Win32;
1615
using Windows.System;
@@ -44,9 +43,14 @@ public static IEnumerable<ListItem> Search(string search)
4443
serviceList = servicesStartsWith.Concat(servicesContains);
4544
}
4645

47-
return serviceList.Select(s =>
46+
var result = serviceList.Select(s =>
4847
{
49-
var serviceResult = new ServiceResult(s);
48+
var serviceResult = ServiceResult.CreateServiceController(s);
49+
if (serviceResult == null)
50+
{
51+
return null;
52+
}
53+
5054
ServiceCommand serviceCommand;
5155
CommandContextItem[] moreCommands;
5256
if (serviceResult.IsRunning)
@@ -93,7 +97,9 @@ public static IEnumerable<ListItem> Search(string search)
9397
// ToolTipData = new ToolTipData(serviceResult.DisplayName, serviceResult.ServiceName),
9498
// IcoPath = icoPath,
9599
};
96-
});
100+
}).Where(s => s != null);
101+
102+
return result;
97103
}
98104

99105
// TODO GH #118 IPublicAPI contextAPI isn't used anymore, but we need equivalent ways to show notifications and status

src/modules/cmdpal/Exts/Microsoft.CmdPal.Ext.WindowsServices/ServiceResult.cs

+18-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class ServiceResult
1717

1818
public bool IsRunning { get; }
1919

20-
public ServiceResult(ServiceController serviceController)
20+
private ServiceResult(ServiceController serviceController)
2121
{
2222
ArgumentNullException.ThrowIfNull(serviceController);
2323

@@ -26,4 +26,21 @@ public ServiceResult(ServiceController serviceController)
2626
StartMode = serviceController.StartType;
2727
IsRunning = serviceController.Status != ServiceControllerStatus.Stopped && serviceController.Status != ServiceControllerStatus.StopPending;
2828
}
29+
30+
public static ServiceResult CreateServiceController(ServiceController serviceController)
31+
{
32+
try
33+
{
34+
var result = new ServiceResult(serviceController);
35+
36+
return result;
37+
}
38+
catch (Exception)
39+
{
40+
// try to log the exception in the future
41+
// retrieve properties from serviceController will throw exception. Such as PlatformNotSupportedException.
42+
}
43+
44+
return null;
45+
}
2946
}

0 commit comments

Comments
 (0)