public async Task <object> ExecutePost()
        {
            try
            {
                var    requestFormat = Request.Headers.GetValues("x-blades-operation-request-type").FirstOrDefault().ToDataFormatEnum();
                var    operationName = Request.Headers.GetValues("x-blades-operation-name").FirstOrDefault();
                string rawData       = await Request.Content.ReadAsStringAsync();

                RequestData = converter.ParseRequestData(rawData, operationName, requestFormat);

                var context = Request.GetOwinContext();
                var user    = new UserInfo(context.Request.User as ClaimsPrincipal);
                user.Location  = context.Request.RemoteIpAddress;
                user.UserAgent = context.Request.Headers.Get("User-Agent");
                var result = await Task.Run(() => executor.Execute(operationName, RequestData, user));

                return(result);
            }
            catch (OperationExecutionException err)
            {
                var response = new HttpResponseMessage(GetHttpStatusCode(err.Status));
                response.Content = new StringContent(string.Join("   \n", err.Errors?.Select(e => e.Message)));
                return(response);
            }
            catch (Exception err)
            {
                log.Error(err);
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                response.Content = new StringContent("FATAL SERVER ERROR");
                return(response);
            }
        }
        public async Task <object> Execute([FromBody] string rawData)
        {
            try
            {
                var requestFormat = Request.Headers["x-blades-operation-request-type"].FirstOrDefault().ToDataFormatEnum();
                var operationName = Request.Headers["x-blades-operation-name"].FirstOrDefault();
                RequestData = converter.ParseRequestData(rawData, operationName, requestFormat);


                var user = new UserInfo(this.User);
                user.Location  = Request.HttpContext.Connection.RemoteIpAddress.ToString();
                user.UserAgent = Request.Headers["User-Agent"].FirstOrDefault();


                var result = await Task.Run(() => executor.Execute(operationName, RequestData, user));

                return(result);
            }
            catch (OperationExecutionException err)
            {
                var response = new HttpResponseMessage(GetHttpStatusCode(err.Status));
                response.Content = new StringContent(string.Join("   \n", err.Errors?.Select(e => e.Message)));
                return(response);
            }
            catch (Exception err)
            {
                log.Error(err);
                var response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
                response.Content = new StringContent("FATAL SERVER ERROR");
                return(response);
            }
        }
Пример #3
0
        public override Task OnMessageReceived(ArraySegment <byte> message, WebSocketMessageType type)
        {
            try
            {
                if (type == WebSocketMessageType.Text)
                {
                    var dataStr       = Encoding.UTF8.GetString(message.Array, message.Offset, message.Count);
                    var dataParts     = dataStr.Split(testMessageSeparator, 3, StringSplitOptions.None);
                    var dataFormat    = dataParts[0].ToDataFormatEnum();
                    var operationName = dataParts[1];
                    var rawData       = dataParts[2];
                    var data          = converter.ParseRequestData(rawData, operationName, dataFormat);

                    Task.Run(() => executor.Execute(operationName, data, User));
                }
                return(base.OnMessageReceived(message, type));
            }
            catch (Exception err)
            {
                log.Error(err);
                return(null);
            }
        }