执行后的PostAsync立即从继承的地方返回

时间:2018-10-01 16:17:24

标签: c# multithreading async-await face-recognition

我正在尝试在Face ++ API中将面孔添加到面孔中。但是,当我尝试发出HTTP发布请求时,它在PostAsync方法上失败。它只是立即返回到其继承位置。在我的示例中,执行var response = await client.PostAsync(addUrl, formData)后,它立即返回Main方法,完全没有响应。尝试使用.ConfigureAwait(false),但没有帮助。也许您有一些想法为什么会这样?

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

主要:

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

1 个答案:

答案 0 :(得分:-1)

我刚刚编辑了Main,并且可以使用!

string res = fs.AddFace(faceToken).Result;