Skip to content

Commit 711bcef

Browse files
fixed all unit tests and wired in mediatr
1 parent 743b2f8 commit 711bcef

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+761
-316
lines changed

Common.Build.props

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
<SourceLink_Version>2.8.1</SourceLink_Version>
1313
<System_Reactive_Version>3.1.1</System_Reactive_Version>
1414
<MediatR_Version>4.1.0</MediatR_Version>
15+
<Autofac_Version>4.8.0</Autofac_Version>
16+
<Autofac_Extensions_DependencyInjection_Version>4.2.2</Autofac_Extensions_DependencyInjection_Version>
1517
<Microsoft_NET_Test_Sdk_Version>15.7.0</Microsoft_NET_Test_Sdk_Version>
1618
<xunit_Version>2.3.1</xunit_Version>
1719
<FluentAssertions_Version>5.3.0</FluentAssertions_Version>

src/JsonRpc/EmptyRequest.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using MediatR;
2+
3+
namespace OmniSharp.Extensions.JsonRpc
4+
{
5+
public class EmptyRequest : IRequest
6+
{
7+
private EmptyRequest() { }
8+
public static EmptyRequest Instance { get; } = new EmptyRequest();
9+
}
10+
}

src/JsonRpc/HandlerCollection.cs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Reflection;
6+
using MediatR;
67

78
namespace OmniSharp.Extensions.JsonRpc
89
{
@@ -14,19 +15,21 @@ internal class HandlerInstance : IHandlerDescriptor, IDisposable
1415
{
1516
private readonly Action _disposeAction;
1617

17-
public HandlerInstance(string method, IJsonRpcHandler handler, Type handlerInterface, Type @params, Action disposeAction)
18+
public HandlerInstance(string method, IJsonRpcHandler handler, Type handlerInterface, Type @params, Type response, Action disposeAction)
1819
{
1920
_disposeAction = disposeAction;
2021
Handler = handler;
2122
Method = method;
2223
HandlerType = handlerInterface;
2324
Params = @params;
25+
Response = response;
2426
}
2527

2628
public IJsonRpcHandler Handler { get; }
2729
public Type HandlerType { get; }
2830
public string Method { get; }
2931
public Type Params { get; }
32+
public Type Response { get; }
3033

3134
public void Dispose()
3235
{
@@ -61,12 +64,17 @@ public IDisposable Add(string method, IJsonRpcHandler handler)
6164
var @interface = GetHandlerInterface(type);
6265

6366
Type @params = null;
67+
Type response = null;
6468
if (@interface.GetTypeInfo().IsGenericType)
6569
{
6670
@params = @interface.GetTypeInfo().GetGenericArguments()[0];
71+
var requestInterface = @params.GetInterfaces()
72+
.FirstOrDefault(x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IRequest<>));
73+
if (requestInterface != null)
74+
response = requestInterface.GetGenericArguments()[0];
6775
}
6876

69-
var h = new HandlerInstance(method, handler, @interface, @params, () => Remove(handler));
77+
var h = new HandlerInstance(method, handler, @interface, @params, response, () => Remove(handler));
7078
_handlers.Add(h);
7179
return h;
7280
}

src/JsonRpc/IHandlerDescriptor.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ public interface IHandlerDescriptor
88
IJsonRpcHandler Handler { get; }
99
Type HandlerType { get; }
1010
Type Params { get; }
11+
Type Response { get; }
1112
}
1213
}

src/JsonRpc/IJsonRpcNotificationHandler.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
namespace OmniSharp.Extensions.JsonRpc
55
{
6-
public interface IJsonRpcNotificationHandler : IRequestHandler<IRequest>, IJsonRpcHandler { }
6+
public interface IJsonRpcNotificationHandler : IRequestHandler<EmptyRequest>, IJsonRpcHandler { }
77

88
public interface IJsonRpcNotificationHandler<in TNotification> : IRequestHandler<TNotification>, IJsonRpcHandler
99
where TNotification : IRequest

src/JsonRpc/IReciever.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using Newtonsoft.Json.Linq;
33
using OmniSharp.Extensions.JsonRpc.Server;
44

@@ -9,4 +9,4 @@ public interface IReciever
99
(IEnumerable<Renor> results, bool hasResponse) GetRequests(JToken container);
1010
bool IsValid(JToken container);
1111
}
12-
}
12+
}

src/JsonRpc/IRequestContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OmniSharp.Extensions.JsonRpc
2+
{
3+
public interface IRequestContext
4+
{
5+
IHandlerDescriptor Descriptor { get; set; }
6+
}
7+
}

src/JsonRpc/JsonRpc.csproj

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.0</TargetFrameworks>
44
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -9,5 +9,7 @@
99
<PackageReference Include="MediatR" Version="$(MediatR_Version)" />
1010
<PackageReference Include="Newtonsoft.Json" Version="$(Newtonsoft_Version)" />
1111
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="$(Microsoft_Extensions_Logging_Version)" />
12+
<PackageReference Include="MediatR.Extensions.Microsoft.DependencyInjection" Version="$(MediatR_Version)" />
13+
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="$(Microsoft_Extensions_DependencyInjection_Version)" />
1214
</ItemGroup>
1315
</Project>
Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System.Linq;
12
using System.Reflection;
23
using System.Threading;
34
using System.Threading.Tasks;
@@ -8,36 +9,50 @@ namespace OmniSharp.Extensions.JsonRpc
89
{
910
public static class MediatRHandlers
1011
{
11-
private static readonly MethodInfo PublishNotificationMethod = typeof(MediatRHandlers)
12-
.GetMethod(nameof(PublishNotification), BindingFlags.NonPublic | BindingFlags.Static);
12+
private static readonly MethodInfo SendRequestUnit = typeof(MediatRHandlers)
13+
.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
14+
.Where(x => x.Name == nameof(SendRequest))
15+
.First(x => x.GetGenericArguments().Length == 1);
1316

14-
private static readonly MethodInfo SendRequestMethod = typeof(MediatRHandlers)
15-
.GetMethod(nameof(SendRequest), BindingFlags.NonPublic | BindingFlags.Static);
17+
private static readonly MethodInfo SendRequestResponse = typeof(MediatRHandlers)
18+
.GetMethods(BindingFlags.NonPublic | BindingFlags.Static)
19+
.Where(x => x.Name == nameof(SendRequest))
20+
.First(x => x.GetGenericArguments().Length == 2);
1621

1722
public static Task HandleNotification(IMediator mediator, IHandlerDescriptor handler, object @params, CancellationToken token)
1823
{
19-
return (Task)PublishNotificationMethod
20-
.MakeGenericMethod(handler.Params ?? typeof(INotification))
24+
return (Task)SendRequestUnit
25+
.MakeGenericMethod(handler.Params ?? typeof(EmptyRequest))
2126
.Invoke(null, new object[] { mediator, @params, token });
2227
}
2328

24-
private static Task PublishNotification<T>(IMediator mediator, T notification, CancellationToken token)
25-
where T : INotification
26-
{
27-
return mediator.Publish(notification, token);
28-
}
29-
3029
public static Task HandleRequest(IMediator mediator, IHandlerDescriptor handler, object @params, CancellationToken token)
3130
{
32-
return (Task)SendRequestMethod
33-
.MakeGenericMethod(handler.Params ?? typeof(INotification))
34-
.Invoke(null, new object[] { mediator, @params, token });
31+
if (handler.HandlerType.GetInterfaces().Any(x =>
32+
x.IsGenericType && typeof(IRequestHandler<>).IsAssignableFrom(x.GetGenericTypeDefinition())))
33+
{
34+
return (Task)SendRequestUnit
35+
.MakeGenericMethod(handler.Params)
36+
.Invoke(null, new object[] { mediator, @params, token });
37+
}
38+
else
39+
{
40+
return (Task)SendRequestResponse
41+
.MakeGenericMethod(handler.Params, handler.Response)
42+
.Invoke(null, new object[] { mediator, @params, token });
43+
}
3544
}
3645

3746
private static Task SendRequest<T>(IMediator mediator, T request, CancellationToken token)
3847
where T : IRequest
3948
{
4049
return mediator.Send(request, token);
4150
}
51+
52+
private static Task<TResponse> SendRequest<T, TResponse>(IMediator mediator, T request, CancellationToken token)
53+
where T : IRequest<TResponse>
54+
{
55+
return mediator.Send(request, token);
56+
}
4257
}
4358
}

src/JsonRpc/RequestContext.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace OmniSharp.Extensions.JsonRpc
2+
{
3+
class RequestContext : IRequestContext
4+
{
5+
public IHandlerDescriptor Descriptor { get; set; }
6+
}
7+
}

0 commit comments

Comments
 (0)