Task.IsFaulted is not working in my .Net core application

后端 未结 1 1109
轮回少年
轮回少年 2021-01-28 02:46

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 plea

相关标签:
1条回答
  • 2021-01-28 03:24

    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>
                                    ";
    
    }
    

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

    0 讨论(0)
提交回复
热议问题