-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathJsonRpcServerServiceCollectionExtensions.cs
214 lines (184 loc) · 9.83 KB
/
JsonRpcServerServiceCollectionExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
using System;
using System.IO.Pipelines;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using DryIoc;
using MediatR;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
namespace OmniSharp.Extensions.JsonRpc
{
public static class JsonRpcServerServiceCollectionExtensions
{
internal static IContainer AddJsonRpcServerCore<T>(this IContainer container, JsonRpcServerOptionsBase<T> options) where T : IJsonRpcHandlerRegistry<T>
{
if (options.Output == null)
{
throw new ArgumentException("Output is missing!", nameof(options));
}
if (options.Input == null)
{
throw new ArgumentException("Input is missing!", nameof(options));
}
container = container.Populate(options.Services);
container.RegisterInstance(options.Output, serviceKey: nameof(options.Output));
container.RegisterInstance(options.Input, serviceKey: nameof(options.Input));
container.RegisterInstance(options.MaximumRequestTimeout, serviceKey: nameof(options.MaximumRequestTimeout));
container.RegisterInstance(options.SupportsContentModified, serviceKey: nameof(options.SupportsContentModified));
container.RegisterInstance(options.Concurrency ?? -1, serviceKey: nameof(options.Concurrency));
if (options.CreateResponseException != null)
{
container.RegisterInstance(options.CreateResponseException);
}
container.RegisterMany<OutputHandler>(
serviceTypeCondition: type => type.IsInterface,
made: Parameters.Of
.Type<PipeWriter>(serviceKey: nameof(options.Output)),
reuse: Reuse.Singleton
);
container.Register<Connection>(
made: new Made.TypedMade<Connection>().Parameters
.Type<PipeReader>(serviceKey: nameof(options.Input))
.Type<TimeSpan>(serviceKey: nameof(options.MaximumRequestTimeout))
.Type<bool>(serviceKey: nameof(options.SupportsContentModified))
.Name("concurrency", serviceKey: nameof(options.Concurrency)),
reuse: Reuse.Singleton
);
container.RegisterMany<ResponseRouter>(
serviceTypeCondition: type => type.IsInterface,
reuse: Reuse.Singleton
);
container.RegisterInstance(options.Handlers);
container.RegisterInitializer<IJsonRpcHandlerCollection>(
(collection, context) => {
foreach (var description in context
.ResolveMany<JsonRpcHandlerDescription>()
.Concat(
context
.ResolveMany<IJsonRpcHandler>().Select(_ => JsonRpcHandlerDescription.Infer(_))
))
{
collection.Add(description);
}
}
);
container.RegisterMany<InstanceHasStarted>(nonPublicServiceTypes: true, reuse: Reuse.Singleton);
return container.AddJsonRpcMediatR();
}
internal static IContainer AddJsonRpcMediatR(this IContainer container)
{
container.RegisterMany(new[] { typeof(IMediator).GetAssembly() }, Registrator.Interfaces, Reuse.ScopedOrSingleton);
container.RegisterMany<RequestContext>(Reuse.Scoped);
container.RegisterDelegate<ServiceFactory>(context => context.Resolve, Reuse.ScopedOrSingleton);
container.Register(typeof(IRequestHandler<,>), typeof(RequestHandler<,>));
container.Register(typeof(IRequestHandler<,>), typeof(RequestHandlerDecorator<,>), setup: Setup.Decorator);
return container;
}
class RequestHandler<T, R> : IRequestHandler<T, R> where T : IRequest<R>
{
private readonly IRequestContext _requestContext;
public RequestHandler(IRequestContext requestContext)
{
_requestContext = requestContext;
}
public Task<R> Handle(T request, CancellationToken cancellationToken)
{
return ((IRequestHandler<T, R>) _requestContext.Descriptor.Handler).Handle(request, cancellationToken);
}
}
class RequestHandlerDecorator<T, R> : IRequestHandler<T, R> where T : IRequest<R>
{
private readonly IRequestHandler<T, R>? _handler;
private readonly IRequestContext? _requestContext;
public RequestHandlerDecorator(IRequestHandler<T, R>? handler = null, IRequestContext? requestContext = null)
{
_handler = handler;
_requestContext = requestContext;
}
public Task<R> Handle(T request, CancellationToken cancellationToken)
{
if (_requestContext == null)
{
if (_handler == null)
{
throw new NotImplementedException($"No request handler was registered for type {typeof(IRequestHandler<T, R>).FullName}");
}
return _handler.Handle(request, cancellationToken);
}
return ((IRequestHandler<T, R>) _requestContext.Descriptor.Handler).Handle(request, cancellationToken);
}
}
internal static IContainer AddJsonRpcServerInternals(this IContainer container, JsonRpcServerOptions options)
{
if (options.Serializer == null)
{
throw new ArgumentException("Serializer is missing!", nameof(options));
}
if (options.Receiver == null)
{
throw new ArgumentException("Receiver is missing!", nameof(options));
}
if (options.RequestProcessIdentifier == null)
{
throw new ArgumentException("RequestProcessIdentifier is missing!", nameof(options));
}
container = container.AddJsonRpcServerCore(options);
container.RegisterInstanceMany(new HandlerTypeDescriptorProvider(options.Assemblies), nonPublicServiceTypes: true);
container.RegisterInstance(options.Serializer);
container.RegisterInstance(options.Receiver);
container.RegisterInstance(options.RequestProcessIdentifier);
container.RegisterInstance(options.OnUnhandledException ?? ( e => { } ));
container.RegisterMany<RequestRouter>(Reuse.Singleton);
container.RegisterMany<HandlerCollection>(
nonPublicServiceTypes: true,
serviceTypeCondition: type => typeof(IHandlersManager) == type || type == typeof(HandlerCollection),
reuse: Reuse.Singleton
);
container.RegisterInitializer<IHandlersManager>(
(manager, context) => {
var descriptions = context.Resolve<IJsonRpcHandlerCollection>();
descriptions.Populate(context, manager);
}
);
container.Register<IJsonRpcServerFacade, DefaultJsonRpcServerFacade>(reuse: Reuse.Singleton);
container.RegisterInstance<IOptionsFactory<JsonRpcServerOptions>>(new ValueOptionsFactory<JsonRpcServerOptions>(options));
container.RegisterMany<JsonRpcServer>(
serviceTypeCondition: type => type == typeof(IJsonRpcServer) || type == typeof(JsonRpcServer),
reuse: Reuse.Singleton,
setup: Setup.With(condition: req => req.IsResolutionRoot || req.Container.Resolve<IInsanceHasStarted>().Started)
);
return container;
}
public static IServiceCollection AddJsonRpcServer(this IServiceCollection services, Action<JsonRpcServerOptions>? configureOptions = null) =>
AddJsonRpcServer(services, Options.DefaultName, configureOptions);
public static IServiceCollection AddJsonRpcServer(this IServiceCollection services, string name, Action<JsonRpcServerOptions>? configureOptions = null)
{
// If we get called multiple times we're going to remove the default server
// and force consumers to use the resolver.
if (services.Any(d => d.ServiceType == typeof(JsonRpcServer) || d.ServiceType == typeof(IJsonRpcServer)))
{
services.RemoveAll<JsonRpcServer>();
services.RemoveAll<IJsonRpcServer>();
services.AddSingleton<IJsonRpcServer>(
_ => throw new NotSupportedException("JsonRpcServer has been registered multiple times, you must use JsonRpcServerResolver instead")
);
services.AddSingleton<JsonRpcServer>(
_ => throw new NotSupportedException("JsonRpcServer has been registered multiple times, you must use JsonRpcServerResolver instead")
);
}
services
.AddOptions()
.AddLogging();
services.TryAddSingleton<JsonRpcServerResolver>();
services.TryAddSingleton(_ => _.GetRequiredService<JsonRpcServerResolver>().Get(name));
services.TryAddSingleton<IJsonRpcServer>(_ => _.GetRequiredService<JsonRpcServerResolver>().Get(name));
if (configureOptions != null)
{
services.Configure(name, configureOptions);
}
return services;
}
}
}