-4

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!

John F
  • 3
  • 1
  • 3
  • 4
    `Action action = async delegate() ...` – DavidG Apr 30 '18 at 17:55
  • `Func action = async () => {/*method body*/}` would also work. That way a Task is being returned that can be awaited by the caller of your method. As `Action` has the return type `void` I do not believe it can be awaited further up the call stack. – Igor Apr 30 '18 at 17:58
  • 1
    Use a static `HttpClient` instance; don't create a new one all the time. – xxbbcc Apr 30 '18 at 18:00

1 Answers1

1

You need to mark your action as async just like you would any method, for example:

Action action = async delegate() 
    //snip

However, this is equivalent to an async void method which is not recommended. Instead you many consider using Func<Task> instead of action:

Func<Task> doStuff = async 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();
      }
    }
  }
}

Now you can await the result:

await doStuff();
DavidG
  • 113,891
  • 12
  • 217
  • 223