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

Expose metadata about TargetMethod publicly #1055

Merged
merged 1 commit into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/StreamJsonRpc/Reflection/TargetMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,20 @@ internal TargetMethod(
}
}

/// <summary>
/// Gets the runtime type of the target object, if there is one.
/// </summary>
/// <remarks>
/// Even when a matching target method is found, there may not be a target <em>object</em>
/// if the target method is <see langword="static" />.
/// </remarks>
public Type? TargetObjectType => this.target?.GetType();

/// <summary>
/// Gets the <see cref="MethodInfo"/> that will be invoked to handle the request, if one was found.
/// </summary>
public MethodInfo? TargetMethodInfo => this.signature?.MethodInfo;

/// <summary>
/// Gets all the exceptions thrown while trying to deserialize arguments to candidate parameter types.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/StreamJsonRpc/net6.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?
2 changes: 2 additions & 0 deletions src/StreamJsonRpc/net8.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?
2 changes: 2 additions & 0 deletions src/StreamJsonRpc/netstandard2.0/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?
2 changes: 2 additions & 0 deletions src/StreamJsonRpc/netstandard2.1/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ StreamJsonRpc.JsonRpc.JoinableTaskTracker.get -> StreamJsonRpc.JsonRpc.JoinableT
StreamJsonRpc.JsonRpc.JoinableTaskTracker.set -> void
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.get -> bool
StreamJsonRpc.RpcMarshalableAttribute.CallScopedLifetime.init -> void
StreamJsonRpc.TargetMethod.TargetMethodInfo.get -> System.Reflection.MethodInfo?
StreamJsonRpc.TargetMethod.TargetObjectType.get -> System.Type?
17 changes: 12 additions & 5 deletions test/StreamJsonRpc.Tests/JsonRpcDelegatedDispatchAndSendTests.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
using System.Diagnostics;
using Microsoft.VisualStudio.Threading;
using StreamJsonRpc;
using StreamJsonRpc.Protocol;
using Xunit;
using Xunit.Abstractions;

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

[Fact]
public async Task DispatchRequestTargetMethod()
{
await this.clientRpc.InvokeAsync<string>(nameof(Server.TestMethodAsync));
Assert.Equal(typeof(Server), this.serverRpc.LastTargetMethodDispatched?.TargetObjectType);
Assert.Equal(typeof(Server).GetMethod(nameof(Server.TestMethodAsync)), this.serverRpc.LastTargetMethodDispatched?.TargetMethodInfo);
}

[Fact]
public async Task DelegatedDispatcherCanDispatchInReverseOrderBasedOnTopLevelProperty()
{
Expand Down Expand Up @@ -84,7 +88,7 @@ public class DelegatedJsonRpc : JsonRpc
{
private const string MessageOrderPropertyName = "messageOrder";

private AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)> requestSignalQueue = new AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
private readonly AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)> requestSignalQueue = new AsyncQueue<(JsonRpcRequest, TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
private int messageCounter = 0;

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

public JsonRpcRequest? LastRequestDispatched { get; private set; }

public TargetMethod? LastTargetMethodDispatched { get; private set; }

public async Task FlushRequestQueueAsync(int expectedCount)
{
var requests = new SortedList<int, (TaskCompletionSource<bool>, Task<JsonRpcMessage>)>();
Expand All @@ -124,6 +130,7 @@ public async Task FlushRequestQueueAsync(int expectedCount)
protected override async ValueTask<JsonRpcMessage> DispatchRequestAsync(JsonRpcRequest request, TargetMethod targetMethod, CancellationToken cancellationToken)
{
this.LastRequestDispatched = request;
this.LastTargetMethodDispatched = targetMethod;
TaskCompletionSource<JsonRpcMessage>? completionTcs = null;

if (this.EnableBuffering)
Expand Down