等待code调用SmtpClient

等待code调用SmtpClient

本文介绍了困惑于如何构建异步/等待code调用SmtpClient.SendMailAsync()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读的说明文件异步等待的功能,但仍对如何构建高糊涂异步等待 code调用 SmtpClient.SendMailAsync()方法。
我应该如何改写code正确使用异步等待

目前,我的code看起来是这样的:
定期 PrivateSignal 被调用的系统发送电子邮件通知。

 公共覆盖无效PrivateSignal(IEventInformation EV)
{
    TagEvent标签=(TagEvent)EV;
    Task.Run(异步()=>
    {
        尝试
        {
           等待smptClient.SendMailAsync(CaptureRC.SmptFromEmailAddr,
                                    ToEmails,
                                    CaptureRC.EmailSubject,
                                    见过移动+ tag.ToString());
        }
        赶上(AggregateException前)
        {
           // TODO处理错误
           // TODO记录误差修改,随着
        }
    })。等待();
}
 

解决方案

您应该使 PrivateSignal 异步作为以及包括与将其标记为异步并返回工作

 公众覆盖异步任务PrivateSignalAsync(IEventInformation EV)
{
    TagEvent标签=(TagEvent)EV;
    尝试
    {
       等待smptClient.SendMailAsync(CaptureRC.SmptFromEmailAddr,ToEmails,CaptureRC.EmailSubject,看到移动+ tag.ToString());
    }
    赶上(例外前)
    {
        // ...
    }
}
 

看来,虽然,你不能这样做,因为 PrivateSignal 是压倒一切已经同步的方法。如果可以更改基本方法返回一个工作那么你应该的,但如果你不能那么干脆不使用异步在里面所有的方法,因为阻塞异步可导致死锁和其他不想要的结果:

 公共覆盖无效PrivateSignal(IEventInformation EV)
{
    TagEvent标签=(TagEvent)EV;
    尝试
    {
       smptClient.SendMail(CaptureRC.SmptFromEmailAddr,ToEmails,CaptureRC.EmailSubject,看到移动+ tag.ToString());
    }
    赶上(例外前)
    {
        // ...
    }
}
 

I read the documentation for async-await feature, but still highly confused on how to structure async-await code that calls SmtpClient.SendMailAsync() method.
How should I rewrite the code to properly use async-await ?

Currently, my code looks like this:
Periodically PrivateSignal is being called by the system to send out email notifications.

public override void PrivateSignal(IEventInformation ev)
{
    TagEvent tag = (TagEvent)ev;
    Task.Run(async () =>
    {
        try
        {
           await smptClient.SendMailAsync(CaptureRC.SmptFromEmailAddr,
                                    ToEmails,
                                    CaptureRC.EmailSubject,
                                    "seen moving" + tag.ToString());
        }
        catch (AggregateException ex)
        {
           //TODO handle the error
           //TODO log the erros, along with
        }
    }).Wait();
}
解决方案

You should be making PrivateSignal async as well which includes marking it with async and returning a Task:

public override async Task PrivateSignalAsync(IEventInformation ev)
{
    TagEvent tag = (TagEvent)ev;
    try
    {
       await smptClient.SendMailAsync(CaptureRC.SmptFromEmailAddr, ToEmails, CaptureRC.EmailSubject, "seen moving" + tag.ToString());
    }
    catch (Exception ex)
    {
        // ...
    }
}

It seems, though, that you can't do that since PrivateSignal is overriding an already synchronous method. If you can change the base method to return a Task then you should, but if you can't then simply don't use async at all inside that method because blocking on async can lead to deadlocks and other unwanted results:

public override void PrivateSignal(IEventInformation ev)
{
    TagEvent tag = (TagEvent)ev;
    try
    {
       smptClient.SendMail(CaptureRC.SmptFromEmailAddr, ToEmails, CaptureRC.EmailSubject, "seen moving" + tag.ToString());
    }
    catch (Exception ex)
    {
        // ...
    }
}

这篇关于困惑于如何构建异步/等待code调用SmtpClient.SendMailAsync()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 19:21