如果我从来没有叫HostingEnvironment.UnregisterObject? [英] Should I never call HostingEnvironment.UnregisterObject?

查看:115
本文介绍了如果我从来没有叫HostingEnvironment.UnregisterObject?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图实现通过SMTP电子邮件asyncronous在我的ASP.Net MVC 3应用程序,我过来<一个href=\"http://stackoverflow.com/questions/6935427/smtpclient-sendasync-blocking-my-asp-net-mvc-request\">SO SmtpClient.SendAsync挡住了我的ASP.NET MVC申请线程。在那里,我发现通过菲尔哈克的文章:<一href=\"http://haacked.com/archive/2011/10/16/the-dangers-of-implementing-recurring-background-tasks-in-asp-net.aspx\"相对=nofollow>实施定期后台任务在ASP.NET 它提供了一种方法,以避免停机的AppDomain后台线程的崩溃的危险。

While trying to implement asyncronous email over smtp in my ASP.Net MVC 3 application I've come around SO SmtpClient.SendAsync blocking my ASP.NET MVC Request thread. There I found the article by Phil Haack: The Dangers of Implementing Recurring Background Tasks In ASP.NET which provides a way to avoid crashing of the background thread on AppDomain shutdown.

文章说叫 HostingEnvironment.RegisterObject(本); 致电 HostingEnvironment.UnregisterObject(这)对构造和; 仅在 IRegisteredObject.Stop方法被调用。

The article says to call HostingEnvironment.RegisterObject(this); on the constructor and call HostingEnvironment.UnregisterObject(this); only if IRegisteredObject.Stop Method is called.

在一般情况下,当请求到达永久和 IRegisteredObject 实施对象的范围是请求,不到风度每个请求中这种方法寄存器对象(利用电子邮件功能),不注销的?

In a general scenario, when requests arrive permanently, and the scope of the object implementing IRegisteredObject is request, dosn't this approach register objects within each request (utilizing the email functionality) and does not unregister any?

可以吗?还是应该异步操作完成后,我也注销?

Is it OK? Or should I also unregister after the asynchronous operation completed?

P.S:由达米安·爱德华兹在链接的SO问题的建议,我用 ThreadPool.QueueUserWorkItem 发送电子邮件超出请求范围。

P.S.: as suggested by Damian Edwards in the linked SO question, I use ThreadPool.QueueUserWorkItem to send the email beyond request scope.

推荐答案

我不知道你所说的请求到达永久的范围是什么意思对象......是要求等。

I'm not sure what you mean by requests arrive permanently, scope of the object ... is request etc.

请求范围永久 ThreadPool.QueueUserWorkItem ;这些话一起是没有意义的。一种使用 ThreadPool.QueueUserWorkItem ,这样的要求并不需要永远。而您的要求达米安·爱德华兹建议立即返回费时的工作是在后台完成。

Request Scope, permanent, and ThreadPool.QueueUserWorkItem; these words together do not make sense at all. One uses ThreadPool.QueueUserWorkItem so that a request does not take forever. The time-consuming job is done in the background while your request returns immediately as Damian Edwards suggests.

我用 IRegisterObject 在一个收到请求发送大量电子邮件。然而,在我的情况,我使用了一个单独的对象 EmailSender 实现 IRegisterObject 。在这种情况下,它是在构造函数和未注册登记后曾在停止()

I have used IRegisterObject to send bulk e-mails upon a request received. However, in my case, I have used a singleton object EmailSender that implements IRegisterObject. In that case, it is registered once in the constructor and unregistered once in Stop().

因此​​,简而言之,请使用

So, in short, please use a singleton.

这篇关于如果我从来没有叫HostingEnvironment.UnregisterObject?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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