是否可以停止使用异步并在需要的地方等待

我正在从使用异步功能的开发人员实现代码。


例子:


public async Task<string> GetDataAsync(string jsonString = "")

{

    string url = $"{this.url}";


    using (HttpClient httpClient = new HttpClient())

    {

        using (StringContent body = new StringContent(jsonString, Encoding.UTF8, "application/json"))

        {

            httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", this.accessToken);


            using (HttpResponseMessage response = await httpClient.PostAsync(url, body))

            {

                switch (response.IsSuccessStatusCode)

                {

                    case true:


                        return await response.Content.ReadAsStringAsync();


                    default:


                        throw new Exception($"Error calling url: {url}. HTTP status: {response.StatusCode}");


                }

            }

        }

    }

}

而且我不需要,也不想要异步调用任何东西。但是问题是异步函数一直在我的函数中冒泡,所以我不能只是停止使用它,而且由于HttpClient没有Post()要使用的函数,PostAnync()所以我感到被卡在了这个异步笼子中。


有没有技巧可以正常调用异步函数,从而阻止线程在所有父函数中冒泡?


还是找到没有异步功能的程序包的唯一解决方案?


摇曳的蔷薇
浏览 112回答 1
1回答
打开App,查看更多内容
随时随地看视频慕课网APP