目前无法启动异步操作 [英] An asynchronous operation cannot be started at this time

查看:129
本文介绍了目前无法启动异步操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码行调用异步操作:

I am making a call to asynchronous operation with the following line of code:

Task.Run(() => { _emailService.SendEmail(email); }).Wait();


public async Task<bool> SendEmail(EmailTemplate email)
{
    try
    {
        using (Client)
        {
            await Client.SendMailAsync(email.Message);
            return true;
        }
    }
    catch (Exception ex)
    {
        Logger.Fatal(ex.ToString(), "Error occurred while sending an Email.");
        return false;
    }
}

第一次运行代码时,问题,我收到了我的电子邮件,但是,每隔运行一次,就会收到以下两个错误:

When the code ran the first time, it worked without issues and I received my email, however when I run it every other time since I get the following 2 errors:


2016-02-04 15:50:05.3039 [11]严重ExceptionHandling.Models.EmailService + d__0.MoveNext-System.ObjectDisposedException:无法访问已处置的对象。
对象名: System.Net.Mail.SmtpClient。
在System.Net.Mail.SmtpClient.SendAsync(MailMessage消息,对象userToken)
在System.Net.Mail.SmtpClient.SendMailAsync(MailMessage消息)ExceptionHandling.Models.EmailService的
。 d__0.MoveNext()

2016-02-04 15:50:05.3039 [11] FATAL ExceptionHandling.Models.EmailService+d__0.MoveNext - System.ObjectDisposedException: Cannot access a disposed object. Object name: 'System.Net.Mail.SmtpClient'. at System.Net.Mail.SmtpClient.SendAsync(MailMessage message, Object userToken) at System.Net.Mail.SmtpClient.SendMailAsync(MailMessage message) at ExceptionHandling.Models.EmailService.d__0.MoveNext()

2016-02-04 16:33:09.9768 [9]致命ExceptionHandling.Models.EmailService + d__0.MoveNext-System.Net.Mail。 SmtpException:发送邮件失败。 ---> System.InvalidOperationException:不能在此时启动异步操作。异步操作只能在异步处理程序或模块内或在页面生命周期中的某些事件期间启动。如果在执行页面时发生此异常,请确保该页面被标记为<%@ Page Async = true%>。此异常也可能表示尝试调用异步无效方法,ASP.NET请求处理通常不支持该方法。相反,异步方法应该返回一个Task,而调用者应该等待它。

2016-02-04 16:33:09.9768 [9] FATAL ExceptionHandling.Models.EmailService+d__0.MoveNext - System.Net.Mail.SmtpException: Failure sending mail. ---> System.InvalidOperationException: An asynchronous operation cannot be started at this time. Asynchronous operations may only be started within an asynchronous handler or module or during certain events in the Page lifecycle. If this exception occurred while executing a Page, ensure that the Page is marked <%@ Page Async="true" %>. This exception may also indicate an attempt to call an "async void" method, which is generally unsupported within ASP.NET request processing. Instead, the asynchronous method should return a Task, and the caller should await it.

我试图理解为什么我得到第一次尝试后是否出现上述错误?

I'm trying to understand why do I get the above errors after the first attempt?

推荐答案

您的错误很可能(至少部分地)是由于您处理了 Client

Your error is probably (at least partly) due to you disposing your Client.

范围的中放置实例化的对象 使用语句是对关键字的非常不常规的使用,很容易弄错。

Disposing an object instantiated outside of the scope of the using statement is a pretty unconventional use of the keyword, and it's easy to get it wrong.

一个简单的解决方法是创建一个新的每次客户端实例:

A simple fix would be to create a new client instance every time:

using (MailClient client = CreateMailClient())
{
    await client.SendMailAsync(email.Message);
    return true;
}

private SmtpClient CreateMailClient()
{
    MailClient client = new MailClient();

    // Configure client.

    return client;
}

这篇关于目前无法启动异步操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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