“应用程序处于中断模式” C# 使用 throw 抛出异常时

“The application is in break mode" C# when using throw to bubble out the exception

当我抛出异常时,我的函数将其冒出并在任务异常中捕获它,而不是继续它会破坏代码-下面是我的代码

public override void Run()
{
    SendRenewalsEmail("ddd@xxx.com", " Email Body from test More", "Test Email from Service another Test");
}

private async void SendRenewalsEmail(string userEmail, string emailBody, string emailSubject)
{
    string replyFromEmailAddress = "renewals@xxx.net";
    string cc = "";
    string bcc = "ccc@xxxx.com";
    SMTPMailHelperAsync sMTPMailHelperAsync = new SMTPMailHelperAsync();
    var x= await sMTPMailHelperAsync.SendEmailAsync(userEmail, cc, bcc, emailSubject, SMTPMailHelperAsync.ProcessTemplate(emailBody, "Renewals.html", emailSubject), replyFromEmailAddress);

    if (x.MailSent)
    {
        throw new Exception("after mail Test more service");
    }
}

以及捕获它的任务

var task=  Task<PluginInstance>.Run<PluginInstance>(() => {
    thisPlugin.LastRunStart = DateTime.Now.ToLocalTime();
    try
    {
        thisPlugin.Plugin.Run();
        thisPlugin.LastRunStatus = Enums.RunStatus.Success;
        thisPlugin.LastRunMessage = "";
    }
    catch (Exception ex)
    {
        thisPlugin.LastRunStatus = Enums.RunStatus.Failed;
        thisPlugin.LastRunMessage = ex.Message;
    }
    thisPlugin.LastRunEnd = DateTime.Now.ToLocalTime();
    return thisPlugin;
});

ListOfTask.Add(task);

现在我正在尝试捕获任务异常中的异常但不是。低于异常

您不得使用 async void。这是一种特殊情况,仅为事件处理程序保留。您的 async 方法必须 return a Task:

private async Task SendRenewalsEmail(…)

那么,你的Plugin.Run方法就坏了。也应该是async

一旦你从 async - await 开始,你就会做到最上面。