I'm calling a web service from some C# code. This code looks similar to this:
using (var client = new HttpClient())
{
var page = "http://en.wikipedia.org/";
using (var response = await client.GetAsync(page))
{
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
}
}
}
I want to execute this code within a utility method. From my understanding, an Action delegate is suited for this. I attempted to do this using the following:
Action action = delegate()
{
using (var client = new HttpClient())
{
var page = "http://en.wikipedia.org/";
using (var response = await client.GetAsync(page))
{
using (var content = response.Content)
{
var result = await content.ReadAsStringAsync();
}
}
}
}
When I wrap my web service code in an Action
delegate, I receive a compile-time error that says:
The 'await' operator can only be used within an async anonymous method. Consider marking this anonymous method with the 'async' modifier.
My question is, how do I call async code in an Action? It looks like I can't. If I can't is there another way that I can pass a block of code to a utility method for execution? What does that look like?
Thank you!