0

I'm calling an async method from my Web API action method that looks like this but I'm getting "Cannot implicitly convert type Task Employee to Employee" error.

What do I need to do?

My Web API action method looks like this:

public IHttpActionResult GetEmployee()
{

   // Get employee info
   Employee emp = myDataMethod.GetSomeEmployee();

   return Ok(emp);
}

And the method I'm calling looks like this:

public static async Task<Employee> GetSomeEmployee()
{
   Employee employee = new Employee();

   // Some logic here to retrieve employee info

   return employee;
}

What do I need to do so that I can call this method to retrieve employee information?

P.S. The GetSomeEmployee() method has to be async because it makes other async calls to retrieve employee data.

1 Answer 1

5

You need to either call the method synchronously, or use await. E.g.:

Synchronously (GetEmployee() will block until GetSomeEmployee() completes):

public IHttpActionResult GetEmployee()
{

   // Get employee info
   Employee emp = myDataMethod.GetSomeEmployee().Result;

   return Ok(emp);
}

Asynchronously (GetEmployee() will return immediately, and then be continued when GetSomeEmployee() completes):

public async Task<IHttpActionResult> GetEmployee()
{

   // Get employee info
   Employee emp = await myDataMethod.GetSomeEmployee();

   return Ok(emp);
}
Sign up to request clarification or add additional context in comments.

7 Comments

Considering this is a Web API method my web/mobile apps will call, does it make more sense for me to call it synchronously so that I return some data? Does calling it asynchronously mean my Web API will return OK without any data?
What do you mean by "return OK"? The caller of the async method will have to do something similar to what you're needing to do here: i.e. either wait on the result, or itself become an async method. You'll have to make that same decision up the call chain, until you get to a point where it makes sense to wait, or you're dealing with an UI event handler or similar where the method doesn't actually return a value (and so the caller doesn't need to care whether the task has completed or not).
If I call the GetSomeEmployee() method asychronously, will the GetEmployee() action method wait till I get the results? I'm getting into asynchronous programming now and I'm not clear about the behavior. I clearly want the GetEmployee() action method to wait for results before returning any response.
"Clearly"? Since you didn't show the caller of GetEmployee() that's hardly clear to everyone reading this question (maybe in the context of the Web API it is, but I'm much more familiar with async methods than Web API :) ). It might be clear to you. If you want the GetEmployee() method to work synchronously -- that is, to not return at all until the GetSomeEmployee() method has completed -- then use the first example I gave.
Only the second option in this answer is correct. You should use await, and most certainly not use Result.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.