-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Process.ProcessName throws when the process is an admin process (like "services.exe") and the current app is running as a low privilege user. This is a regression from .NET 6, and it breaks Microsoft.Extensions.Hosting.WindowsServices when running as a non-admin user.
This could possibly be a regression from #59672.
Repro Steps
- Build a console app as below:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>using System.Diagnostics;
namespace ConsoleApp1
{
internal class Program
{
const int ServicesProcessId = 756;
static void Main(string[] args)
{
var services = Process.GetProcessById(ServicesProcessId);
Console.WriteLine(services.ProcessName);
}
}
}-
Look in Task Manager for the
services.exeprocess and replace the ServiceProcessId with the process ID for your current machine. -
Run the app as you, it prints
services
- Run the app as a non-admin, low privilege user
Unhandled exception. System.InvalidOperationException: Process has exited, so the requested information is not available.
at System.Diagnostics.Process.get_ProcessName()
at ConsoleApp1.Program.Main(String[] args) in C:\Users\eerhardt\source\repos\WindowsService1\ConsoleApp1\Program.cs:line 12
- Change the
TargetFrameworktonet6.0, and run as the same non-admin, low privilege user:
services
Original Report
I come back to the issue #67093, which is closed. The error still occurs with preview.4 and todays nightly build, but only on Windows Server 2016, not on my local Windows 11 developer machine. The error is:
Application: CloudManagementTool.WorkerService.exe
CoreCLR Version: 7.0.22.27203
.NET Version: 7.0.0-preview.5.22272.3
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException: The type initializer for 'CloudManagementTool.WorkerService.WorkerService' threw an exception.
---> System.InvalidOperationException: Process has exited, so the requested information is not available.
at System.Diagnostics.Process.get_ProcessName()
at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()
at ConsoleApp1.Program.<Main>(String[] args) in D:\Test\Program.cs:line 13
A reproducing sample program is quite short, but it only crashes on Windows Server 2016 and when registered and executed as a service. Starting the same code directly from the console works fine.
public class Program
{
public static async Task Main(string[] args)
{
var isService = WindowsServiceHelpers.IsWindowsService();
if (isService)
{
// ...
This issue becomes critical now, because it will make the 7.0 version unsable for windows services on Windows Server 2016.