Skip to content

Commit 98607d0

Browse files
committed
Expose metadata about TargetMethod publicly
This allows logging based on overridding `DispatchRequestAsync` to know what the target method is.
1 parent 91a5a96 commit 98607d0

File tree

6 files changed

+34
-5
lines changed

6 files changed

+34
-5
lines changed

src/StreamJsonRpc/Reflection/TargetMethod.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,20 @@ internal TargetMethod(
7070
}
7171
}
7272

73+
/// <summary>
74+
/// Gets the runtime type of the target object, if there is one.
75+
/// </summary>
76+
/// <remarks>
77+
/// Even when a matching target method is found, there may not be a target <em>object</em>
78+
/// if the target method is <see langword="static" />.
79+
/// </remarks>
80+
public Type? TargetObjectType => this.target?.GetType();
81+
82+
/// <summary>
83+
/// Gets the <see cref="MethodInfo"/> that will be invoked to handle the request, if one was found.
84+
/// </summary>
85+
public MethodInfo? TargetMethodInfo => this.signature?.MethodInfo;
86+
7387
/// <summary>
7488
/// Gets all the exceptions thrown while trying to deserialize arguments to candidate parameter types.
7589
/// </summary>

src/StreamJsonRpc/net6.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
44
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
55
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
66
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
7+
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
8+
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?

src/StreamJsonRpc/net8.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
44
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
55
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
66
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
7+
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
8+
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?

src/StreamJsonRpc/netstandard2.0/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
44
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
55
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
66
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
7+
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
8+
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?

src/StreamJsonRpc/netstandard2.1/PublicAPI.Unshipped.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
44
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
55
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
66
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
7+
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
8+
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?

test/StreamJsonRpc.Tests/JsonRpcDelegatedDispatchAndSendTests.cs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
using System.Diagnostics;
22
using Microsoft.VisualStudio.Threading;
3-
using StreamJsonRpc;
4-
using StreamJsonRpc.Protocol;
5-
using Xunit;
6-
using Xunit.Abstractions;
73

84
public class JsonRpcDelegatedDispatchAndSendTests : TestBase
95
{
@@ -37,6 +33,14 @@ public async Task DispatchRequestIsPassedCorrectTypeOfRequest()
3733
Assert.Equal("StreamJsonRpc.JsonMessageFormatter+InboundJsonRpcRequest", this.serverRpc.LastRequestDispatched?.GetType().FullName);
3834
}
3935

36+
[Fact]
37+
public async Task DispatchRequestTargetMethod()
38+
{
39+
await this.clientRpc.InvokeAsync<string>(nameof(Server.TestMethodAsync));
40+
Assert.Equal(typeof(Server), this.serverRpc.LastTargetMethodDispatched?.TargetObjectType);
41+
Assert.Equal(typeof(Server).GetMethod(nameof(Server.TestMethodAsync)), this.serverRpc.LastTargetMethodDispatched?.TargetMethodInfo);
42+
}
43+
4044
[Fact]
4145
public async Task DelegatedDispatcherCanDispatchInReverseOrderBasedOnTopLevelProperty()
4246
{
@@ -84,7 +88,7 @@ public class DelegatedJsonRpc : JsonRpc
8488
{
8589
private const string MessageOrderPropertyName = "messageOrder";
8690

87-
private AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)> requestSignalQueue = new AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
91+
private readonly AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)> requestSignalQueue = new AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
8892
private int messageCounter = 0;
8993

9094
public DelegatedJsonRpc(IJsonRpcMessageHandler handler)
@@ -101,6 +105,8 @@ public DelegatedJsonRpc(IJsonRpcMessageHandler handler, object target)
101105

102106
public JsonRpcRequest? LastRequestDispatched { get; private set; }
103107

108+
public TargetMethod? LastTargetMethodDispatched { get; private set; }
109+
104110
public async Task FlushRequestQueueAsync(int expectedCount)
105111
{
106112
var requests = new SortedList<int, (TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
@@ -124,6 +130,7 @@ public async Task FlushRequestQueueAsync(int expectedCount)
124130
protected override async ValueTask<JsonRpcMessage> DispatchRequestAsync(JsonRpcRequest request, TargetMethod targetMethod, CancellationToken cancellationToken)
125131
{
126132
this.LastRequestDispatched = request;
133+
this.LastTargetMethodDispatched = targetMethod;
127134
TaskCompletionSource<JsonRpcMessage>? completionTcs = null;
128135

129136
if (this.EnableBuffering)

0 commit comments

Comments
 (0)