Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从WebService发出异步请求_C#_Rest_Web Services - Fatal编程技术网

C# 从WebService发出异步请求

C# 从WebService发出异步请求,c#,rest,web-services,C#,Rest,Web Services,我正在构建一个可以执行2个操作的Web服务。 1.将数据保存到SQL数据库中 2.通过http请求调用微服务的另一个方法 它现在正在工作,但我必须等待microservice的响应才能将消息返回给客户端。我想换一下。我希望调用microservice方法而不等待它的响应,并立即在客户端发送消息 这就是方法 [HttpPost] public async Task<HttpResponseMessage> saveRRAsync(RrDTO u) {

我正在构建一个可以执行2个操作的Web服务。 1.将数据保存到SQL数据库中 2.通过http请求调用微服务的另一个方法

它现在正在工作,但我必须等待microservice的响应才能将消息返回给客户端。我想换一下。我希望调用microservice方法而不等待它的响应,并立即在客户端发送消息

这就是方法

 [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;
    }
我想立即在客户端发送响应,而不等待CallMicroservice方法的响应,可以吗

 HttpResponseMessage response = await CallMicroservice(mT, client);