I'm fairly new to reflection and I was wonder what I would use a (second) AppDomain for? What practical application would one have in a business application?
我对反思很新,我很想知道我会用什么(第二个)AppDomain?在商业应用程序中有什么实际应用?
3 个解决方案
#1
There are numerous uses. An secondary AppDomain can provide a degree of isolation that is similar to the isolation an OS provides processes.
有很多用途。辅助AppDomain可以提供一定程度的隔离,类似于OS提供进程的隔离。
One practical use that I've used it for is dynamically loading "plug-in" DLLs. I wanted to support scanning a directory for DLLs at startup of the main executable, loading them and checking their types to see if any implemented a specific interface (i.e. the contract of the plug-in). Without creating a secondary AppDomain, you have no way to unload a DLL/assembly that may not have any types that implement the interface sought. Rather than carry around extra assemblies and types, etc. in your process, you can create a secondary AppDomain, load the assembly there and then examine the types. When you're done, you can get rid of the secondary AppDomain and thus your types.
我用它的一个实际用途是动态加载“插件”DLL。我想支持在主可执行文件启动时扫描DLL目录,加载它们并检查它们的类型以查看是否有任何实现特定接口(即插件的合同)。在不创建辅助AppDomain的情况下,您无法卸载可能没有任何实现所寻求接口的类型的DLL /程序集。您可以创建辅助AppDomain,在那里加载程序集,然后检查类型,而不是在流程中携带额外的程序集和类型等。当你完成后,你可以摆脱辅助AppDomain,从而摆脱你的类型。
#2
99% of the time I would avoid additional AppDomains. They are essentially separate processes. You must marshal data from one domain to the other which adds complexity and performance issues.
99%的时间我会避免使用其他AppDomains。它们本质上是分开的过程。您必须将数据从一个域封送到另一个域,这会增加复杂性和性能问题。
People have attempted to use AppDomains to get around the problem that you can't unload assemblies once they have been loaded into an AppDomain. So you create a second AppDomain where you can load your dynamic Assemblies and then unload the complete AppDomain to free the memory associated with the Assemblies.
人们已经尝试使用AppDomains来解决在将程序集加载到AppDomain后无法卸载程序集的问题。因此,您可以创建第二个AppDomain,您可以在其中加载动态程序集,然后卸载完整的AppDomain以释放与程序集关联的内存。
Unless you need to dynamically load & unload Assemblies they are not really worth worrying about.
除非您需要动态加载和卸载程序集,否则它们并不值得担心。
#3
AppDomains are useful when you have to have multiple instances of a singleton. For example, you have an assembly that implements a communication protocol to some device and this assembly uses singletons. If you want to instantiate multiple instances of this class (to talk to multiple devices) and you want the instances to not interfere with one another, then AppDomains are perfect for this purpose.
当您必须拥有单个实例的多个实例时,AppDomains非常有用。例如,您有一个程序集,它实现了某些设备的通信协议,并且此程序集使用单例。如果要实例化此类的多个实例(与多个设备通信)并且您希望实例不会相互干扰,那么AppDomains非常适合此目的。
It does make programming more difficult, however, as you have to do more work to communicate across AppDomain boundaries.
但是,它确实使编程变得更加困难,因为您必须做更多的工作来跨AppDomain边界进行通信。
#1
There are numerous uses. An secondary AppDomain can provide a degree of isolation that is similar to the isolation an OS provides processes.
有很多用途。辅助AppDomain可以提供一定程度的隔离,类似于OS提供进程的隔离。
One practical use that I've used it for is dynamically loading "plug-in" DLLs. I wanted to support scanning a directory for DLLs at startup of the main executable, loading them and checking their types to see if any implemented a specific interface (i.e. the contract of the plug-in). Without creating a secondary AppDomain, you have no way to unload a DLL/assembly that may not have any types that implement the interface sought. Rather than carry around extra assemblies and types, etc. in your process, you can create a secondary AppDomain, load the assembly there and then examine the types. When you're done, you can get rid of the secondary AppDomain and thus your types.
我用它的一个实际用途是动态加载“插件”DLL。我想支持在主可执行文件启动时扫描DLL目录,加载它们并检查它们的类型以查看是否有任何实现特定接口(即插件的合同)。在不创建辅助AppDomain的情况下,您无法卸载可能没有任何实现所寻求接口的类型的DLL /程序集。您可以创建辅助AppDomain,在那里加载程序集,然后检查类型,而不是在流程中携带额外的程序集和类型等。当你完成后,你可以摆脱辅助AppDomain,从而摆脱你的类型。
#2
99% of the time I would avoid additional AppDomains. They are essentially separate processes. You must marshal data from one domain to the other which adds complexity and performance issues.
99%的时间我会避免使用其他AppDomains。它们本质上是分开的过程。您必须将数据从一个域封送到另一个域,这会增加复杂性和性能问题。
People have attempted to use AppDomains to get around the problem that you can't unload assemblies once they have been loaded into an AppDomain. So you create a second AppDomain where you can load your dynamic Assemblies and then unload the complete AppDomain to free the memory associated with the Assemblies.
人们已经尝试使用AppDomains来解决在将程序集加载到AppDomain后无法卸载程序集的问题。因此,您可以创建第二个AppDomain,您可以在其中加载动态程序集,然后卸载完整的AppDomain以释放与程序集关联的内存。
Unless you need to dynamically load & unload Assemblies they are not really worth worrying about.
除非您需要动态加载和卸载程序集,否则它们并不值得担心。
#3
AppDomains are useful when you have to have multiple instances of a singleton. For example, you have an assembly that implements a communication protocol to some device and this assembly uses singletons. If you want to instantiate multiple instances of this class (to talk to multiple devices) and you want the instances to not interfere with one another, then AppDomains are perfect for this purpose.
当您必须拥有单个实例的多个实例时,AppDomains非常有用。例如,您有一个程序集,它实现了某些设备的通信协议,并且此程序集使用单例。如果要实例化此类的多个实例(与多个设备通信)并且您希望实例不会相互干扰,那么AppDomains非常适合此目的。
It does make programming more difficult, however, as you have to do more work to communicate across AppDomain boundaries.
但是,它确实使编程变得更加困难,因为您必须做更多的工作来跨AppDomain边界进行通信。