From 03d9833a6192fa37dd75d1719e04def42dbe3925 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Thu, 5 Dec 2024 20:42:12 +0100 Subject: [PATCH 1/4] Make DiagnosticsClient.ApplyStartupHook public --- .../DiagnosticsClient/DiagnosticsClient.cs | 51 ++++++++++++++++++- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs index 47a2f5a922..6ab28c20dd 100644 --- a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs +++ b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs @@ -332,15 +332,62 @@ internal async Task> GetProcessEnvironmentAsync(Cance return await helper.ReadEnvironmentAsync(response.Continuation, token).ConfigureAwait(false); } - internal void ApplyStartupHook(string startupHookPath) + /// + /// Loads the specified assembly with a StartupHook in the target process. + /// + /// The path to the assembly containing the StartupHook. + /// Thrown when is null or empty. + /// Thrown when the specified assembly does not exist. + /// Thrown when the runtime version is less than 8.0. + public void ApplyStartupHook(string startupHookPath) { + if (string.IsNullOrEmpty(startupHookPath)) + { + throw new ArgumentNullException(nameof(startupHookPath)); + } + + if (!File.Exists(startupHookPath)) + { + throw new FileNotFoundException($"Startup hook file not found: {startupHookPath}"); + } + + ProcessInfo processInfo = GetProcessInfo(); + if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) + { + throw new NotSupportedException($"Startup hooks are only supported on .NET 8.0 and later. Runtime Version: {version}."); + } + IpcMessage message = CreateApplyStartupHookMessage(startupHookPath); IpcMessage response = IpcClient.SendMessage(_endpoint, message); ValidateResponseMessage(response, nameof(ApplyStartupHook)); } - internal async Task ApplyStartupHookAsync(string startupHookPath, CancellationToken token) + /// + /// Loads the specified assembly with a StartupHook in the target process. + /// + /// The path to the assembly containing the StartupHook. + /// The token to monitor for cancellation requests. + /// Thrown when is null or empty. + /// Thrown when the specified assembly does not exist. + /// Thrown when the runtime version is less than 8.0. + public async Task ApplyStartupHookAsync(string startupHookPath, CancellationToken token) { + if (string.IsNullOrEmpty(startupHookPath)) + { + throw new ArgumentNullException(nameof(startupHookPath)); + } + + if (!File.Exists(startupHookPath)) + { + throw new FileNotFoundException($"Startup hook file not found: {startupHookPath}"); + } + + ProcessInfo processInfo = await GetProcessInfoAsync(token).ConfigureAwait(false); + if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) + { + throw new NotSupportedException($"Startup hooks are only supported on .NET 8.0 and later. Runtime Version: {version}."); + } + IpcMessage message = CreateApplyStartupHookMessage(startupHookPath); IpcMessage response = await IpcClient.SendMessageAsync(_endpoint, message, token).ConfigureAwait(false); ValidateResponseMessage(response, nameof(ApplyStartupHookAsync)); From 29e0161806aae27cce74019b0e47f5b5c7ec8a7b Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Thu, 5 Dec 2024 20:59:56 +0100 Subject: [PATCH 2/4] Don't check if assembly exists locally --- .../DiagnosticsClient/DiagnosticsClient.cs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs index 6ab28c20dd..96e77a4579 100644 --- a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs +++ b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs @@ -346,11 +346,6 @@ public void ApplyStartupHook(string startupHookPath) throw new ArgumentNullException(nameof(startupHookPath)); } - if (!File.Exists(startupHookPath)) - { - throw new FileNotFoundException($"Startup hook file not found: {startupHookPath}"); - } - ProcessInfo processInfo = GetProcessInfo(); if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) { @@ -377,11 +372,6 @@ public async Task ApplyStartupHookAsync(string startupHookPath, CancellationToke throw new ArgumentNullException(nameof(startupHookPath)); } - if (!File.Exists(startupHookPath)) - { - throw new FileNotFoundException($"Startup hook file not found: {startupHookPath}"); - } - ProcessInfo processInfo = await GetProcessInfoAsync(token).ConfigureAwait(false); if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) { From bb7c6cbaf6b808e232923e99a68ee7124132f565 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Thu, 5 Dec 2024 21:00:29 +0100 Subject: [PATCH 3/4] Rermove checks for .NET 8 --- .../DiagnosticsClient/DiagnosticsClient.cs | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs index 96e77a4579..df7a63dfd2 100644 --- a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs +++ b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs @@ -346,12 +346,6 @@ public void ApplyStartupHook(string startupHookPath) throw new ArgumentNullException(nameof(startupHookPath)); } - ProcessInfo processInfo = GetProcessInfo(); - if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) - { - throw new NotSupportedException($"Startup hooks are only supported on .NET 8.0 and later. Runtime Version: {version}."); - } - IpcMessage message = CreateApplyStartupHookMessage(startupHookPath); IpcMessage response = IpcClient.SendMessage(_endpoint, message); ValidateResponseMessage(response, nameof(ApplyStartupHook)); @@ -372,12 +366,6 @@ public async Task ApplyStartupHookAsync(string startupHookPath, CancellationToke throw new ArgumentNullException(nameof(startupHookPath)); } - ProcessInfo processInfo = await GetProcessInfoAsync(token).ConfigureAwait(false); - if (!processInfo.TryGetProcessClrVersion(out Version version) || version.Major < 8) - { - throw new NotSupportedException($"Startup hooks are only supported on .NET 8.0 and later. Runtime Version: {version}."); - } - IpcMessage message = CreateApplyStartupHookMessage(startupHookPath); IpcMessage response = await IpcClient.SendMessageAsync(_endpoint, message, token).ConfigureAwait(false); ValidateResponseMessage(response, nameof(ApplyStartupHookAsync)); From 36f377f1b4057787cbbc87f4b2167d81f6f0a878 Mon Sep 17 00:00:00 2001 From: Alexandre Mutel Date: Thu, 5 Dec 2024 21:02:09 +0100 Subject: [PATCH 4/4] Remove invalid XML docs --- .../DiagnosticsClient/DiagnosticsClient.cs | 4 ---- 1 file changed, 4 deletions(-) diff --git a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs index df7a63dfd2..4438a4dbb4 100644 --- a/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs +++ b/src/Microsoft.Diagnostics.NETCore.Client/DiagnosticsClient/DiagnosticsClient.cs @@ -337,8 +337,6 @@ internal async Task> GetProcessEnvironmentAsync(Cance /// /// The path to the assembly containing the StartupHook. /// Thrown when is null or empty. - /// Thrown when the specified assembly does not exist. - /// Thrown when the runtime version is less than 8.0. public void ApplyStartupHook(string startupHookPath) { if (string.IsNullOrEmpty(startupHookPath)) @@ -357,8 +355,6 @@ public void ApplyStartupHook(string startupHookPath) /// The path to the assembly containing the StartupHook. /// The token to monitor for cancellation requests. /// Thrown when is null or empty. - /// Thrown when the specified assembly does not exist. - /// Thrown when the runtime version is less than 8.0. public async Task ApplyStartupHookAsync(string startupHookPath, CancellationToken token) { if (string.IsNullOrEmpty(startupHookPath))