.Net Runtime Host

CLR 没有与任何操作系统集成,也就是说,当用户运行一个托管代码程序或者一个Native代码的程序时,OS对其是毫不知情的,所以也就没法根据其是什么类型的代码来决定是否要调用CLR来运行特定的程序。因此进程创建时必须由自己完成CLR的加载操作。

加载CLR涉及到运行库宿主(Runtime Host) 。Runtime host需要加载CLR,因此他本身至少包含是Native代码的。

Most runtime hosts consist of both unmanaged and managed code. The unmanaged hosting code loads the runtime into the process when the process starts.

多数runtime host既包含托管代码又包含非托管代码。当进程启动时,非托管部分的hosting代码加载runtime到进程中。

Once the runtime has been loaded into the process, control can be transferred to the managed portion of the hosting code, to improve performance.

一旦runtime被加载到进程中,控制就会被转送到hosting的托管代码中,从而提高性能。

Implementing the portion of the host that interacts with user code in managed code results in better performance because calls from the hosting code to the user code are made in the managed environment. If you wrote the entire host in unmanaged code, you would have to transition from unmanaged code to managed code each time the hosting code and the user code interact.

在托管代码中实现与用户代码交互部分可以得到更好的性能,因为从hosting代码到用户代码的调用是在托管环境中的。如果你把整个host都写成非托管代码,那每次托管代码和用户代码交互时,就不得不在托管代码和给托管代码间转换。

Unmanaged hosting code is used to configure the common language runtime, load it in the process, and transition the program into managed code. The managed portion of the hosting code creates the application domains in which user code will run and dispatches user requests to the created application domains.

非托管代码用于配置CLR,并将其加载到进程中,将程序转换到托管代码中。托管部分代码创建application domain,用户代码可以在其中执行,并dispatch用户请求给创建好的application domain。

Runtime Host的职责:

1. 加载CLR;

2. 创建AppDomain

3. 处理未捕获的异常

Microsoft .NET: Implement a Custom Common Language Runtime Host for Your Managed App

http://msdn.microsoft.com/en-us/magazine/cc301479.aspx

原文地址:https://www.cnblogs.com/whyandinside/p/1765707.html