使用C#async在Azure上使用SendGrid发送电子邮件,等待

tal*_*kol 8 c# azure sendgrid async-await

我正在按照本教程使用SendGrid从我的Azure Web角色发送电子邮件.

这是教程中的相关部分:

// Create credentials, specifying your user name and password.
var credentials = new NetworkCredential("username", "password");

// Create an REST transport for sending email.
var transportREST = REST.GetInstance(credentials);

// Send the email.
transportREST.Deliver(myMessage);
Run Code Online (Sandbox Code Playgroud)

在我看来,该transportREST.Deliver功能是同步的.由于它正在执行IO,我更喜欢使用新的.NET 4.5 异步,而是等待(我的所有其他代码都是完全异步的).这可能吗?是否有SendGrid的异步API或某种方式来包装现有的调用?

bwe*_*est 3

目前,SendGrid C# 库没有异步方法,因为我们希望保持与 .NET 4.0 的兼容性。

您可以轻松地自己进行此更改。事实上,这是我后来在我们决定坚持使用 .NET 4.0 后恢复的一个提交。

更新:截至 2013 年 10 月 24 日和 sendgrid-csharp 1.2.1,有一个 DeliverAsync 方法可用

  • 关于您链接到的提交的一些评论: 1.您的“异步”方法返回 void(它们应该返回“任务”),因此它们不会那么有用(您不能“等待”它们)。2. 您可以通过让这些方法返回“Task”并在方法中使用“.ContinueWith”而不是“await”来保持与.NET 4.0的兼容性(在提交中,看起来并不很难使用“.ContinueWith”与“await”。 (3认同)