Example #1
0
        private async Task <bool> ParseRequestAsync(HttpRequest req, HttpResponse res, HttpRequestData requestData, RouteItem router)
        {
            bool             result = false;
            IHttpParsePlugin plugin = null;

            if (router.Plugin is IHttpParsePlugin parsePlugin)
            {
                plugin = parsePlugin;
            }

            requestData.MessageId = router.MessageId;
            requestData.ServiceId = router.ServiceId;

            if (plugin != null)
            {
                result = await plugin.ParseAsync(req, res, requestData, router);

                CollectCommonData(req, requestData.QueryOrFormData);
                return(result);
            }


            try
            {
                ProcessRequestData(req, router, requestData);
                CollectCommonData(req, requestData.QueryOrFormData);
            }
            catch (Exception ex)
            {
                res.StatusCode = (int)HttpStatusCode.InternalServerError;
                await res.WriteAsync("InternalServerError:" + ex.Message);

                _logger.LogError(ex, "cast http request to rpc error");
                return(true);
            }


            return(result);
        }
Example #2
0
        private async Task <bool> InvokeInner(HttpContext context)
        {
            var req = context.Request;
            var res = context.Response;

            bool hasRes;
            var  router = FindRouter(req);

            if (router == null) //not match any router
            {
                this._logger.LogWarning("[{0}:{1}] does not match any router", req.Path, req.Method);
                return(false);
            }

            var requestData = new HttpRequestData();

            hasRes = await ParseRequestAsync(req, res, requestData, router);

            if (hasRes)
            {
                return(hasRes);
            }

            hasRes = await BeforeAsyncCall(req, res, requestData);

            if (hasRes)
            {
                return(hasRes);
            }
            var parameters = router.InvokeMethod.GetParameters();

            if (parameters.Length != 1 && parameters.Length != 2)
            {
                res.StatusCode = (int)HttpStatusCode.InternalServerError;
                await res.WriteAsync("parameters length not match");

                this._logger.LogError("parameters length not match");
                return(true);
            }

            var reqType = parameters[0].ParameterType;

            try
            {
                object invokeParam = ParseInvokeParameter(requestData, reqType);
                object retVal      = router.InvokeMethod.Invoke(router.InvokeService,
                                                                parameters.Length == 1 ? new [] { invokeParam } : new [] { invokeParam, parameters[1].DefaultValue });

                if (retVal == null)
                {
                    res.StatusCode = (int)HttpStatusCode.InternalServerError;
                    await res.WriteAsync("InternalServerError");

                    this._logger.LogError("InternalServerError:call rpc method and return null");
                    return(true);
                }

                hasRes = await ProcessOutput(req, res, router, retVal);
            }
            catch (Exception ex)
            {
                res.StatusCode = (int)HttpStatusCode.InternalServerError;
                await res.WriteAsync($"InternalServerError:{ex.Message}");

                this._logger.LogError(ex, "call error:");
                hasRes = true;
            }

            return(hasRes);
        }
Example #3
0
 protected virtual Task <bool> BeforeAsyncCall(HttpRequest req, HttpResponse res, HttpRequestData requestData)
 {
     return(Task.FromResult(false));
 }