Assemblies and application domains

Source: Internet
Author: User

. NET assemblies are used to resolve DLL HELL (that is, DLL-related issues). An assembly is a self-describing logical unit, not a file. An assembly can be a file that contains metadata, or it can be a DLL or an EXE file. In summary, assemblies are published as a whole. NET executable or a part of a. NET executable program.

Assemblies are divided into private assemblies and shared assemblies, and private assemblies are created. NET project, the private assembly is provided to the application in the form of an executable program or library, and the code in the library serves only this application. A shared assembly is a common library that can serve all programs in the system. Shared assemblies are installed to the. NET, and other programs that are serviced do not need to know where to install.

PS: metadata (Metadata), also known as intermediary data , relay Data , is the data that describes the data It mainly describes the information of data property, which is used to support functions such as indicating storage location, historical data, resource lookup, file record, etc. (from Baidu Encyclopedia)

Any. NET programs consist of assemblies, which are code logical units that contain compiled, oriented. NET Framework. When an assembly is stored in multiple files, there must be a primary file that contains the portal of the Assembly's main program. A process can accommodate multiple application sets, so that. NET enables multiple applications to run in the same process, and in the absence of proxies, instances and static members in different application domains cannot be shared, thus guaranteeing security.

Demonstrates simple application of assemblies and application domains. New Project Applicationa, write the following code in Program.cs:

namespace applicationa{    class  program    {        staticvoid Main (  String[] args)        {            // Create the current of the AppDomain for reference Appdomain.currentdomain             AppDomain current = appdomain.currentdomain;             // the name            of the application domain for the output program Console.WriteLine (" This is assembly A, I am in the application domain is {0}", current.) FriendlyName);     }}}

It is visible that the name of the application domain that is the default is the program name when the application runs. Now, Applicationa has compiled the private assembly, which is located under the Debug folder of the project.

New Project APPLICATIONB, write the following code in Program.cs:

namespaceapplicationb{classProgram {Static voidMain (string[] args) {AppDomain current=Appdomain.currentdomain; Console.WriteLine ("This is assembly B, my base directory is {0}, the name of the application domain I am in and the context policy: {1}", current. Basedirectory,current.            ToString ()); Current. ExecuteAssembly ("ApplicationA.exe"); Console.WriteLine ("Loading Applicationa Assemblies"); AppDomain After= Appdomain.createdomain ("room-a"); After. ExecuteAssembly ("ApplicationA.exe"); Console.WriteLine ("Applicationa The base directory of the assembly: {The name of the application domain where the 0}\napplicationa assembly resides and the context policy: {1}", after. BaseDirectory, after.        ToString ()); }    }}

Before executing the code, you need to add a reference to the Applicationa, which is actually copying the application assembly directly to the same directory in Applicationb, which also fully reflects the convenience of the private assembly. At this point, applicationb can load the Applicationa assembly into the new application domain when it executes.

The output is this:

You can see that a is first loaded into the current application domain and then loaded into an application domain named: "Room-a", as you can see from Task Manager, A does not have A new process. In fact, the Assembly of a has already been loaded into the ApplicationB.exe process, which also achieves the purpose of running multiple applications in the same process.

(Please ignore. vshost, I directly debug get, Visual Studio debugging when the actual is ApplicationA.vshost.exe, this is just the compiler for debugging convenience, but actually the debug directory has a ApplicationA.exe)

Assemblies and application domains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.