I have a application where I use Async and Await when calling a rest web service. When running my unit tests, I can't seem to get any proper response back even though I am using await. This is the async method:
public async Task<Response> SendEmail(string apiKey, string senderEmail, string senderName, string recipientEmail, string recipientName, string subject, string content, bool html)
{
var client = new SendGridClient(apiKey);
var from = new EmailAddress(senderEmail, senderName);
var to = new EmailAddress(recipientEmail, recipientName);
var msg = MailHelper.CreateSingleEmail(from, to, subject, html ? null : content, html ? content : null);
var response = await client.SendEmailAsync(msg);
return response;
}
The calling method looks like this:
public static object SendEmail(string apiKey, string senderEmail, string senderName, string recipientEmail, string recipientName, string subject, string content, bool html)
{
EmailHandler emailHandler = new EmailHandler();
var response = emailHandler.SendEmail(apiKey, senderEmail, senderName, recipientEmail, recipientName, subject, content, html);
return response;
}
Now if I put a breakpoint on return response in the calling function, I can see an object that has the status="Waiting for Activation" and Result="Not yet computed". Of what I have been able to gather, calling .Result on the returned object should make it run synchronously and return the result. (For example, status code of the request such as 200).
What am I missing here? Why does it not wait until it is finished?