0

I am using Azure Vision API for OCR purpose. The sample code in MVC is working fine but when I use same code in Asp.net on button click it is not working. Not giving any response or giving error.

response = await client.PostAsync(uri, content); // No response

response = await client.PostAsync(uri, content).ConfigureAwait(false); // Error:Resource not found

Event:

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

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

            }
            catch (Exception ex)
            {

            }
        }

Function:

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;
        }
5
  • 1
    Your issue is that you call GetOCRDetails() without awaiting it. Meaning the proces will just continue and finish the execution of the button click without waiting for a answer. As slamnation suggested did you try protected async void btnScanCheque_Click(object sender, EventArgs e) > dont make it a task or it wont execute. Commented Mar 1, 2019 at 7:36
  • I tried that as well but not working Commented Mar 1, 2019 at 9:02
  • Did you add Task<string> task = await imgScan.GetOCRDetails(); Commented Mar 1, 2019 at 9:03
  • Yes but nothing changed Commented Mar 1, 2019 at 9:27
  • Just a fix it should be string foo = await imgScan.GetOCRDetails();. Since your getting already the result as string and not another task. Commented Mar 1, 2019 at 9:39

1 Answer 1

0

Changing the event to async with await on GetOCRDetails method should help.

Sign up to request clarification or add additional context in comments.

1 Comment

Event is not firing after making it async

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.