I read about the application domains and did not understand a bit about the circumstances under which the assemblies should be loaded into a separate domain.

As I understand it, in terms of functionality, this is similar to the standard dynamic library loading (late binding), but with one difference, the domains are isolated between themselves and some kind of interaction protocol should be used for interaction (for example, WCF).

I correctly understand that this is something like: An application that contains the Nth number of other isolated applications?

  • 2
    Domains should be used if there is a need to unload assemblies. Assembly cannot be unloaded from the application. But if you upload it to the domain, you can unload the entire domain. - Alexander Petrov
  • for the interaction of domains, you need to register exported and imported interfaces, if you use MEF, or make a full MAF binding, both options are almost equivalent at the present time and are supported by .NET itself without using third-party libraries. Actually by the names of MEF and MAF and need to look for documentation. Well, one more thing, all imported / exported types are required to support marshaling by value or reference, otherwise you will get a bunch of exceptions. Also, the reflection does not work across the domain boundaries, GetType () will always return MarshalByRefObject for reference types. - rdorn

1 answer 1

There are several scenarios in which domains are needed.

For example, this is the execution of a code of unknown origin. If your application loads the plugin from the Internet, then it may be a hostile code. You can transfer it to another domain with reduced rights so that it does not have access to the file system, P / Invoke or reflection.

Further, if the code is not malicious, but simply “falling”, then again it makes sense to unload it into a separate domain so that when it falls, it will not “drag” the main application behind it.

The following script is mentioned by @Alexander Petrov in a comment: you cannot upload the downloaded assembly, but you can upload the whole domain. Uploading an assembly may be needed to delete a file with it (for example, you want to download a new version of the plugin).

Another scenario is that you can get rid of a very large static object in this way. (In terms of language, a static object lives forever, but in fact it will be unloaded along with the domain.)

  • Can you also describe how isolation is manifested? Ie I can load the assembly into another domain, call some methods, but I can’t get any returned data? - iluxa1810
  • 2
    @ iluxa1810: Well, if you create another domain, you can call a method in it and drag the result across the domain boundary. The isolation is that (1) if the domain falls, then your application will continue to run, (2) you can cut down his rights, and he will not be able to work with your domain, even if he wants. - VladD