【问题标题】:Async Task Function not working on Button click异步任务功能在按钮单击时不起作用
【发布时间】:2019-03-01 07:04:36
【问题描述】:

我将 Azure Vision API 用于 OCR。 MVC 中的示例代码工作正常,但是当我在 Asp.net 中使用相同的代码单击按钮时,它不起作用。没有给出任何响应或给出错误。

response = await client.PostAsync(uri, content); // 无响应

response = await client.PostAsync(uri, content).ConfigureAwait(false); // 错误:找不到资源

事件:

protected  void btnScanCheque_Click(object sender, EventArgs e)
        {
            try
            {               

                Task<string> task =  imgScan.GetOCRDetails();

            }
            catch (Exception ex)
            {

            }
        }

功能:

public async Task<string> GetOCRDetails()
        {
            string imageFilePath = @"C:\Projects\OCR Test\ReadImage\Uploads\Cheque_1.JPG";
            var errors = new List<string>();
            string extractedResult = "";
            ImageInfoViewModel responeData = new ImageInfoViewModel();

            try
            {
                HttpClient client = new HttpClient();

                // Request headers.
                client.DefaultRequestHeaders.Add(
                    "Ocp-Apim-Subscription-Key", subscriptionKey);
                // Request parameters.
                string requestParameters = "language=unk&detectOrientation=true";
                // Assemble the URI for the REST API Call.
                string uri = endPoint + "?" + requestParameters;
                HttpResponseMessage response;
                // Request body. Posts a locally stored JPEG image.
                byte[] byteData = GetImageAsByteArray(imageFilePath);
                using (ByteArrayContent content = new ByteArrayContent(byteData))
                {
                    // This example uses content type "application/octet-stream".
                    // The other content types you can use are "application/json"
                    // and "multipart/form-data".
                    content.Headers.ContentType =
                        new MediaTypeHeaderValue("application/octet-stream");
                    // Make the REST API call.
                    response = await client.PostAsync(uri, content);
                }

                // Get the JSON response.
                string result = await response.Content.ReadAsStringAsync();
                //If it is success it will execute further process.
                if (response.IsSuccessStatusCode)
                {
                    // The JSON response mapped into respective view model.
                    responeData = JsonConvert.DeserializeObject<ImageInfoViewModel>(result,
                        new JsonSerializerSettings
                        {
                            NullValueHandling = NullValueHandling.Include,
                            Error = delegate (object sender, Newtonsoft.Json.Serialization.ErrorEventArgs earg)
                            {
                                errors.Add(earg.ErrorContext.Member.ToString());
                                earg.ErrorContext.Handled = true;
                            }
                        }
                    );

                    var linesCount = responeData.regions[0].lines.Count;
                    for (int i = 0; i < linesCount; i++)
                    {
                        var wordsCount = responeData.regions[0].lines[i].words.Count;
                        for (int j = 0; j < wordsCount; j++)
                        {
                            //Appending all the lines content into one.
                            extractedResult += responeData.regions[0].lines[i].words[j].text + " ";
                        }
                        extractedResult += Environment.NewLine;
                    }

                }
            }
            catch (Exception e)
            {
                Console.WriteLine("\n" + e.Message);
            }
            return extractedResult;
        }

【问题讨论】:

  • 您的问题是您调用 GetOCRDetails() 而不等待它。这意味着该过程将继续并完成按钮单击的执行,而无需等待答案。正如 slamnation 建议的那样,您是否尝试过 protected async void btnScanCheque_Click(object sender, EventArgs e) > 不要将其设为任务,否则它不会执行。
  • 我也试过了,但没用
  • 你加了Task&lt;string&gt; task = await imgScan.GetOCRDetails();
  • 是的,但没有任何改变
  • 只是一个修复它应该是string foo = await imgScan.GetOCRDetails();。由于您已经将结果作为字符串而不是另一个任务。

标签: c# asp.net computer-vision


【解决方案1】:

在 GetOCRDetails 方法上将事件更改为与 await 异步应该会有所帮助。

【讨论】:

  • 事件在异步后没有触发
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 2019-11-09
相关资源
最近更新 更多