alibaba-archive / ons

Aliyun ONS Q&A
22 stars 10 forks source link

ASP.NET 上正在中止线程 异常问题。 #13

Closed zhanglong19891129 closed 6 years ago

zhanglong19891129 commented 7 years ago

最近3,4天都在 整这个问题,逻辑是这样的。在我们的云平台有一个消费者。这个消费者会消费客户端发送上来的消息。然后我们将消息,放入一个队列中,然后由其他线程去处理。 现在问题是这样的。VS2017 F5调试模式下 。ASP.NET 上 如果这个消息不放入线程中,而是在 consume中直接处理。一切正常。但是在线程中处理的话,一定会出现 “正在中止线程”这么一个异常。异常位置还是随机的,即使捕获到了该异常,也会自动中断调试。 所以我一直怀疑是我们线程池的有问题 。但是当我 在Winform中运行的时候,一切正常,线程能处理数据,不会出现这个问题。所以确认 和我们的线程没有关系。现在我们 卡在这个点 已经4 天。因为某些原因,我们只能在IIS上使用。

zhanglong19891129 commented 7 years ago

hello 这个问题解决了, 实在Global中 无法使用线程的问题。这个是 在StackOverflow中找到的解释。 29 down vote accepted If you spawn threads in Application_Start, they will still be executing in the application pool's AppDomain. If an application is idle for some time (meaning that no requests are coming in), or certain other conditions are met, ASP.NET will recycle the entire appdomain. When that happens, any threads that you started from that AppDomain, including those from Application_Start, will be aborted.

Lots more on application pools and recycling in this question: What exactly is Appdomain recycling

If you are trying to run a long-running process within IIS/ASP.NET, the short answer is usually "Don't". That's what Windows Services are for.

shareimprove this answer edited May 23 at 12:17

Community♦ 11 answered Oct 3 '11 at 1:55

Chris Shain 41.9k371110

That sounds exactly like what is happening. I have another question... Is Application_Start run again when the site becomes active again? It looks like it isnt (but i may have coded something wrong) – acidzombie24 Oct 3 '11 at 11:25 2
It isn't. From here: msdn.microsoft.com/en-us/library/ms178473.aspx "The Application_Start and Application_End methods are special methods that do not represent HttpApplication events. ASP.NET calls them once for the lifetime of the application domain, not for each HttpApplication instance." – Chris Shain Oct 3 '11 at 13:22 所以 改成 Owin.Startup启动,就一切正常了