SmtpClient.SendAsync 阻塞了我的 ASP.NET MVC 请求 [英] SmtpClient.SendAsync blocking my ASP.NET MVC Request

查看:33
本文介绍了SmtpClient.SendAsync 阻塞了我的 ASP.NET MVC 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送简单电子邮件的操作:

I have a Action that sends a simple email:

    [HttpPost, ActionName("Index")]
    public ActionResult IndexPost(ContactForm contactForm)
    {
        if (ModelState.IsValid)
        {
            new EmailService().SendAsync(contactForm.Email, contactForm.Name, contactForm.Subject, contactForm.Body, true);

            return RedirectToAction(MVC.Contact.Success());
        }
        return View(contactForm);
    }

还有电子邮件服务:

    public void SendAsync(string fromEmail, string fromName, string subject, string body, bool isBodyHtml)
    {
        MailMessage mailMessage....
        ....
        SmtpClient client = new SmtpClient(settingRepository.SmtpAddress, settingRepository.SmtpPort);

        client.EnableSsl = settingRepository.SmtpSsl;
        client.Credentials = new NetworkCredential(settingRepository.SmtpUserName, settingRepository.SmtpPassword);
        client.SendCompleted += client_SendCompleted;
        client.SendAsync(mailMessage, Tuple.Create(client, mailMessage));
    }

    private void client_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
    {
        Tuple<SmtpClient, MailMessage> data = (Tuple<SmtpClient, MailMessage>)e.UserState;
        data.Item1.Dispose();
        data.Item2.Dispose();

        if (e.Error != null)
        {

        }
    }

当我发送电子邮件时,我使用的是 Async 方法,然后我的方法 SendAsync 立即返回,然后调用 RedirectToAction.但是在 client_SendCompleted 完成之前,ASP.NET 不会发送响应(在本例中为重定向).

When I send a email, I am using Async method, then my method SendAsync return immediately, then RedirectToAction is called. But the response(in this case a redirect) isn´t sent by ASP.NET until client_SendCompleted is completed.

这是我想要理解的:

在 Visual Studio 调试器中观察执行时,SendAsync 立即返回(并调用 RedirectToAction),但在发送电子邮件之前浏览器中没有任何反应?

When watching the execution in Visual Studio debugger, the SendAsync returns immediately (and RedirectToAction is called), but nothing happens in the browser until email is sent?

如果我在 client_SendCompleted 中放置一个断点,客户端将保持加载状态......直到我在调试器中按 F5.

If i put a breakpoint inside client_SendCompleted, the client stay at loading.... until I hit F5 at debugger.

推荐答案

这是设计使然.如果异步工作以调用底层 SynchronizationContext 的方式开始,ASP.NET 将自动等待任何未完成的异步工作完成,然后再完成请求.这是为了确保如果您的异步操作尝试与 HttpContextHttpResponse 等交互,它仍然会存在.

This is by design. ASP.NET will automatically wait for any outstanding async work to finish before finishing the request if the async work was kicked off in a way that calls into the underlying SynchronizationContext. This is to ensure that if your async operation tries to interact with the HttpContext, HttpResponse, etc. it will still be around.

如果你想做真火&忘记了,您需要将您的调用包装在 ThreadPool.QueueUserWorkItem 中.这将强制它在一个新的线程池线程上运行而不通过 SynchronizationContext,因此请求将很高兴地返回.

If you want to do true fire & forget, you need to wrap your call in ThreadPool.QueueUserWorkItem. This will force it to run on a new thread pool thread without going through the SynchronizationContext, so the request will then happily return.

但是请注意,如果由于任何原因应用域在您的发送过程中关闭(例如,如果您更改了 web.config 文件、将新文件放入 bin、应用程序池回收等).) 你的异步发送会突然中断.如果你关心这个,看看 Phil Haacks WebBackgrounder 用于 ASP.NET,让您以确保它的方式排队和运行后台工作(如发送电子邮件)在应用域关闭的情况下优雅地完成.

Note however, that if for any reason the app domain were to go down while your send was still in progress (e.g. if you changed the web.config file, dropped a new file into bin, the app pool recycled, etc.) your async send would be abruptly interrupted. If you care about that, take a look at Phil Haacks WebBackgrounder for ASP.NET, which let's you queue and run background work (like sending an email) in such a way that will ensure it gracefully finishes in the case the app domain shuts down.

这篇关于SmtpClient.SendAsync 阻塞了我的 ASP.NET MVC 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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