使用调用另一个 Task 方法的异步 Task 方法 - 可以只使用一个任务吗?
Using an async Task method that calls another Task method - possible to use only one task?
我编写了一个通用的异步方法,用于从 Web API
获取 json
private static async Task<T> WebReq<T>(string url, string method)
{
// Init a HttpWebRequest for the call
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = method;
using (var memoryStream = new MemoryStream())
{
// Send request to the internet and wait for the response
using (var response = await httpWebRequest.GetResponseAsync())
{
// Get the datastream
using (var responseStream = response.GetResponseStream())
{
// Read bytes in the responseStream and copy them to the memoryStream
await responseStream.CopyToAsync(memoryStream);
}
}
// Read from the memoryStream
using (var streamReader = new StreamReader(memoryStream))
{
var result = await streamReader.ReadToEndAsync();
return JsonConvert.DeserializeObject<T>(result);
}
}
}
我的所有方法都将使用此通用方法来调用 API,例如
public static async Task<Dictionary<string, string>> GetExampleDictAsync(string id)
{
string url = baseUrl + "GetExampleDictionary/" + id;
return await WebReq<Dictionary<string, string>>(url, "POST");
}
据我了解,这将创建 2 个任务。如果我每次都写出 WebReq 的内容,那么每次调用只有 1 个任务......我如何使用我的通用方法并只启动一个任务?
不等待就返回WebReq这么简单吗?
我觉得这很好,我可能不会更改它。如果您担心,可以将第二种方法的签名更改为:
public static Task<Dictionary<string, string>> GetExampleDictAsync(string id)
{
string url = baseUrl + "GetExampleDictionary/" + id;
return WebReq<Dictionary<string, string>>(url, "POST");
}
然后您只需返回由您的内部方法创建的任务,您可以在调用方中等待它 - 无需在此方法中等待它,因此它不需要异步。
但是,如果此方法在 调用 WebReq
之后需要执行任何操作,那么它会受益于异步,因此我会在更改之前考虑这一点它。
我编写了一个通用的异步方法,用于从 Web API
获取 jsonprivate static async Task<T> WebReq<T>(string url, string method)
{
// Init a HttpWebRequest for the call
var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = method;
using (var memoryStream = new MemoryStream())
{
// Send request to the internet and wait for the response
using (var response = await httpWebRequest.GetResponseAsync())
{
// Get the datastream
using (var responseStream = response.GetResponseStream())
{
// Read bytes in the responseStream and copy them to the memoryStream
await responseStream.CopyToAsync(memoryStream);
}
}
// Read from the memoryStream
using (var streamReader = new StreamReader(memoryStream))
{
var result = await streamReader.ReadToEndAsync();
return JsonConvert.DeserializeObject<T>(result);
}
}
}
我的所有方法都将使用此通用方法来调用 API,例如
public static async Task<Dictionary<string, string>> GetExampleDictAsync(string id)
{
string url = baseUrl + "GetExampleDictionary/" + id;
return await WebReq<Dictionary<string, string>>(url, "POST");
}
据我了解,这将创建 2 个任务。如果我每次都写出 WebReq 的内容,那么每次调用只有 1 个任务......我如何使用我的通用方法并只启动一个任务?
不等待就返回WebReq这么简单吗?
我觉得这很好,我可能不会更改它。如果您担心,可以将第二种方法的签名更改为:
public static Task<Dictionary<string, string>> GetExampleDictAsync(string id)
{
string url = baseUrl + "GetExampleDictionary/" + id;
return WebReq<Dictionary<string, string>>(url, "POST");
}
然后您只需返回由您的内部方法创建的任务,您可以在调用方中等待它 - 无需在此方法中等待它,因此它不需要异步。
但是,如果此方法在 调用 WebReq
之后需要执行任何操作,那么它会受益于异步,因此我会在更改之前考虑这一点它。