Esempio n. 1
0
        public override Task <AddRes> AddAsync(AddReq req)
        {
            var res = new AddRes();

            res.C = req.A + req.B;
            return(Task.FromResult(res));
        }
Esempio n. 2
0
        public override Task <RpcResult <AddRes> > AddAsync(AddReq req)
        {
            var res = new AddRes();

            res.C = req.A + req.B;
            return(Task.FromResult(new RpcResult <AddRes>()
            {
                Data = res
            }));
        }
Esempio n. 3
0
        public override async Task <RpcResult <AddRes> > PlusAsync(AddReq req)
        {
            //await Task.Delay(3000);

            string response = await _req.GetRequest();

            Console.WriteLine("response:{0}", response);
            var res = new AddRes();

            res.C = req.A + req.B;
            return(new RpcResult <AddRes>()
            {
                Data = res
            });
        }
Esempio n. 4
0
        public AddRes Post([FromBody] AddReq value)
        {
            //Retorna BadRequest si la cantidad de operandos es menor a 2
            if (value.Addends.Length < 1)
            {
                //throw new HttpResponseException(HttpStatusCode.BadRequest);
            }
            AddRes result = new AddRes();

            result.Sum = 0;
            try
            {
                AddReq numbers = value;

                /*  Realiza la suma para cada uno de los numeros dentro de la peticion,
                 *  cuando el encabezado no contiene el identificador "X-Evi-Tracking-Id"
                 */
                if (String.IsNullOrEmpty(Request.Headers["X-Evi-Tracking-Id"]))
                {
                    foreach (float number in numbers.Addends)
                    {
                        result.Sum += number;
                    }
                }

                /* Si el encabezado contiene el "X-Evi-Tracking-Id" se efectua la operacion
                 * y se crea un archivo con el nombre del id del usuario y los datos de la
                 * operacion realizada
                 */
                else
                {
                    string operation = "";
                    foreach (float number in numbers.Addends)
                    {
                        result.Sum += number;
                        operation  += number + " + ";
                    }
                    LogJournal logJournal = new LogJournal(Request.Headers["X-Evi-Tracking-Id"]);
                    logJournal.Add("{" + "\"" + "Operation" + "\"" + ": " + "\"" + "Sum" + "\"" + ", " + "\"" + "Calculation" + "\"" + ": " + "\"" + operation.Substring(0, operation.Length - 3) + " = " + result.Sum + "\"" + ", " + "\"" + "Date" + "\"" + ": " + "\"" + DateTime.Now.ToString("u") + "\"" + "}");
                }
            }
            catch (Exception ex) {
                Log log = new Log();
                log.Add("Error en AddController.cs " + ex.Message);
            }

            return(result);
        }
Esempio n. 5
0
        public Task <AmpMessage> AddAsync(AmpMessage reqMsg)
        {
            var rsp = AmpMessage.CreateResponseMessage(reqMsg.ServiceId, reqMsg.MessageId);

            AddReq req = MessagePackSerializer.Deserialize <AddReq>(reqMsg.Data);

            if (req != null)
            {
                var res = new AddRes();
                res.C = req.A + req.B;

                rsp.Data = MessagePackSerializer.Serialize(res);

                Logger.Debug("{0}${1}${2}  ,code=0  ,req={3}  ,res={4}", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, MessagePackSerializer.ToJson(req), MessagePackSerializer.ToJson(res));
            }
            else
            {
                rsp.Code = ErrorCodes.CODE_INTERNAL_ERROR;
                Logger.Error("{0}${1}${2}  ,code={3}  ,req={4}  ,res=null", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, rsp.Code, MessagePackSerializer.ToJson(req));
            }
            return(Task.FromResult(rsp));
        }
Esempio n. 6
0
        public Task <AmpMessage> AddAsync(AmpMessage reqMsg)
        {
            var rsp = AmpMessage.CreateResponseMessage(reqMsg.ServiceId, reqMsg.MessageId);



            AddReq req = AddReq.Parser.ParseFrom(reqMsg.Data);

            if (req != null)
            {
                var res = new AddRes();
                res.C    = req.A + req.B;
                rsp.Data = res.ToByteArray();
                Logger.Debug("{0}${1}${2}  ,code=0  ,req={3}  ,res={4}", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, req.ToString(), res.ToString());
            }
            else
            {
                rsp.Code = ErrorCodes.CODE_INTERNAL_ERROR;
                Logger.Error("{0}${1}${2}  ,code={3}  ,req={4}  ,res=null", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, rsp.Code, req.ToString());
            }
            return(Task.FromResult(rsp));
        }
Esempio n. 7
0
        public Task <AmpMessage> AddAsync(AmpMessage reqMsg)
        {
            var rsp = AmpMessage.CreateResponseMessage(reqMsg.ServiceId, reqMsg.MessageId);


            var    json = Encoding.UTF8.GetString(reqMsg.Data);
            AddReq req  = JSON.Deserialize <AddReq>(json);

            if (req != null)
            {
                var res = new AddRes();
                res.C = req.A + req.B;
                var resJson = JSON.Serialize(res);
                rsp.Data = Encoding.UTF8.GetBytes(resJson);
                Logger.Debug("{0}${1}${2}  ,code=0  ,req={3}  ,res={4}", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, json, resJson);
            }
            else
            {
                rsp.Code = ErrorCodes.CODE_INTERNAL_ERROR;
                Logger.Error("{0}${1}${2}  ,code={3}  ,req={4}  ,res=null", reqMsg.ServiceId, reqMsg.MessageId, reqMsg.Sequence, rsp.Code, json);
            }
            return(Task.FromResult(rsp));
        }
        //
        // POST: /Calculator/Add

        public void Add()
        {
            try
            {
                // Receive and process the POST data
                string       res            = HttpContext.Request.Method.ToString();
                Encoding     enc            = System.Text.Encoding.GetEncoding("utf-8");
                StreamReader responseStream = new StreamReader(HttpContext.Request.Body, enc);
                string       body           = responseStream.ReadToEnd();

                Add add = JsonConvert.DeserializeObject <Add>(body);

                // Get the result
                Calculator C   = new Calculator();
                int        sum = C.add(add.Addends);

                // Create a Result Object
                AddRes addres = new AddRes();
                addres.Sum = sum;

                // Serialize it
                string param = JsonConvert.SerializeObject(addres);


                // Send it back using POST
                HttpContext.Response.ContentType = "application/json";
                using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc))
                {
                    streamWriter.Write(param);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var logger = NLog.LogManager.GetCurrentClassLogger();
                logger.Info("Add operation");
            } catch (JsonReaderException je)
            {
                // Create a Result Object
                CalculatorService.Client.Exception ex = new CalculatorService.Client.Exception();
                ex.ErrorCode    = "InternalError";
                ex.ErrorStatus  = 500;
                ex.ErrorMessage = je.Message;

                // Serialize it
                string exec = JsonConvert.SerializeObject(ex);

                // Send it back using POST
                HttpContext.Response.ContentType = "application/json";
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc))
                {
                    streamWriter.Write(exec);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var logger = NLog.LogManager.GetCurrentClassLogger();
                logger.Info("Unexpected character encountered while parsing value.");
            }
            catch (NullReferenceException npe) {
                // Create a Result Object
                CalculatorService.Client.Exception ex = new CalculatorService.Client.Exception();
                ex.ErrorCode    = "InternalError";
                ex.ErrorStatus  = 500;
                ex.ErrorMessage = npe.Message;

                // Serialize it
                string exec = JsonConvert.SerializeObject(ex);

                // Send it back using POST
                HttpContext.Response.ContentType = "application/json";
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc))
                {
                    streamWriter.Write(exec);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var logger = NLog.LogManager.GetCurrentClassLogger();
                logger.Info("Object reference not set to an instance of an object.");
            }
            catch (System.Exception e)
            {
                // Create a Result Object
                CalculatorService.Client.Exception ex = new CalculatorService.Client.Exception();
                ex.ErrorCode    = "InternalError";
                ex.ErrorStatus  = 500;
                ex.ErrorMessage = e.Message;

                // Serialize it
                string exec = JsonConvert.SerializeObject(ex);

                // Send it back using POST
                HttpContext.Response.ContentType = "application/json";
                Encoding enc = System.Text.Encoding.GetEncoding("utf-8");
                using (var streamWriter = new StreamWriter(HttpContext.Response.Body, enc))
                {
                    streamWriter.Write(exec);
                    streamWriter.Flush();
                    streamWriter.Close();
                }

                var logger = NLog.LogManager.GetCurrentClassLogger();
                logger.Info("Object reference not set to an instance of an object.");
            }
        }