Exemplo n.º 1
0
        public List <RpcCallContext> ExtractAndMatchCalls(string json, string service)
        {
            var calls    = new List <RpcCallContext>();
            var reqObj   = this.ParseJsonOrThrow(json);
            var reqArray = this.ExtractCallArrayOrThrow(reqObj);

            foreach (JToken req in reqArray)
            {
                var call = new RpcCallContext();
                call.RawRequestJson = req;
                calls.Add(call);
                try
                {
                    this.FillCallContext(req, call, service);
                }
                catch (JsonRpcInternalErrorException ex)
                {
                    this.logger.LogError(ex.Message, ex);
                    call.Error = RpcError.FromException(ex);
                }
                catch (JsonRpcException ex)
                {
                    call.Error = RpcError.FromException(ex);
                }
                catch (Exception ex)
                {
                    this.logger.LogError($"Error while extrating JsonRpc method.", ex);
                    call.Error = RpcError.FromException(new JsonRpcInternalErrorException(ex));
                }
            }
            return(calls);
        }
Exemplo n.º 2
0
        private void FillCallContext(JToken request, RpcCallContext call, string service)
        {
            this.CheckIfValidJsonRpcOrThrow(request);

            call.MethodName = request["method"].ToString();
            call.Id         = (JToken)request["id"];
            call.CallType   = call.Id == null ? RpcCallType.Notification : RpcCallType.Call;

            var methodEntry = this.register.GetMethodEntry(call.MethodName, service);

            if (methodEntry == null)
            {
                throw new JsonRpcMethodNotFoundException();
            }
            call.RegistryEntry = methodEntry;
            var paramsInfo = methodEntry.MethodInfo.GetParameters();
            var parameters = new List <object>();

            var requestParams = (JContainer)request["params"] ?? new JArray();

            if (requestParams.Type == JTokenType.Array)
            {
                parameters = this.ExtractParamsFromArrayOrThrow(paramsInfo, (JArray)requestParams);
            }
            else
            {
                parameters = this.ExtractParamsFromObjectOrThrow(paramsInfo, (JObject)requestParams);
            }
            call.Parameters = parameters;
        }
Exemplo n.º 3
0
 private void HandleExeption(Exception ex, RpcCallContext call)
 {
     if (ex is JsonRpcException)
     {
         call.Error = RpcError.FromException((JsonRpcException)ex);
     }
     else
     {
         if (this.exceptionHandlers.ContainsKey(ex.GetType()))
         {
             var handler = this.exceptionHandlers[ex.GetType()];
             call.Error = handler(ex);
         }
         else
         {
             this.logger.LogError(0, ex, ex.Message);
             call.Error = RpcError.FromException(new JsonRpcInternalErrorException(ex));
         }
     }
 }