-1

I'm trying to add a face to the faceset in Face++ API. But when I try to make an HTTP post request, it fails on PostAsync method. It just immediately returns to where it was inherited from. As in my example, after executing var response = await client.PostAsync(addUrl, formData), it immediately returns to the Main method, giving me no response at all. Tried to .ConfigureAwait(false), but it didn't help. Maybe you have some ideas why this is happening?

Documentation of the API: https://console.faceplusplus.com/documents/40622182

API:

   public async Task<string> AddFace(string faceToken)
    {
        try
        {
            HttpContent keyContent = new StringContent(Keys.apiKey);
            HttpContent secretContent = new StringContent(Keys.apiSecret);
            HttpContent facesetTokenContent = new StringContent(facesetToken);
            HttpContent faceTokenContent = new StringContent(faceToken);

            using (var formData = new MultipartFormDataContent())
            {
                formData.Add(keyContent, "api_key");
                formData.Add(secretContent, "api_secret");
                formData.Add(facesetTokenContent, "faceset_token");
                formData.Add(faceTokenContent, "face_tokens");

                var response = await client.PostAsync(addUrl, formData).ConfigureAwait(false);

                string responseString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                return responseString;
            }
        }
        catch (Exception e)
        {
            Console.WriteLine(e);

            return null;
        }
    }

MAIN:

    static void Main()
    {
        Bitmap b = new Bitmap(@"C:\dev\ProtingaVizija\SmartVision\d.jpg");
        FaceRecognition fr = new FaceRecognition();

        var face = JsonConvert.DeserializeObject<AnalyzedFaces>(fr.AnalyzeImage(b));
        string faceToken = face.faces[0].face_token;

        Faceset fs = new Faceset("d8d6679e5b37c2612e420f94f0225159");
        fs.AddFace(faceToken);

        string x = fs.GetDetail().Result;
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new FormFaceDetection());
    }
Deivyyyy
  • 27
  • 7

1 Answers1

-1

I've just edited the Main and it works!

string res = fs.AddFace(faceToken).Result;
Deivyyyy
  • 27
  • 7
  • That was [not the correct solution](https://stackoverflow.com/a/24657079/11683). You should have used `await`. – GSerg Oct 01 '18 at 16:37