フレームワークを使用してWeb呼び出しを行っていますasync
。以下のコードにエラーが記載されています
class Program
{
static void Main(string[] args)
{
TestAsync async = new TestAsync();
await async.Go();//Error: the await operator can only be used with an async method. Consider markign this method with the async modifier. Consider applying the await operator to the result of the call
}
}
class TestAsync
{
public async Task Go()
{
using (WebClient client = new WebClient())
{
var myString = await client.DownloadStringTaskAsync("http://msdn.microsoft.com");
Console.WriteLine(myString);
}
}
}
このコードのいくつかのバリエーションを試しました。実行時に失敗するか、コンパイルされません。この場合、非同期呼び出しの起動が許可される前にメソッドが完了します。私は何が間違っているのですか?
私の目標は、WebClientを非同期的に使用してWebサイトへの呼び出しを実行することです。結果を文字列として返し、を使用して出力したいと思いますConsole.WriteLine
。実行するコードから始める方が快適な場合は、変更するだけです。
await async.Go();
コードは実行されasync.Go();
ますが、Console.WriteLineはヒットしません。