示例#1
0
 public void Validate(IRpcSessionContext context)
 {
     if (context.Request.Method != HttpMethod.Post)
     {
         throw new InvalidRpcRequestException("Only 'POST' method is accepted.");
     }
 }
示例#2
0
            public void Validate(IRpcSessionContext context)
            {
                var headers = context.Request.Headers;

                if (!headers.ContainsKey("Content-Length"))
                {
                    throw new InvalidRpcRequestException("'Content-Length' header is missing.");
                }

                if (!int.TryParse(headers["Content-Length"], out var contentLength) || contentLength <= 0)
                {
                    throw new InvalidRpcRequestException("'Content-Length' header is incorrect.");
                }

                if (!headers.ContainsKey("Content-Type"))
                {
                    throw new InvalidRpcRequestException("'Content-Type' header is missing.");
                }

                var contentType = headers["Content-Type"];

                if (contentType != ProtocolConstants.ContentType)
                {
                    throw new InvalidRpcRequestException("'Content-Type' header is incorrect.");
                }
            }
示例#3
0
        protected static void ReportError([NotNull] IRpcSessionContext context, [NotNull] Exception ex, [CanBeNull] object id = null)
        {
            var err = JsonRpcError.CreateEmpty();

            err.Id            = id;
            err.Error.Code    = ex.HResult;
            err.Error.Message = ex.Message;

            var response = context.Response;

            response.StatusCode = (int)HttpStatusCode.OK;
            response.SetBody(err);
        }
示例#4
0
        private void HandleTestAdd([NotNull] IRpcSessionContext context)
        {
            var body = (JsonRpcRequest)context.Request.GetRequestBody();

            try {
                Deconstruct(body.Params, out int a, out int b);

                var result = ServiceProvider.TestAdd(a, b);

                ReportResult(context, result, body.Id);
            } catch (Exception ex) {
                ReportError(context, ex, body.Id);
            }
        }
示例#5
0
        protected static void ReportResult <T>([NotNull] IRpcSessionContext context, [CanBeNull] T result, [CanBeNull] object id = null)
        {
            var resp = JsonRpcResponse.CreateEmpty();

            if (ReferenceEquals(result, null))
            {
                resp.Result = JValue.CreateNull();
            }
            else
            {
                resp.Result = JToken.FromObject(result);
            }

            resp.Id = id;

            var response = context.Response;

            response.StatusCode = (int)HttpStatusCode.OK;
            response.SetBody(resp);
        }