异步方法调用是否应该在所有方法调用范围内链接起来?
Should async method calls chain up in all method calls scope?
我正在编写一个 asp.net 核心网络 API,它正在使用另一个第三方 API 并将一些 JSON 响应返回给将成为客户端的调用者网页浏览器。在以异步方式编写我的实现时,visual studio 建议从我的以下异步方法中删除异步等待。
我只是想得到澄清,我不需要将这两个方法包装在异步等待中?
方法如下:
public async Task<T> GetAsync<T>(string url)
{
return await GetResponse<T>(HttpMethod.GET,url);
}
public async Task<T> PostAsync<T>(string url, object payload)
{
return await GetResponse<T>(HttpMethod.POST, url,payload);
}
以下是上述两种方法使用的方法:
public async Task<T> GetResponse<T>(HttpMethod method,string url, object payload = null)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
HttpResponseMessage response;
switch (method)
{
case HttpMethod.POST:
{
var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
response = await client.PostAsync(url, content).ConfigureAwait(false);
break;
}
case HttpMethod.GET:
default:
method = HttpMethod.GET;
response = await client.GetAsync(url).ConfigureAwait(false);
break;
}
var responseMessageString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
_logger.LogInformation($"{method.ToString()} {method.ToString()} {Environment.NewLine} Response: {responseMessageString}");
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseMessageString);
}
以下是 Visual Studio 的建议:
方法声明中的 Async 和 await 可以省略
I just wanted to get clarification that i don't need to wrap these two method in async await?
没错。您可以相信 Visual Studio 和 ReSharper 提出的建议;他们的建议非常保守。
在这种情况下,因为每个方法只是将参数传递给另一个方法和returns相同的事情,所以elide the async
and await
.
是安全的
但是,我认为您没有。省略关键字会给您带来(非常)微小的性能提升。但是如果这些方法做任何不平凡的事情 - 或者 在未来被改变做任何不平凡的事情 - 那么你会想要保留 async
/await
关键字。
我正在编写一个 asp.net 核心网络 API,它正在使用另一个第三方 API 并将一些 JSON 响应返回给将成为客户端的调用者网页浏览器。在以异步方式编写我的实现时,visual studio 建议从我的以下异步方法中删除异步等待。
我只是想得到澄清,我不需要将这两个方法包装在异步等待中?
方法如下:
public async Task<T> GetAsync<T>(string url)
{
return await GetResponse<T>(HttpMethod.GET,url);
}
public async Task<T> PostAsync<T>(string url, object payload)
{
return await GetResponse<T>(HttpMethod.POST, url,payload);
}
以下是上述两种方法使用的方法:
public async Task<T> GetResponse<T>(HttpMethod method,string url, object payload = null)
{
System.Net.Http.HttpClient client = new System.Net.Http.HttpClient();
HttpResponseMessage response;
switch (method)
{
case HttpMethod.POST:
{
var content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
response = await client.PostAsync(url, content).ConfigureAwait(false);
break;
}
case HttpMethod.GET:
default:
method = HttpMethod.GET;
response = await client.GetAsync(url).ConfigureAwait(false);
break;
}
var responseMessageString = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
_logger.LogInformation($"{method.ToString()} {method.ToString()} {Environment.NewLine} Response: {responseMessageString}");
return Newtonsoft.Json.JsonConvert.DeserializeObject<T>(responseMessageString);
}
以下是 Visual Studio 的建议:
方法声明中的 Async 和 await 可以省略
I just wanted to get clarification that i don't need to wrap these two method in async await?
没错。您可以相信 Visual Studio 和 ReSharper 提出的建议;他们的建议非常保守。
在这种情况下,因为每个方法只是将参数传递给另一个方法和returns相同的事情,所以elide the async
and await
.
但是,我认为您没有。省略关键字会给您带来(非常)微小的性能提升。但是如果这些方法做任何不平凡的事情 - 或者 在未来被改变做任何不平凡的事情 - 那么你会想要保留 async
/await
关键字。