/// <summary> /// Parses the request and dispatches it to the correct service's method. /// </summary> /// <param name="body">The raw rpc request.</param> /// <param name="cancellationToken">The cancellation token that will be past to the service handler in case it expects/accepts one.</param> /// <returns>The response that, after serialization, is returned as response.</returns> public async Task <string> HandleAsync(string body, CancellationToken cancellationToken) { if (!JsonRpcRequest.TryParse(body, out var jsonRpcRequests, out var isBatch)) { return(JsonRpcResponse.CreateErrorResponse(null, JsonRpcErrorCodes.ParseError).ToJson(DefaultSettings)); } var results = new List <string>(); foreach (var jsonRpcRequest in jsonRpcRequests) { cancellationToken.ThrowIfCancellationRequested(); results.Add(await HandleRequestAsync(jsonRpcRequest, cancellationToken)); } return(isBatch ? $"[{string.Join(",", results)}]" : results[0]); }
private string Error(JsonRpcErrorCodes code, string reason, string id) { var response = JsonRpcResponse.CreateErrorResponse(id, code, reason); return(id is { }