2016-08-11 128 views
0

我已经升级到SendGrid API v3,用于我的ASP.Net项目,并发现旧的代码在IIdentityMessageService.SendAsync函数中发送电子邮件的问题。SendGrid API v3 post breaks示例

我的代码如下:

Public Class EmailService 
    Implements IIdentityMessageService 

    Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync 
     Dim sg = New SendGridAPIClient(ConfigurationManager.AppSettings("SendGridApiKey")) 

     Dim from = New Email("[email protected]", "InvestorsEdge") 
     Dim [to] = New Email(message.Destination) 
     Dim Content = New Content("text/html", message.Body) 
     Dim Mail = New Mail(from, message.Subject, [to], Content) 

     Dim response = sg.client.mail.send.post(requestBody:=Mail.Get()).GetAwaiter().GetResult() 
     If Not response.StatusCode = Net.HttpStatusCode.Accepted Then 
      AtlasDb.Log.ErrorFormat("EmailService.SendAsync", "Abnormal response code ({0}", response.StatusCode) 
     End If 

     Return Task.FromResult(response.StatusCode) 
    End Function 
End Class 

这段代码在API第2工作正常,但访问响应对象时,我现在得到一个System.MissingMemberException。

在新的例子中,这个帖子正在等待,但是我可以将SendAsync签名更改为Async,因为它不是我的代码。 。

回答

0

发现在另一个答案SO张贴here - 邮寄返回的对象现在是一个ConfiguredAwaitable对象 - 的结果可以通过.GetAwaiter()来访问调用getResult()如下:

Public Class EmailService 
    Implements IIdentityMessageService 

    Public Function SendAsync(message As IdentityMessage) As Task Implements IIdentityMessageService.SendAsync 
     Dim sg = New SendGridAPIClient(ConfigurationManager.AppSettings("SendGridApiKey")) 

     Dim from = New Email("[email protected]", "InvestorsEdge") 
     Dim [to] = New Email(message.Destination) 
     Dim Content = New Content("text/html", message.Body) 
     Dim Mail = New Mail(from, message.Subject, [to], Content) 

     Dim response = sg.client.mail.send.post(requestBody:=Mail.Get()).GetAwaiter().GetResult() 
     If Not response.StatusCode = Net.HttpStatusCode.Accepted Then 
      AtlasDb.Log.ErrorFormat("EmailService.SendAsync", "Abnormal response code ({0}", response.StatusCode) 
     End If 

     Return Task.FromResult(response.StatusCode) 
    End Function 
End Class 

I” m不知道在普通代码中这将如何执行关于阻止UI,但认为这是在ASP.Net中的一个单独的线程运行,效果不会太糟糕。