public SubRes Post([FromBody] SubReq value) { SubRes result = new SubRes(); try { SubReq numbers = value; if (String.IsNullOrEmpty(Request.Headers["X-Evi-Tracking-Id"])) { result.Difference = value.Minuend - value.Subtrahend; } else { result.Difference = value.Minuend - value.Subtrahend; Log log = new Log("journal_"); log.Add(Request.Headers["X-Evi-Tracking-Id"] + " operation subtraction numbers: " + value.Minuend + " - " + value.Subtrahend + " result: " + result.Difference); } } catch (Exception ex) { Log log = new Log(); log.Add("Error en SubController.cs " + ex.Message); } return(result); }
// // POST: /Calculator/Sub public void Sub() { 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(); Sub subs = JsonConvert.DeserializeObject <Sub>(body); // Get the result Calculator C = new Calculator(); int dif = C.sub(subs.Minuend, subs.Substraend); // Create a Result Object SubRes subres = new SubRes(); subres.Difference = dif; // Serialize it string param = JsonConvert.SerializeObject(subres); // 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("Sub operation"); } 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."); } }