Task.IsFaulted没有在我的.Net核心中捕获异常 [英] Task.IsFaulted is not catching exception in my .Net core

查看:105
本文介绍了Task.IsFaulted没有在我的.Net核心中捕获异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Task.IsFaulted 没有捕获异常。即使条件为假,它也会发送到else块。您能建议我哪里做错了吗。我尝试实施 try ... catch ,但仍然无法正常工作。

Task.IsFaulted is not catching exception. It is sending to else block even the condition is false. Can you please suggest me where I am doing wrong. I tried implementing try... catch but still not working.

public async Task<string> SendEmailAsync(string userid, string fullname, string comment)
{
    string contentRootPath = _hostingEnvironment.ContentRootPath;

    string json = System.IO.File.ReadAllText("appsettings.json");
    JObject obj = JObject.Parse(json);

    string email = (string)obj["SendEmailTo"];
    string subject = "Feedback by: " + fullname + " (" + userid + ")";

    //  await _emailSender.SendEmailAsync(contentRootPath, email, subject, comment, userid, fullname);

    var result = _emailSender.SendEmailAsync(contentRootPath, email, subject, comment, userid, fullname);

    string responseString = "";
    if (result.IsFaulted)
    {
        try
        {
            responseString = @" 
                <!DOCTYPE html>
                <html>
                    <p>Sorry, your feedback was not completed successfully</p>
                </body>
                </html>";
        }
        catch
        {
            responseString = @" 
                <!DOCTYPE html>
                <html>
                    <p>Feedback submitted successfully.</p>
                </body>
                </html>";
        }
    }

    return await Task.FromResult<string>(responseString);
}


推荐答案

IsFaulted 不会等待任务完成或出现故障,它只是检查 Task 的当前状态。在您的情况下,您正在异步发送电子邮件,而不是等待该操作完成,然后测试可能仍在运行的 Task 的状态(并且没有出现故障)

IsFaulted doesn't wait for the task to complete or fault, it just checks the current state of the Task. In your case you're asynchronously sending an email, not waiting for that operation to complete, then testing the state of a Task that is likely still running (and not in a faulted state yet).

要解决此问题,您可以将一个等待的呼叫包装在try / catch块中...

To fix this, you can wrap an awaited call in a try/catch block...

try
{
    await _emailSender.SendEmailAsync(contentRootPath, email, subject, comment, userid, fullname);
}
catch (Exception exception)
{
    return @"<!DOCTYPE html>
<html>
    <p>Sorry, your feedback was not completed successfully</p>
</body>
</html>";
}

return @"<!DOCTYPE html>
<html>
    <p >Feedback submitted successfully.</p>
</body>
</html>";

...或者是 Task< bool> 返回方法,您可以在任务完成后测试成功/失败结果,例如

...or factor towards Task<bool> returning methods where you can test the succeeded/failed result once the task completes e.g.

public async Task<string> SendEmailAsync(string userid, string fullname, string comment)
{
    string contentRootPath = _hostingEnvironment.ContentRootPath;

    string json = System.IO.File.ReadAllText("appsettings.json");
    JObject obj = JObject.Parse(json);

    string email = (string)obj["SendEmailTo"];
    string subject = "Feedback by: " + fullname + " (" + userid + ")";

    async Task<bool> SendEmailAsync()
    {
        try
        {
            await _emailSender.SendEmailAsync(contentRootPath, email, subject, comment, userid, fullname);
        }
        catch (Exception exception)
        {
            // TODO: Log exception etc.
            return false;
        }

        return true;
    }

    var succeeded = await SendEmailAsync();

    if (succeeded)
    {
        return @"<!DOCTYPE html>
<html>
    <p>Feedback submitted successfully.</p>
</body>
</html>";
    }
    else
    {
        return @"<!DOCTYPE html>
<html>
    <p>Sorry, your feedback was not completed successfully</p>
</body>
</html>";
    }
}

这篇关于Task.IsFaulted没有在我的.Net核心中捕获异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆