One of the benefits of the JSON-RPC protocol is that communication can be fully bi-directional. Through some sort of interface agreement, like LSP / DAP, both sides can act as both client (sender) and server (reciever).
JsonRpcServer can be created through two methods.
JsonRpcServer.Create(options => {})This will create a server where you provide options, handlers and more. An optionalIServiceProvidercan be provided that will be used as a fallback container whenIJsonRcpHandlersare being resolved.
services.AddJsonRpcServer([string name, ], options => {})This will addJsonRpcServerto your service collection, or any number of namedJsonRpcServers.
- In the event that you add multiple named servers, they must be resolved using
JsonRpcServerResolver.
When created through Microsoft DI the server will use the IServiceProvider as a fallback when resolving IJsonRpcHandlers.
Some of the important options include...
options.WithInput()takes an inputStreamorPipeReaderoptions.WithOutput()takes an outputStreamorPipeWriteroptions.WithAssemblies()takes additional assemblies that will participate in scanning operations.- Sometimes we scan this list of assemblies for potential strongly typed requests and notifications
options.AddHandlerallows you to add handlers that implementIJsonRpcNotificationHandler<>,IJsonRpcRequestHandler<>,IJsonRpcRequestHandler<,>.- Handlers can be added as an instance, type or a factory that is given a
IServiceProvider
- Handlers can be added as an instance, type or a factory that is given a
options.OnNotification/options.OnRequest- These methods can be used to create handler delegates without having to implement the request interfaces.
options.OnJsonNotification/options.OnJsonRequest- These methods can be used to create handler delegates without having to implement the request interfaces.
- These json
JTokenan the request / response types.
options.WithMaximumRequestTimeout()- Sets the maximum timeout before a request is cancelled
- Defaults to 5 minutes
options.WithSupportsContentModified()- Sets the into a special model that is used more for Language Server Protocol.
- In this mode any
Serialrequest will cause any outstandingParallelrequests will be cancelled with an error message.
options.WithRequestProcessIdentifier()- This allows you to control how requests are "identified" or how they will behave (Serial / Parallel)
We have some helper classes that can be used to aid in Unit Testing such as JsonRpcServerTestBase.
Handlers can be implemented as classes that implement IJsonRpcNotificationHandler<>, IJsonRpcRequestHandler<>, IJsonRpcRequestHandler<,> or as delegates on the JsonRpcServer itself.
Additionally handlers can be dynamically added and removed after the server has been initialized by using the registry.
server.Register(registry => {}) will return an IDisposable that can be used to remove all the handlers that were registered at that time.