I m building a WebService that make 2 operations. 1. Save data into database SQL 2. Call another method of a microservice by http request.
It are working now but I must wait the response of microservice to return Message to the client. I want change it. I want call microservice method without wait the response of it and send immediately message at client.
This is the method.
[HttpPost]
public async Task<HttpResponseMessage> saveRRAsync(RrDTO u)
{
try
{
log.Debug(JsonConvert.SerializeObject(u, Formatting.Indented));
}
catch (Exception e)
{
}
DateTime dataInvioMicroservice = DateTime.Now;
try
{
if (u.rrData == null || u.rrData.Count <= 0)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError, new RMessageHRV(null,
"Data empty",
true,
(short)status_code.Failure, true, null));
}
DbData dbData = new DbData();
//recupero l'id del paziente dal primo elemento nel json
String patientId = u.rrData[0].patientId;
int idPatient = Int32.Parse(patientId);
int id = getMaxIdFromRR(idPatient);
List<RR> listRr = new List<RR>();
foreach (RrDTO.RrData data in u.rrData)
{
if (data.rr != null && data.rr.Count > 0)
{
foreach (String d in data.rr)
{
dataInvioMicroservice = data.createdDate;
RR ecg = new RR();
ecg.Id = id++;
ecg.IdPerson = idPatient;
ecg.y = Decimal.Parse(d);
ecg.timestamp = data.createdDate;
listRr.Add(ecg);
}
}
}
dbData.RR.AddRange(listRr);
int savedData = dbData.SaveChanges();
//CHIAMO IL MICROSERVICE CHE DOVRÃ ELABORARE IN AUTOMATICO L'HRV
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(endPointMicroservice+ "elaboraHRV");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
MicroserviceDTO mT = new MicroserviceDTO();
mT.applyClearF3A = true;
mT.countryID = "Central European Standard Time";
mT.timeType = 0;
mT.patientId = idPatient;
mT.date = dataInvioMicroservice;
HttpResponseMessage response = await CallMicroservice(mT, client);
if (response != null && response.StatusCode == HttpStatusCode.OK)
{
log.Debug(response.Content);
log.Debug("CHIAMATA AL MICROSERVICE CORRETTO");
}
else
{
{
log.Debug("CHIAMATA AL MICROSERVICE FALLITA");
log.Debug(response.Content);
}
}
return Request.CreateResponse(HttpStatusCode.OK, new RMessageHRV(null,
"Data saved",
true,
(short)status_code.Success, true, null));
}
catch (Exception e)
{
log.Error(string.Format("{0} {1}", e.Message, e.StackTrace));
log.Error(e.InnerException);
log.Error(e.Source);
return Request.CreateResponse(HttpStatusCode.InternalServerError, new RMessageHRV(null,
"Data empty",
true,
(short)status_code.Exception, true, null));
}
}
static async Task<HttpResponseMessage> CallMicroservice(MicroserviceDTO microservice, HttpClient client)
{
HttpResponseMessage response = await client.PostAsJsonAsync(
"", microservice);
response.EnsureSuccessStatusCode();
// return URI of the created resource.
return response;
}
After this code:
HttpResponseMessage response = await CallMicroservice(mT, client);
I want send immediately the response at the client without wait the response of CallMicroservice Method, it is possible do it?