public async Task <HttpResponseMessage> CallFromHttp(IServiceInstance service, HttpRequestMessage request, CancellationToken cancellationToken) { var requestJson = await request.Content.ReadAsStringAsync(); if (string.IsNullOrWhiteSpace(requestJson)) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } var requestPayload = JsonConvert.DeserializeObject(requestJson, service.RequestType) as BaseRequest; if (requestPayload == null) { return(new HttpResponseMessage(HttpStatusCode.BadRequest)); } var responsePayload = await service.Execute(requestPayload, cancellationToken); var responseJson = JsonConvert.SerializeObject(responsePayload); var httpStatusCode = ServiceResultExpander.HttpStatusCode(responsePayload.MetaData.Result); var response = new HttpResponseMessage((HttpStatusCode)httpStatusCode) { Content = new StringContent(responseJson, Encoding.UTF8, "application/json"), }; response.Headers.Add("x-cw-duration", responsePayload.MetaData.DurationMs.ToString()); response.Headers.Add("x-cw-correlationId", responsePayload.MetaData.CorrelationId.ToString()); response.Headers.Add("x-cw-result", responsePayload.MetaData.Result.ToString()); return(response); }
public async Task Execute( HttpContext context, IServiceInstance service, CancellationToken cancellationToken ) { DateTimeOffset start = ServiceClock.CurrentTime(); ServiceResponse output; Guid? correlationId = null; try { var input = await _converter.ConvertRequest(service.RequestType, context.Request, new HttpDataSerializerOptions()); correlationId = input.CorrelationId; output = await service.Execute(input, cancellationToken); } catch (ValidationErrorException vex) { output = new ServiceResponse( new ResponseMetaData( service, ServiceResult.ValidationError, validationErrors: ValidationHelper.Create(vex.Error, vex.Member))); } await _converter.ConvertResponse(output, service.ResponseType, context.Response); }
public async Task <HttpResponseMessage> CallFromHttp <TRequest, TResponse>(IServiceInstance <TRequest, TResponse> service, HttpRequestMessage request, Action <TRequest, RequestMapping> mapping, CancellationToken cancellationToken) where TRequest : BaseRequest, new() where TResponse : class, new() { Stopwatch sw = new Stopwatch(); sw.Start(); var requestJson = await request.Content.ReadAsStringAsync(); var requestPayload = new TRequest(); if (!string.IsNullOrWhiteSpace(requestJson)) { requestPayload = JsonConvert.DeserializeObject <TRequest>(requestJson); } try { mapping(requestPayload, new RequestMapping(request)); } catch (Exception ex) { logger.LogError(ex, "Cannot map object. Skipping the remainder of the mapping"); } var responsePayload = await service.Execute(requestPayload, cancellationToken); var responseJson = JsonConvert.SerializeObject(responsePayload); var httpStatusCode = ServiceResultExpander.HttpStatusCode(responsePayload.MetaData.Result); var response = new HttpResponseMessage((HttpStatusCode)httpStatusCode) { Content = new StringContent(responseJson, Encoding.UTF8, "application/json") }; response.Headers.Add("x-cw-svc-duration", responsePayload.MetaData.DurationMs.ToString()); response.Headers.Add("x-cw-correlationId", responsePayload.MetaData.CorrelationId.ToString()); response.Headers.Add("x-cw-result", responsePayload.MetaData.Result.ToString()); sw.Stop(); response.Headers.Add("x-cw-e2e-duration", sw.ElapsedMilliseconds.ToString()); return(response); }
public async Task <HttpResponseMessage> Handle( HttpRequestMessage request, IServiceInstance service, CancellationToken cancellationToken) { var start = ServiceClock.CurrentTime(); try { var input = await _responseGenerator.ConvertRequest(service.RequestType, request); var output = await service.Execute(input, cancellationToken); var response = await _responseGenerator.ConvertResponse(request, output, service.ResponseType); return(response); } catch (SchemaValidationException ex) { var error = await _responseGenerator.ConvertResponse( request, new ServiceResponse( new ResponseMetaData( service.FullName, service.CorrelationId == default(Guid) ? Guid.Empty : service.CorrelationId, ServiceResult.ValidationError, (long)(ServiceClock.CurrentTime() - start).TotalMilliseconds, ServiceClock.CurrentTime(), validationErrors: ex.Errors ) ), service.ResponseType); return(error); } catch (Exception ex) { var serialEx = ex as CodeWorksSerializationException; string originalPayload = serialEx?.RawData ?? ""; Dictionary <string, string[]> errors = null; if (!string.IsNullOrWhiteSpace(originalPayload)) { errors = new Dictionary <string, string[]> { ["payload"] = new[] { originalPayload } }; } var error = await _responseGenerator.ConvertResponse( request, new ServiceResponse( new ResponseMetaData( service.FullName, service.CorrelationId == default(Guid) ? Guid.Empty : service.CorrelationId, ServiceResult.PermanentError, (long)(ServiceClock.CurrentTime() - start).TotalMilliseconds, ServiceClock.CurrentTime(), validationErrors: errors, exceptionMessage: ex.ToString() ) ), service.ResponseType); return(error); } }