Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.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# 异步任务函数在单击按钮时不起作用_C#_Asp.net_Computer Vision - Fatal编程技术网

C# 异步任务函数在单击按钮时不起作用

C# 异步任务函数在单击按钮时不起作用,c#,asp.net,computer-vision,C#,Asp.net,Computer Vision,我正在使用Azure Vision API进行OCR。MVC中的示例代码工作正常,但当我在Asp.net中使用相同的代码时,单击按钮,它就不工作了。没有给出任何响应或错误 response=wait client.PostAsyncuri,content;//没有回应 response=await client.PostAsyncuri,content.ConfigureAwaitfalse;//错误:找不到资源 活动: protected void btnScanCheque_Click(o

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

response=wait client.PostAsyncuri,content;//没有回应

response=await client.PostAsyncuri,content.ConfigureAwaitfalse;//错误:找不到资源

活动:

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;
        }

将事件更改为async with await on GetOCRDetails方法应该会有所帮助。

事件在异步后不会触发您的问题是您在不等待的情况下调用GetOCRDetails。这意味着过程将继续并完成按钮单击的执行,而无需等待回答。正如slamnation建议的那样,您是否尝试过受保护的异步void btnScanCheque_Clickobject sender,EventArgs e>不将其作为任务或它不会执行。我也尝试过,但不起作用您是否添加了任务task=wait imgScan.getocrdails;是,但没有任何更改只是修复了它应该是字符串foo=await imgScan.getocrdestails;。因为您已经获得了字符串形式的结果,而不是另一个任务。