简体   繁体   English

AppDomains与强大的服务器

[英]AppDomains vs. a robust server

after doing some research it seems that AppDomains are not really a tool for building a hosting server. 经过一些研究后,AppDomains似乎并不是构建托管服务器的真正工具。 From my understanding, the hosting server will still crash if there is an unhandled exception in a created AppDomain (if the exception is thrown from a thread in the created AppDomain). 根据我的理解,如果在创建的AppDomain中存在未处理的异常(如果从创建的AppDomain中的线程抛出异常),托管服务器仍将崩溃。 So in that case if the hosting server hosts a service which leaks exceptions this will bring down the default AppDomain as well. 因此,在这种情况下,如果托管服务器托管泄漏异常的服务,这也将导致默认的AppDomain失效。

So I guess from a server architecture point-of-view there is nothing better than creating child processes and monitoring them. 所以我认为从服务器架构的角度来看,没有比创建子进程和监视它们更好的了。

Is that correct or am I missing something with AppDomains? 这是正确的还是我错过了AppDomains的东西?

thanks, Christoph 谢谢,克里斯托夫

If you can control the threads created in the other AppDomain, you can also handle exceptions by using catch-all blocks in the thread main method. 如果可以控制在其他AppDomain中创建的线程,则还可以通过在线程main方法中使用catch-all块来处理异常。

Other than that, as long as you use the default host, I believe that your assumption is correct. 除此之外,只要您使用默认主机,我相信您的假设是正确的。 However, if you host the runtime yourself, you can also handle unhandled exceptions. 但是,如果您自己托管运行时,则还可以处理未处理的异常。

From a forum post on the topic : 来自关于该主题论坛帖子

Well, it is possible. 嗯,这是可能的。 You'd have to create your own CLR host. 您必须创建自己的CLR主机。 That starts with ICorBindToRuntimeEx(). 这从ICorBindToRuntimeEx()开始。 You get to have full control of AppDomains that throw exceptions. 您可以完全控制引发异常的AppDomains。 And it's being used by MSFT software like ASP.NET and SQL Server 2005. When you write a service, you are working with the default CLR host implementation and it terminates the process when any unhandled exception is raised, regardless of what AppDomain caused the exception. 它正在被ASP.NET和SQL Server 2005等MSFT软件使用。当您编写服务时,您正在使用默认的CLR主机实现,并在引发任何未处理的异常时终止该过程,无论AppDomain导致异常是什么。

Problem is, hosts like ASP.NET and SQL server have a very well defined code execution path. 问题是,像ASP.NET和SQL服务器这样的主机有一个定义良好的代码执行路径。 In a web server, managed code runs because of a page request. 在Web服务器中,托管代码由于页面请求而运行。 In a dbase server, it runs because of a query. 在dbase服务器中,它由于查询而运行。 When something bad happens, they have the luxury of simply aborting everything that the request started (killing the AppDomain) and returning a "sorry, couldn't do it" status back to the client. 当发生不好的事情时,他们可以简单地中止请求开始的所有事情(杀死AppDomain)并将“抱歉,无法做到”状态返回给客户端。 You might have seen it, crashing the forums server on the old web site was pretty trivial but didn't stop it from serving other requests. 您可能已经看过它,在旧网站上崩溃论坛服务器非常简单,但并没有阻止它提供其他请求。 Not actually 100% sure about that. 实际上并非100%确定。

Your service implementation is probably not nearly as clean. 您的服务实现可能不是那么干净。 I can't tell, you didn't say anything about it. 我不知道,你没有说什么。 It general, there's a problem with aborting a thread. 一般来说,中止线程有问题。 You always have to abort a thread when there's an unhandled exception. 当出现未处理的异常时,您总是必须中止一个线程。 A service typically has one thread, started by the OnStart() method. 服务通常有一个线程,由OnStart()方法启动。 Aborting it kills the server until somebody stops and starts it again. 中止它会杀死服务器,直到有人停止并再次启动它。

You can definitely make it more resilient than that, you could start a "master" thread that launches child threads in response to external events that makes your service do its job. 你绝对可以使它更具弹性,你可以启动一个“主”线程来启动子线程,以响应使你的服务完成工作的外部事件。 Having a child thread terminated because of an unhandled exception is something you could possibly recover from. 由于未处理的异常导致子线程终止是您可以从中恢复的。 But then, if you make that next step, why not have the child thread catch an exception and pass it back to the master thread so it can make an intelligent decision about what to do next. 但是,如果您进行下一步,为什么不让子线程捕获异常并将其传递回主线程,以便它可以明智地决定下一步该做什么。

The cold hard fact of the default CLR host is: if you are not willing to deal with failure, it is not going to do the job for you. 默认CLR主机的冷酷事实是:如果您不愿意处理失败,那么它就不会为您完成任务。 And it shouldn't, the .NET 1.x behavior to threads that died with exceptions was a major mistake that got corrected in .NET 2.0. 它不应该,.NET 1.x行为导致死机异常死亡是一个在.NET 2.0中得到纠正的重大错误。

You know what to do: handle failure. 你知道该怎么做:处理失败。 Or write you own host. 或者写你自己的主人。 Or accept that things could be beyond your control and log a good error message so you can tell your customer what to do. 或者接受事情可能超出您的控制并记录一条好的错误消息,以便您可以告诉客户该做什么。 I'd strongly recommend the latter. 我强烈推荐后者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM