无法捕获 Azure 辅助角色中的异常
Unable to catch exception in Azure Worker Role
我正在尝试创建一个 Azure 辅助角色来下载电子邮件并将它们存储在数据库中。当我无法连接到邮件服务器或无法通过邮件服务器进行身份验证时,我会抛出一些异常,但捕获这些异常是行不通的。
我抛出的异常没有被 try catch 块捕获。这是为什么?
我的工作者角色的 RunAsync 方法:
private async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following with your own logic.
while (!cancellationToken.IsCancellationRequested)
{
Trace.TraceInformation("Working");
var emailManager = new EmailManager();
var emails = new List<Email>();
try
{
emails = emailManager.GetNewEmails("outlook.office365.com", 993, "email", "password");
}
catch(Exception ex)
{
Trace.TraceInformation("Error");
}
await Task.Delay(1000);
}
}
EmailManager.GetNewEmails()
public List<Email> GetNewEmails(string server, ushort port, string username, string password)
{
var imapClient = new ImapClient(server, port, username, password, false);
if (!imapClient.Connect())
throw new Exception("Unable to connect to server.");
if (!imapClient.Authenticate())
throw new Exception("Unable to authenticate with server.");
var messages = imapClient.GetMessages();
var emails = Mapper.Map<List<MailMessage>, List<Email>>(messages);
return emails;
}
事实证明线程实际上并没有崩溃,而是无限期挂起。这似乎是我正在使用的电子邮件库 OpaqueMail 中的一个错误,它会在您尝试建立非 SSL POP3 或 IMAP 连接时发生。我在他们的 GitHub 页面上向开发人员提出了一个错误。
当我强制建立 SSL 连接时,一切都按预期工作,并且抛出的异常会按应有的方式被捕获。
所以总结起来,一个错误导致线程挂起,这个问题与Azure、异步方法或异常处理无关。这一直是一个 OpaqueMail 错误。
我正在尝试创建一个 Azure 辅助角色来下载电子邮件并将它们存储在数据库中。当我无法连接到邮件服务器或无法通过邮件服务器进行身份验证时,我会抛出一些异常,但捕获这些异常是行不通的。
我抛出的异常没有被 try catch 块捕获。这是为什么?
我的工作者角色的 RunAsync 方法:
private async Task RunAsync(CancellationToken cancellationToken)
{
// TODO: Replace the following with your own logic.
while (!cancellationToken.IsCancellationRequested)
{
Trace.TraceInformation("Working");
var emailManager = new EmailManager();
var emails = new List<Email>();
try
{
emails = emailManager.GetNewEmails("outlook.office365.com", 993, "email", "password");
}
catch(Exception ex)
{
Trace.TraceInformation("Error");
}
await Task.Delay(1000);
}
}
EmailManager.GetNewEmails()
public List<Email> GetNewEmails(string server, ushort port, string username, string password)
{
var imapClient = new ImapClient(server, port, username, password, false);
if (!imapClient.Connect())
throw new Exception("Unable to connect to server.");
if (!imapClient.Authenticate())
throw new Exception("Unable to authenticate with server.");
var messages = imapClient.GetMessages();
var emails = Mapper.Map<List<MailMessage>, List<Email>>(messages);
return emails;
}
事实证明线程实际上并没有崩溃,而是无限期挂起。这似乎是我正在使用的电子邮件库 OpaqueMail 中的一个错误,它会在您尝试建立非 SSL POP3 或 IMAP 连接时发生。我在他们的 GitHub 页面上向开发人员提出了一个错误。
当我强制建立 SSL 连接时,一切都按预期工作,并且抛出的异常会按应有的方式被捕获。
所以总结起来,一个错误导致线程挂起,这个问题与Azure、异步方法或异常处理无关。这一直是一个 OpaqueMail 错误。