AppDomain, handling the exceptions

前端 未结 4 770
孤街浪徒
孤街浪徒 2020-12-08 22:30

I am developing a large application which consists of many smaller plugins/applications.

They are not big enough to be a full process, but are too small to be run in

4条回答
  •  醉酒成梦
    2020-12-08 22:35

    There are two problems with an unhandled exception. AppDomain solves only one of them. You're trying to deal with the other one.

    Good news first. When you handle an exception, you have to restore the program state as though the exception never happened. Everything has to be rewound to the before-the-code-ran state. You normally have a bunch of catch and finally clauses that undo the state mutations performed by code. Nothing very simple of course. But entirely impossible if the exception is unhandled. You have no idea exactly what got mutated and how to restore it. AppDomain handles this very difficult problem with aplomb. You unload it and whatever state was left is just gone. No more garbage collected heap, no more loader heap (statics). The whole enchilada gets reset to whatever the state was before you create the AppDomain.

    That's great. But there's another problem that's pretty hard to deal with as well. Your program was asked to perform a job. The thread set off to do that job. But it suffered a heart attack. Big problem number one: the thread is dead. That's pretty bad news if your program had only one thread to begin with. There's no thread left, the program terminates. Nice that the AppDomain unloaded first, but it really doesn't make any difference, it would have got unloaded anyway.

    Big problem too: it was really rather important that this job got done. It didn't. That matters, the job was, say, to balance the corporate profit and loss statement. That didn't get done, somebody is going to have to take care of that because not balancing the statement is going to get a lot of people very upset.

    How do you solve that?

    There are only a few selected scenarios where that's acceptable. Server scenarios. Somebody asks it to do something, the server reports back "couldn't do it, please contact the system administrator". The way ASP.NET and SQL Server work. They use AppDomains to keep the server stable. And have system administrators to deal with the problems. You'll have to create that kind of support system to make AppDomains work for you.

提交回复
热议问题