[HttpPost]
[Authorize("PublicCreateOrder")]
public async Task<ActionResult> CreateOrder(CancellationToken cancellationToken)
{
var watch = System.Diagnostics.Stopwatch.StartNew();
//--------------> Performance measure Start <------------ FROM THIS POINT
StreamReader requestReader = new StreamReader(Request.Body);
JObject jsonRequest;
try
{
jsonRequest = JObject.Parse(await requestReader.ReadToEndAsync());
}
catch (Exception ex)
{
return BadRequest("Please send a metadata has the type of JSON.");
}
var filePath = System.IO.Path.GetDirectoryName(Assembly.GetEntryAssembly().Location) + "\\OrderSchema.json";
using (StreamReader fileReader = new StreamReader(filePath))
{
JSchema jsonSchema = JSchema.Parse(await fileReader.ReadToEndAsync());
IList<ValidationError> errors;
if (!jsonRequest.IsValid(jsonSchema, out errors))
{
List<ValidationError> newErrors = new List<ValidationError>();
new List<ValidationError>(errors).ForEach(error =>
{
if (error.ChildErrors.Count > 0)
newErrors.AddRange(error.ChildErrors);
else
newErrors.Add(error);
});
var errorsItems = errors.Select(error => new GetSchemaValidationResponse
{
Message = error.Message,
ErrorType = error.ErrorType.ToString(),
LineNumber = error.LineNumber,
LinePosition = error.LinePosition,
Path = error.Path
}).ToList();
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
//--------------> Performance measure Finish<------------ TO THIS POINT
return BadRequest(errorsItems);
}
}
CreateOrderRequest request = JsonConvert.DeserializeObject<CreateOrderRequest>(jsonRequest.ToString());
In this code block i apply a json schema validation against an http request with json body. But because of this validation will perform against every request maybe it will cause a performance issues. For now, this code block took under 2ms but i want to improve the performance. Do you have any idea?
And also if you have an advice about code quality, i would be glad to know it.