简体   繁体   English

Azure 人脸检测 API - 发送请求时出错

[英]Azure Face Detect API - An Error occurred while sending the request

I am using Azure Cognitive Services to detect faces in an image in my ASP.NET MVC web application.我正在使用 Azure 认知服务在我的 ASP.NET MVC web 应用程序中检测图像中的人脸。 But its "DetectWithStreamAsync" always throws "An error occurred while sending the request".但它的“DetectWithStreamAsync”总是抛出“发送请求时发生错误”。 I took a reference from this project https://github.com/Azure-Samples/Cognitive-Face-CSharp-sample which is WPF app.我从这个项目https://github.com/Azure-Samples/Cognitive-Face-CSharp-sample中引用了 WPF 应用程序。 However, this WPF app does not throw the same exception when I use the same code, subscription key and endpoint URL.但是,当我使用相同的代码、订阅密钥和端点 URL 时,此 WPF 应用程序不会引发相同的异常。 The exceptions are thrown only when requests are made from by MVC application.仅当 MVC 应用程序发出请求时才会引发异常。

Edit: I have also tried using Microsoft.ProjectOxford.Face's, "DetectAsync" method, but got the same exception.编辑:我也尝试过使用 Microsoft.ProjectOxford.Face 的“DetectAsync”方法,但遇到了同样的异常。

My Code is as follows:我的代码如下:

using Microsoft.Azure.CognitiveServices.Vision.Face;
using Microsoft.Azure.CognitiveServices.Vision.Face.Models;

namespace MyApplication.FaceRecognition
{
    public class FaceRecognitionService
    {
        private static string key =ConfigurationManager.AppSettings["FaceAPIKey"];
        private static string endPoint =ConfigurationManager.AppSettings["FaceAPIEndPoint"];  

        private readonly IFaceClient faceClient = new FaceClient(
          new ApiKeyServiceClientCredentials(key),
          new System.Net.Http.DelegatingHandler[] { });


        public FaceRecognitionService()
        {  
            faceClient.Endpoint = endPoint;
        }

        public async Task<Guid> DetectFace(string imgPath)
        {
            Guid faceID = Guid.Empty;

            try
            {
                using (Stream faceimagestream = File.OpenRead(imgPath))
                {
                    var faces = await faceClient.Face.DetectWithStreamAsync(faceimagestream,true,false);
                    if (faces.Count > 0)
                        faceID = faces[0].FaceId?? default;
                }

                return faceID;
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }
        
    }
    
}


Upon inspection of inner exception, I got "The underlying connection was closed, an unexpected error occured on a send".检查内部异常后,我得到“底层连接已关闭,发送时发生意外错误”。 It was due to issue with security protocol.Turns out my httpRuntime targetFrameworkwas 4.5, changing it to 4.7 or enabling TLS 1.2 resolved the above error.这是由于安全协议问题。原来我的 httpRuntime targetFramework 是 4.5,将其更改为 4.7 或启用 TLS 1.2 解决了上述错误。

@Sanjay Thanks for the solution. @Sanjay 感谢您的解决方案。 Also the 4.6 version is enough. 4.6版本也足够了。
I edited the web.config like below and it works!我编辑了 web.config 如下所示,它可以工作!

<system.web>
  <compilation targetFramework="4.6" />
  <httpRuntime targetFramework="4.6" />
</system.web>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 发送请求时出错 - An error occurred while sending the request HttpClient:“ HttpRequestException:发送请求时发生错误” - HttpClient: “HttpRequestException: An error occurred while sending the request” 发送请求时发生错误,post - An error occurred while sending the request, post Azure函数为我的一些客户抛出“发送请求时发生错误” - Azure functions throws “An error occurred while sending the request” for some of my customers “发送请求时发生错误”,GetAsync(),Xamarin表单 - “An error occurred while sending the request”, GetAsync(), Xamarin Forms Bot Framework 和 LUIS 发送请求时发生错误 - Bot Framework and LUIS An error occurred while sending the request C# TMDbClient HttpRequestException: 发送请求时发生错误 - C# TMDbClient HttpRequestException: An error occurred while sending the request HttpClient 抛出“发送请求时发生错误。” - HttpClient throwing "An error occurred while sending the request." AggregateException“发生一个或多个错误。发送请求时发生错误。” - AggregateException “One or more errors occurred.An error occurred while sending the request.” 发生一个或多个错误(发送请求时发生错误)-Xamarin.Forms - One or more errors occurred (an error occurred while sending the request) - Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM