Skip to content

Commit

Permalink
Fix DiagnosticsClientHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
xoofx committed Dec 7, 2024
1 parent efee8f8 commit a5bc97a
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions src/Ultra.Core/DiagnosticsClientHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
namespace Ultra.Core;

// Waiting for the following PR to be merged:
// - `DiagnosticsClientConnector`: https://github.com/dotnet/diagnostics/pull/5073
// - `DiagnosticsClientConnector`: https://github.com/dotnet/diagnostics/pull/5073
// - `ApplyStartupHook`: https://github.com/dotnet/diagnostics/pull/5086
internal static class DiagnosticsClientHelper
{
Expand All @@ -16,19 +16,22 @@ internal static class DiagnosticsClientHelper

[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "ApplyStartupHookAsync")]
public static extern Task ApplyStartupHookAsync(this DiagnosticsClient client, string assemblyPath, CancellationToken token);

public static DiagnosticsClient Create(IpcEndpointBridge endPoint)
{
return (DiagnosticsClient)typeof(DiagnosticsClient)
.GetConstructor(BindingFlags.NonPublic, [IpcEndpointBridge.IpcEndpointType!])!
.Invoke([endPoint.Instance]);
var ctor = typeof(DiagnosticsClient).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
[IpcEndpointBridge.IpcEndpointType!])!;

var result = (DiagnosticsClient)ctor.Invoke([endPoint.Instance]);
return result;
}

public static DiagnosticsClient Create(IpcEndPointConfigBridge endPoint)
{
return (DiagnosticsClient)typeof(DiagnosticsClient)
.GetConstructor(BindingFlags.NonPublic, [IpcEndPointConfigBridge.IpcEndpointConfigType!])!
.Invoke([endPoint.Instance]);
var ctor = typeof(DiagnosticsClient).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance,
[IpcEndPointConfigBridge.IpcEndpointConfigType!])!;
var result = (DiagnosticsClient)ctor.Invoke([endPoint.Instance]);
return result;
}
}

Expand Down Expand Up @@ -148,8 +151,8 @@ struct ReversedDiagnosticsServerBridge
{
public static readonly Type? ReversedDiagnosticsServerType = typeof(DiagnosticsClient).Assembly.GetType("Microsoft.Diagnostics.NETCore.Client.ReversedDiagnosticsServer");
private static readonly ConstructorInfo? ReversedDiagnosticsServerConstructor = ReversedDiagnosticsServerType?.GetConstructor([typeof(string)]);
private static readonly MethodInfo? ReversedDiagnosticsServerStartMethod = ReversedDiagnosticsServerType?.GetMethod("Start");
private static readonly MethodInfo? ReversedDiagnosticsServerAcceptAsyncMethod = ReversedDiagnosticsServerType?.GetMethod("AcceptAsync");
private static readonly MethodInfo? ReversedDiagnosticsServerStartMethod = ReversedDiagnosticsServerType?.GetMethod("Start", []);
private static readonly MethodInfo? ReversedDiagnosticsServerAcceptAsyncMethod = ReversedDiagnosticsServerType?.GetMethod("AcceptAsync", [typeof(CancellationToken)]);
private static readonly MethodInfo? ReversedDiagnosticsServerDisposeAsyncMethod = ReversedDiagnosticsServerType?.GetMethod("DisposeAsync");

public readonly object Instance;
Expand All @@ -173,8 +176,9 @@ public void Start()
}
public async Task<IpcEndpointInfoBridge> AcceptAsync(CancellationToken ct)
{
Task<object> task = (Task<object>)ReversedDiagnosticsServerAcceptAsyncMethod!.Invoke(Instance, [ct])!;
var result = await task;
Task task = (Task)ReversedDiagnosticsServerAcceptAsyncMethod!.Invoke(Instance, [ct])!;
await task;
var result = task.GetType().GetProperty("Result")!.GetValue(task)!;
return new IpcEndpointInfoBridge(result);
}

Expand Down

0 comments on commit a5bc97a

Please sign in to comment.