本文介绍了Task.IsFaulted 在我的 .Net 核心应用程序中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Task.IsFaulted 未捕获异常.IsFault 似乎不起作用.即使条件为假,它也会发送到 else 块.你能告诉我我哪里做错了吗.

Task.IsFaulted is not capturing exception. It seems that IsFault is not working. It is sending to else block even the condition is false. Can you please suggest me where I am doing wrong.

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)
            {
                responseString = @"
                                <!DOCTYPE html>
                                <html>
                                <head>
                                    <meta charset='utf - 8' />
                                    <title>MCP Feedback Facility</title>
                                </head >
                                <body >
                                    <br />
                                 <br />
                                 <br />
                                    <p>Sorry, your feedback submission was not completed successfully</p>
                                </body>
                                </html>
                                ";
            }
          else
            {
                responseString = @"
                                    <!DOCTYPE html>
                                    <html>
                                    <head>
                                        <meta charset='utf - 8' />
                                        <title>MCP Feedback Facility</title>
                                    </head >
                                    <body >
                                        <br />
                                        <br />
                                        <br />
                                        <p>Feedback submitted successfully.</p>
                                      </body>
                                    </html>
                                    ";

            }

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

推荐答案

不要使用IsFaulted,只需使用try/catch:

Instead of using IsFaulted, just use try/catch:

try
{
  await _emailSender.SendEmailAsync(contentRootPath, email, subject, comment, userid, fullname);
  return @"
                                    <!DOCTYPE html>
                                    <html>
                                    <head>
                                        <meta charset='utf - 8' />
                                        <title>MCP Feedback Facility</title>
                                    </head >
                                    <body >
                                        <br />
                                        <br />
                                        <br />
                                        <p>Feedback submitted successfully.</p>
                                      </body>
                                    </html>
                                    ";
}
catch
{
  return @"
                                <!DOCTYPE html>
                                <html>
                                <head>
                                    <meta charset='utf - 8' />
                                    <title>MCP Feedback Facility</title>
                                </head >
                                <body >
                                    <br />
                                 <br />
                                 <br />
                                    <p>Sorry, your feedback submission was not completed successfully</p>
                                </body>
                                </html>
                                ";

}

旁注:return await Task.FromResult(responseString); 只是 return responseString;

Side note: return await Task.FromResult<string>(responseString); is just a less efficient and less maintainable version of return responseString;

这篇关于Task.IsFaulted 在我的 .Net 核心应用程序中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 02:42