The host and how the host loads the CLR are discussed earlier. It also discusses how the host tells the CLR to create and unload an AppDomain. To make these discussions more specific, the following describes some common host and AppDomain usage scenarios. In particular, I want to explain how different application types host the CLR, and how they manage the AppDomain.
1 Executable applications
Console UI applications, NT Service applications, Windows Forms applications, and WPF applications are examples of self-hosted applications, all of which have managed EXE files. When Windows Initializes a process with a managed EXE file, the shim is loaded. The shim examines the CLR header information contained in the Assembly of the application. The header information indicates the version of the CLR that was used when the application was built and tested. The shim determines which version of the CLR is loaded into the process based on this information, and after the CLR is loaded and initialized, it checks the assembly's CLR header to determine which of the application's entry methods (Main). The CLR calls this method. At this point, the application will actually start and run.
When the code runs, it accesses other types. When you reference a type in another assembly, the CLR is set to the desired assembly and loaded into the same AppDomain. After the application's main method returns, the Windows process terminates (destroying the default AppDomain and all other AppDomain).
Note: By the way, if you want to close the window process (including all of its AppDomain), You can call the static method of System.Environment exit, which terminates the process in the most appropriate way, because it first invokes the Finalize method of all objects on the managed heap, and then releases any unmanaged COM objects held by the CLR. Finally, exit calls Win32 's ExitProcess function.
The application can tell the CLR to create additional AppDomain in the address space of the process.
2.Silverlight Rich Internet applications
Microsift's Silverlight "run-time" technology employs a special CLR that differs from the normal desktop version of the. NET Framework. Once the Silverlight runtime is installed, each access to a Web site using Silverlight technology causes the Silverlight CLR (CoreClr.dll) to load into the browser (which may be, It may not be ie-or even a Windows machine. Each Silverlight control on a Web page runs in its own AppDomain. When a user closes a tab, or switches to another site, the AppDomain for any Silverlight control that is no longer in use is unloaded. Silverlight code in the AppDomain runs in a restricted security sandbox and does not cause damage to users or machines in any way
3.asp.net Web Forms and XML Web server applications
ASP. NET is implemented as an ISAPI DLL (aspnet_isapi.dll). ASP when the customer first requests a URL that is processed by an ASP. NET ISAPI DLL. NET loads the CLR. When a client requests a Web application, ASP. NET to judge if this is not the first request. If yes, ASP. NET tells the CLR to create a new AppDomain for the Web application each Web application is identified by its virtual root directory. Then, ASP. NET instructs the CLR to load an assembly containing the type exposed by the application into the new AppDomain, create an instance of that type, and invoke its methods in response to the client's Web request. If the code references more types, the CLR loads the required assemblies into the AppDomain of the Web application.
When a future client requests a Web application that has started running, ASP. NET no longer creates an appdomain; instead, it uses the AppDomain now to create a new instance of the type of the Web application and start calling the method. These methods have been JIT-compiled with cost code, so the processing performance of subsequent client requests will be better.
If the client requests a different Web application, ASP. NET tells the CLR to create a new AppDomain. The new AppDomain is usually created in the same work process as the other AppDomain. This means that many Web applications run in the same window process, which increases overall system efficiency. Similarly, the assemblies required for each Web application are loaded into a separate AppDomain, the only purpose of which is to tell the code of the Web application and other Web application isolation.
Asp. NET another great feature is the ability to dynamically change the code of a Web site without shutting down the Web server. The Web site's files are changed on the hard disk when ASP. NET detects the situation and uninstalls the AppDomain that contains the old version of the file and creates a new AppDomain to which to load the new version of the file. To ensure that the process runs smoothly, ASP. NET uses a feature called "Image Copy" in the AppDomain.
4. SQL Server
Microsoft SQL Server is an unmanaged application because most of its code is still written in unmanaged C + +. SQL Server allows developers to create stored procedures through managed code. When you first request a database to run a stored procedure that is written in managed code, SQL Server loads the CLR. Stored procedures run in their own secure AppDomain, which avoids the negative impact stored procedures have on the database server.
This is actually an extraordinary feature! This means that developers can choose their preferred programming language to write stored procedures. Stored procedures can use strongly typed data objects in their own code. The code is also JIT-compiled with cost-code, rather than in the way that parsing is performed. Developers can take advantage of any type defined by the FCL or any other assembly. The result is that our work is getting easier, but the application is doing better.
5. More usage is confined to your own imagination
Productive applications, such as word processing software and spreadsheet software, also allow users to write macros in any programming language. Macros can access all the assemblies and types that run with the CLR. These macros will be compiled as they execute faster. And most importantly, these macros will run in a secure AppDomain to avoid any negative impact on any user. Your own application can also take advantage of this feature. How to use it is confined to your own imagination.
Section Sixth: How the host uses the AppDomain