Reputation: 515
I am using Azure Cognitive Services to detect faces in an image in my ASP.NET MVC web application. But its "DetectWithStreamAsync" always throws "An error occurred while sending the request". I took a reference from this project https://github.com/Azure-Samples/Cognitive-Face-CSharp-sample which is WPF app. However, this WPF app does not throw the same exception when I use the same code, subscription key and endpoint URL. The exceptions are thrown only when requests are made from by MVC application.
Edit: I have also tried using Microsoft.ProjectOxford.Face's, "DetectAsync" method, but got the same exception.
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;
}
}
}
}
Upvotes: 0
Views: 777
Reputation: 108
@Sanjay Thanks for the solution. Also the 4.6 version is enough.
I edited the web.config like below and it works!
<system.web>
<compilation targetFramework="4.6" />
<httpRuntime targetFramework="4.6" />
</system.web>
Upvotes: 0
Reputation: 515
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.
Upvotes: 1