等待来自 SendGrid 的 API return 代码时应用程序冻结
Application freezes when waiting for API return code from SendGrid
我的应用程序基本上是一个电子邮件客户端。它通常工作正常——没有问题或错误。当它向收件人发送电子邮件时,他们会成功收到邮件。但是,当该异步任务完成时,整个应用程序都冻结了。
我使用的修补程序是从 API 响应中删除 await
关键字。这行得通,但是使用这种方法你无法获得确定消息是否成功发送的状态代码,因为你没有等待响应。
代码如下:
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace SendMailClass{
internal class FunctionCall{
#region Getting user info
static bool EmailHasBeenSend = false;
static string Documents = Environment.GetFolderPath(Environment.SpecialFolder
.MyDocuments) + "\Documents.txt";
static string UserName = File.ReadLines(Documents).Skip(0).Take(1).First();
static string UserEmail = File.ReadLines(Documents).Skip(1).Take(1).First();
#endregion
public static void SendFunction(string Recipient, string SubjectVariable,
string CC, string BCC, String Messange,string FileName,
string AttachmentPath)
{
Execute(Recipient, SubjectVariable ,CC,BCC, Messange ,FileName,
AttachmentPath).Wait();
}
static async Task Execute(string Recipient, string SubjectVariable,
string CC, string BCC, string Messange,string FileName,
string AttachmentPath)
{
#region Email Values
var apiKey = Environment.GetEnvironmentVariable("InMail_Api_Key");
var client = new SendGridClient(apiKey);
var from = new EmailAddress(UserEmail, UserName);
var subject = SubjectVariable == "" ? "(No subject)" : SubjectVariable;
var to = new EmailAddress(Recipient);
var plainTextContent = Messange == "" ? " " : Messange; ;
var htmlContent = Messange;
#endregion
var Message = MailHelper.CreateSingleEmail(from, to, subject,
plainTextContent, htmlContent);
#region Attachment
if (AttachmentPath != "") {
var bytes = System.IO.File.ReadAllBytes(AttachmentPath);
var File = Convert.ToBase64String(bytes);
Message.AddAttachment(FileName, File);
}
#endregion
/*
#region CC/BCC
if (CC != "") {
Message.AddCcs("anemail@example.com");
}
if (BCC != ""){
Message.AddBccs("anemail@example.com")}
#endregion
*/
var response =await client.SendEmailAsync(Message);
//var response =client.SendEmailAsync(Message); ---> the hotfix
}
}
}
调试后我得到的唯一结果来自调试器:
The thread --- has exited with code 0 (0x0).
在断点处:
The APIResponse is equal to NULL
它导致应用程序冻结的原因没有任何意义。
我提到我的最后一个修补程序是:
APIResponse =client.SendEmailAsync(Message);
我发现这解决了问题:
APIResponse = await client.SendEmailAsync(Message).ConfigureAwait(false);
在确定这是最佳解决方案之前,我想多测试一下。
到目前为止,响应是 Accepted(如果电子邮件格式正确)和 null 如果连接不活动(没有互联网)。
我的应用程序基本上是一个电子邮件客户端。它通常工作正常——没有问题或错误。当它向收件人发送电子邮件时,他们会成功收到邮件。但是,当该异步任务完成时,整个应用程序都冻结了。
我使用的修补程序是从 API 响应中删除 await
关键字。这行得通,但是使用这种方法你无法获得确定消息是否成功发送的状态代码,因为你没有等待响应。
代码如下:
using System;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using SendGrid;
using SendGrid.Helpers.Mail;
namespace SendMailClass{
internal class FunctionCall{
#region Getting user info
static bool EmailHasBeenSend = false;
static string Documents = Environment.GetFolderPath(Environment.SpecialFolder
.MyDocuments) + "\Documents.txt";
static string UserName = File.ReadLines(Documents).Skip(0).Take(1).First();
static string UserEmail = File.ReadLines(Documents).Skip(1).Take(1).First();
#endregion
public static void SendFunction(string Recipient, string SubjectVariable,
string CC, string BCC, String Messange,string FileName,
string AttachmentPath)
{
Execute(Recipient, SubjectVariable ,CC,BCC, Messange ,FileName,
AttachmentPath).Wait();
}
static async Task Execute(string Recipient, string SubjectVariable,
string CC, string BCC, string Messange,string FileName,
string AttachmentPath)
{
#region Email Values
var apiKey = Environment.GetEnvironmentVariable("InMail_Api_Key");
var client = new SendGridClient(apiKey);
var from = new EmailAddress(UserEmail, UserName);
var subject = SubjectVariable == "" ? "(No subject)" : SubjectVariable;
var to = new EmailAddress(Recipient);
var plainTextContent = Messange == "" ? " " : Messange; ;
var htmlContent = Messange;
#endregion
var Message = MailHelper.CreateSingleEmail(from, to, subject,
plainTextContent, htmlContent);
#region Attachment
if (AttachmentPath != "") {
var bytes = System.IO.File.ReadAllBytes(AttachmentPath);
var File = Convert.ToBase64String(bytes);
Message.AddAttachment(FileName, File);
}
#endregion
/*
#region CC/BCC
if (CC != "") {
Message.AddCcs("anemail@example.com");
}
if (BCC != ""){
Message.AddBccs("anemail@example.com")}
#endregion
*/
var response =await client.SendEmailAsync(Message);
//var response =client.SendEmailAsync(Message); ---> the hotfix
}
}
}
调试后我得到的唯一结果来自调试器:
The thread --- has exited with code 0 (0x0).
在断点处:
The APIResponse is equal to NULL
它导致应用程序冻结的原因没有任何意义。
我提到我的最后一个修补程序是:
APIResponse =client.SendEmailAsync(Message);
我发现这解决了问题:
APIResponse = await client.SendEmailAsync(Message).ConfigureAwait(false);
在确定这是最佳解决方案之前,我想多测试一下。 到目前为止,响应是 Accepted(如果电子邮件格式正确)和 null 如果连接不活动(没有互联网)。