Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process.Start with RedirectStandardOutput fails with System.PlatformNotSupportedException in .NET 9 #113450

Open
decriptor opened this issue Mar 12, 2025 · 6 comments
Labels
area-System.Diagnostics.Process needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner

Comments

@decriptor
Copy link
Contributor

Description

I've inherited some code that creates a new process with Process.Start() which redirects its stdout to report benchmark results back to the parent process. The second process uses loads an assembly through reflection. The target assembly calls into another library that uses System.Management and results in an exception:
System.PlatformNotSupportedException: 'System.Management currently is only supported for Windows desktop applications.'

I believe I've found a couple bugs that, loosely(?), relate to this: #17938 #28005

It seems like the "simple" fix is to set UseShellExecute to true, however, that doesn't work in this case since I'm also redirecting IO streams:
The Process object must have the UseShellExecute property set to false in order to redirect IO streams.

Although UseShellExecute defaults to true in .net framework, I'm assuming that it might disable it if io redirect is being used? So, maybe there is something else that needs to be set?

Hopefully this makes sense. If not, I'll try to clarify further.

Reproduction Steps

Create an application that uses Process.Start to run a second application. The second application would take some arguments such as the target assembly to load and which method and arguments to call.
The second application then uses reflection to call code in the target assembly which contains something platform specific.

In the first application:

Process process = Process.Start(new ProcessStartInfo
{
	FileName = exe,
	Arguments = exeArgs,
	UseShellExecute = false,
	LoadUserProfile = false,
	ErrorDialog = false,
	CreateNoWindow = true,
	WindowStyle = ProcessWindowStyle.Hidden,
	StandardErrorEncoding = Encoding.UTF8,
	StandardOutputEncoding = Encoding.UTF8,
	RedirectStandardError = true,
	RedirectStandardOutput = true,
	RedirectStandardInput = true,
});

In the second application:

static void Run (string assemblyName, string className, string functionName)
{
    var assembly = AssemblyLoadContext.Default.LoadFromAssemblyPath(assemblyName);
    var type = assembly.GetType(className);
    var method = type.GetMethod(functionName);
    // parameters
    var result = method.Invoke(null, parameters);

    // output results to Console.OpenStandardOutput()
}

Something simple like this, I think, should work for the library:

using System.Management;

public static string ShowOS()
{
    var result = new ManagementObjectSearcher("SELECT Caption FROM Win32_OperatingSystem").Get().Cast<ManagementObject>().First()["Caption"];
    return result;
}

Expected behavior

Executes correctly

Actual behavior

It throws this exception: System.PlatformNotSupportedException

Regression?

This works when targeting net472.

Known Workarounds

No response

Configuration

.NET 9.0
Windows 11
x64
I don't think it is specific to this configuration

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Mar 12, 2025
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

@huoyaoyuan
Copy link
Member

The second process uses loads an assembly through reflection. The target assembly calls into another library that uses System.Management and results in an exception

The process should be loading wrong System.Management.dll. See #110604.

@decriptor
Copy link
Contributor Author

Interesting, I'm loading my MyAssembly.dll which loads another MyAssembly2.dll which depends on System.Management. So, I'm not directly loading the System.Management assembly. I'll try and verify if that is what is happening.

MyAssembly2.dll is multi targets net472 and net9.0 and is also a winforms library. I would have assumed that it'd load the correct assemblies such as System.Management?

@huoyaoyuan
Copy link
Member

It's particularly important how you arranges the output of MyAssembly.dll and its dependencies. Try dotnet publish -f net9.0 -r win-x64.

@decriptor
Copy link
Contributor Author

Thanks for the reply. I just pulled off onto another project for a week. I'll try and get back to this as quickly as possible and follow up.

@steveisok steveisok added the needs-author-action An issue or pull request that requires more info or actions from the author. label Mar 22, 2025
Copy link
Contributor

This issue has been marked needs-author-action and may be missing some important information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Diagnostics.Process needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants