Situation is pretty simple - I wrote an extension method and made it async with return type Task<T>
. But when I try to call it using await, compiler throws an error which suggests that the extension method wasn't recognized as async at all.
Here's my code:
public static async Task<NodeReference<T>> CreateWithLabel<T>(this GraphClient client, T source, String label) where T: class
{
var node = client.Create(source);
var url = string.Format(ConfigurationManager.AppSettings[configKey] + "/node/{0}/labels", node.Id);
var serializedLabel = string.Empty;
using (var tempStream = new MemoryStream())
{
new DataContractJsonSerializer(label.GetType()).WriteObject(tempStream, label);
serializedLabel = Encoding.Default.GetString(tempStream.ToArray());
}
var bytes = Encoding.Default.GetBytes(serializedLabel);
using (var webClient = new WebClient())
{
webClient.Headers.Add("Content-Type", "application/json");
webClient.Headers.Add("Accept", "application/json; charset=UTF-8");
await webClient.UploadDataTaskAsync(url, "POST", bytes);
}
return node;
}
var newNode = await client.CreateWithLabel(new Task(), "Task");
Exact error message is this:
The 'await' operator can only be used within an async method. Consider marking this method with the 'async' modifier and changing its return type to 'Task'
Am I doing something wrong or is it a language/compiler limitation?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…