Ejemplo n.º 1
0
        protected virtual async Task <ErrorResponse> RouteRequest(IHandlerDescriptor handler, Request request, CancellationToken token)
        {
            if (request.Method is null)
            {
                return(new MethodNotFound(request.Id, request.Method));
            }

            object @params;

            try
            {
                @params = request.Params.ToObject(handler.Params, _serializer.JsonSerializer);
            }
            catch
            {
                return(new InvalidParams(request.Id));
            }

            var result = ReflectionRequestHandlers.HandleRequest(handler, @params, token);

            await result.ConfigureAwait(false);

            object responseValue = null;

            if (result.GetType().GetTypeInfo().IsGenericType)
            {
                var property = typeof(Task <>)
                               .MakeGenericType(result.GetType().GetTypeInfo().GetGenericArguments()[0]).GetTypeInfo()
                               .GetProperty(nameof(Task <object> .Result), BindingFlags.Public | BindingFlags.Instance);

                responseValue = property.GetValue(result);
            }

            return(new Client.Response(request.Id, responseValue));
        }
Ejemplo n.º 2
0
        public async Task RouteNotification(IHandlerDescriptor handler, Notification notification)
        {
            Task result;

            if (handler.Params is null)
            {
                result = ReflectionRequestHandlers.HandleNotification(handler);
            }
            else
            {
                var @params = notification.Params.ToObject(handler.Params, _serializer.JsonSerializer);
                result = ReflectionRequestHandlers.HandleNotification(handler, @params);
            }
            await result.ConfigureAwait(false);
        }