1

I am using SendGrid (cloud based SMTP service) to send emails from a web api project. I want my application to wait/block (for say 30 secs) until I have a response from SendGrid before returning the response to the client, rather than returning immediately. The SendGrid library has a DeliverAsync method which returns a Task.

I have been looking at how I might Wait on the task.

I have read endless articles about how one might do this and understand that if it was my own code I would use the ConfigureAwait(false) on the task to prevent a deadlock and allow me to Wait. The problem here is that the code is not mine! It doesn't look like SendGrid have a synchronous Send method.

I do not have async controllers wired up, although appreciate this would be a way to do this, but I'd like to know if there is another way I could do this.

Hope this makes sense!!

user644698
  • 209
  • 2
  • 12
  • 1
    If you using third party library in Web APi - then you can use `await` and controller will return only after task completes. – Fabio Dec 05 '16 at 20:23
  • If using the await keyword in your code does not make sense you can add ".GetAwaiter().GetResult();" to the end of the async SendGrid call and it will wait for the async function to complete. – Kyle Dec 05 '16 at 20:24
  • In ASP:NET Web API asynchronous method used for more effective threads using. In WPF or Winforms asynchronous methods will free UI thread for other operation while Task completes which makes UI "responsive" by "non blocking" UI thread. – Fabio Dec 05 '16 at 20:25
  • @Kyle That will just deadlock the program. – Servy Dec 05 '16 at 20:31

1 Answers1

0

If you can await all the way up to and including the controller action, you should, and as that's your code it should be achievable. In that case, at most you might want to consider ConfigureAwait(true) for the call from the controller method only, and have the rest (downwards) as ConfigureAwait(false) (as library methods should be). Most of the time you don't even need the context preserved in the controller action - it depends what you do there - and in that case use ConfigureAwait(false) there too.

You use "wait/block" as though they're the same, but in the TAP world they're quite different. Using await will wait for the SendGrid() call to complete before continuing, while not blocking the calling thread.

If you can't do that, it's far less-preferable to use the blocking .Wait() or .Result, or as others mention GetAwaiter().GetResult(). All 3 will block the caller as well. In Web API you can often get away with this; but in other contexts - e.g. WinForms - you probably won't.

As it's your code, use await.

sellotape
  • 8,034
  • 2
  • 26
  • 30
  • Thanks for the reply. Await all the way up is not an option for me at present, although I understand it is the best solution. I think all the other suggestions will block because the 3rd party library does not call ConfigureAwait(false) with its await calls. So, I think this is an issue with the library, so will contact them for more info... – user644698 Dec 06 '16 at 10:25
  • If you cannot await, just do .Result but this will be a blocking call. – alltej Dec 06 '16 at 17:02