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
[API Proposal]: Assembly.SetEntryAssembly() #101616
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
There is nothing NativeAOT or trim incompatible in this API.
Yes.
Yes. |
This is best effort. We won't be ever able to fully abstract away the fact that the application was launched through a launcher. For example, https://learn.microsoft.com/dotnet/api/system.diagnostics.process.mainmodule is going to point to the launcher .exe and not to the app.exe, and it is not something that we want to shim. If there are scenarios where diagnostic tools need to know about the current entrypoint, we can always add new diagnostic APIs to handle the new scenario if the existing diagnostic APIs are not sufficient. |
The proposal LGTM! Feel free to flip it to api-ready-to-review. (Also, mark it as blocking if you would like it to be reviewed with higher priority.) |
Is this supposed to be static? |
Will there be any validation on the assembly used? E.g.:
|
We may want to check that it is corelib-defined RuntimeAssembly as a sanity check. I do not think we need any other extraneous validation. It is up to the caller of this API to ensure that it does not break the rest of the app. |
Background and motivation
There exist certain specialty applications that act as a launcher for other applications within the same process. These applications typically wait for some event to occur (file system change, database change, time elapsed, etc), load a target application, and then execute it.
This works almost perfectly within .NET where the launcher can do something like spawn a thread and quickly get out of the way. However, this remains a tad complicated in the initial bookkeeping of the BCL / runtime that applications rely on. If, as applications sometimes do, they want to invoke
Assembly.GetEntryAssembly
, the result they get would obviously be wrong, as it would point to the launcher instead of the application itself. So, this requires changes in order to operate correctly within a launcher.In order for customers to have that bookkeeping consistency and be able to change the assembly that actually will be run in these scenarios, we would like to propose allowing them to update what the runtime views as the entry assembly, and acts accordingly to that.
API Proposal
namespace System.Reflection; public class Assembly { public static Assembly? GetEntryAssembly(); // Existing API + public static void SetEntryAssembly(Assembly assembly); }
API Usage
Alternative Designs
One alternative may be to provide an API like the dotnet/arcade
RemoteExecutor
that makes it easier to launch a new process with copy of the existing runtime but running a different entry method (potentially from another assembly). That is insufficient for some use cases: some platforms (for example WebAssembly or mobile) do not support launching a new process; in other use cases it may be important to preserve part of the runtime state rather than starting from a brand new process.Risks
The risk of this new API depends on the depth of the implementation. For diagnostics purposes, there are places in the runtime that cache the string path to the initial entry assembly. We would need to update and potentially change how that information is stored. Additionally, we may want to consider tricks that benefit startup should the entry assembly change early enough (within a startup hook, for example).
Other Notes to Consider
Questions:
SetEntryAssembly
with an assembly from a collectable ALC and then later set it to something else, should the collectable ALC be able to be collected?The text was updated successfully, but these errors were encountered: