An asynchronous operation cannot be started at this time

蹲街弑〆低调 提交于 2019-12-12 16:16:32

问题


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] 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] 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?


回答1:


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


来源:https://stackoverflow.com/questions/35212140/an-asynchronous-operation-cannot-be-started-at-this-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!