Good example of use of AppDomain

后端 未结 7 1656
傲寒
傲寒 2020-12-07 10:25

I keep getting asked about AppDomains in interviews, and I know the basics:

  • they are an isolation level within an application (making them different from appl
7条回答
  •  清歌不尽
    2020-12-07 11:01

    I see 2 or 3 main use cases for creating separate application domains:

    1) Process-like isolation with low resource usage and overhead. For example, this is what ASP.NET does - it hosts each website in a separate application domain. If it used different threads in the single application domain then code of different websites could interfere with each other. If it hosted different websites in different processes - it would use much resources and also interprocess communication is relatively difficult comparing to inprocess communication.

    2) Executing untrusted code in a separate application domain with particular security permissions (this is actually related to 1st reason). As people already said, you could load 3rd party plugins or untrusted dlls into separate application domains.

    3) Ability to unload assemblies to reduce unnecessary memory usage. Unfortunately, there is no way to unload an assembly from an application domain. So if you load some big assembly to your main application domain, the only way to free the corresponding memory after that assembly is not needed anymore is to close your application. Loading assemblies in a separate application domain and unloading that application domain when those assemblies are not needed anymore is a solution to this problem.

提交回复
热议问题