异步任务功能无法处理按钮单击

问题描述 投票:0回答:1

我正在使用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;
        }
c# asp.net computer-vision
1个回答
1
投票

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

热门问题
推荐问题
最新问题