Task.IsFaulted
ist keine Ausnahme. Es wird an einen else-Block gesendet, auch wenn die Bedingung falsch ist. Können Sie mir bitte vorschlagen, wo ich falsch mache. Ich habe versucht, es zu implementieren, try... catch
aber es funktioniert immer noch nicht.
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
wartet nicht, bis die Aufgabe abgeschlossen ist oder ein Fehler aufgetreten ist, sondern überprüft nur den aktuellen Status der Task
. In Ihrem Fall senden Sie asynchron eine E-Mail, warten nicht auf den Abschluss dieses Vorgangs und testen dann den Status einer Task
wahrscheinlich noch laufenden (und noch nicht fehlerhaften) E-Mail .
Um dies zu beheben, können Sie einen erwarteten Anruf in einen try/catch-Block einschließen...
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>";
... oder berücksichtigen Sie die Task<bool>
Rückgabe von Methoden, bei denen Sie das erfolgreiche/fehlgeschlagene Ergebnis testen können, sobald die Aufgabe abgeschlossen ist, z
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>";
}
}
Dieser Artikel stammt aus dem Internet. Bitte geben Sie beim Nachdruck die Quelle an.
Bei Verstößen wenden Sie sich bitte [email protected] Löschen.
Lass mich ein paar Worte sagen