未等待我内部函数回调的 return

The return of my inner func callback isn't waited

我真的不知道如何命名这个问题,欢迎您编辑它。

这是我的功能:

public static KeyValuePair<bool, string> IsAuthenticated(Token token)
    {
        List<KeyValuePair<string, string>> parameters = new List<KeyValuePair<string, string>>();
        parameters.Add(new KeyValuePair<string, string>("token", token.ToString()));
        parameters.Add(new KeyValuePair<string, string>("creation_date", token.Creation_date));

        //MESSAGE
        if (!Plugin.Connectivity.CrossConnectivity.Current.IsConnected)
            return (new KeyValuePair<bool, string>(false, "No connectivity."));

        else
        {
            HttpHandler.Post(HttpHandler.ApiAddresses.ISAUTHENTICATED, parameters, (object response, bool isOk) =>
            {
                if (isOk)
                {
                    return (new KeyValuePair<bool, string>(isOk, ""));
                }
                else
                {
                    return (new KeyValuePair<bool, string>(isOk, (response as Exception).Message));
                }
            });
        }

        return (new KeyValuePair<bool, string>(false, "isAuthenticated() didn't worked.."));
    }

所以,基本上,我想避免最后一个 return,但如果我这样做,那么我会收到警告 -> All code path doesn't return a value blablabla

但我真正的问题是,我希望我的委托函数(或任何你称之为的东西)达到 return 最终值。

这是 HttpHandle.Post() 函数:

public static async void Post(string url, List<KeyValuePair<string, string>> parameters, Func<object, bool, KeyValuePair<bool, string>> callback)
    {
        try
        {
            HttpClient httpClient = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage();
            httpClient.BaseAddress = new Uri(url);
            var content = new FormUrlEncodedContent(parameters);
            HttpResponseMessage response = await httpClient.PostAsync(url, content);
            callback(await response.Content.ReadAsStringAsync() as object, true);
        }
        catch (Exception e)
        {
            callback(e as object, false);
        }
    }

因为每次我只到达"isAuthenticated() didn't worked.." return..可能我不明白什么或者我完全错了,请给我解释一下..

感谢您的帮助!

我认为 Post 应该 return 一个 Task<KeyValuePair<...>> 而不是 void。然后你也可以制作 IsAuthenticated asyncawait 来自 Post 的结果(应该只是 returning 任何 callback returns) 或者你 可能 可以通过调用 ResultWait 来逃脱,但要注意这是死锁的常见来源 -如果可能的话,最好将 async 一直推到调用堆栈中。

因此,Post 将是:

public static async Task<KeyValuePair<bool, string>> Post(string url, List<KeyValuePair<string, string>> parameters, Func<object, bool, KeyValuePair<bool, string>> callback)
    {
        try
        {
            HttpClient httpClient = new HttpClient();
            HttpRequestMessage request = new HttpRequestMessage();
            httpClient.BaseAddress = new Uri(url);
            var content = new FormUrlEncodedContent(parameters);
            HttpResponseMessage response = await httpClient.PostAsync(url, content);
            return callback(await response.Content.ReadAsStringAsync() as object, true);
        }
        catch (Exception e)
        {
            return callback(e as object, false);
        }
    }

async void的问题是它会在未来的某个时间点完成它的工作并且它没有给你任何方法跟踪并知道何时完成。这就是为什么通常最好避免在事件处理程序之外使用它。