简体   繁体   English

启动 .net 应用程序时会发生什么?

[英]What happens when a .net application is started?

I have been developing apps using .net for quite sometime now.很长一段时间以来,我一直在使用 .net 开发应用程序。 But, I am still not sure how does the CLR know that a .net app has started.但是,我仍然不确定 CLR 是如何知道 .net 应用程序已启动的。 Is there like one instance of CLR per app?每个应用程序是否有一个 CLR 实例? I don't think this can be the case as there is just one GC which manages all the memory for all .net apps.我认为情况并非如此,因为只有一个 GC 管理所有 .net 应用程序的所有 memory。 Does the CLR kind of run in background? CLR 会在后台运行吗? I am quite confused.我很困惑。

Hmm, let me take a shot at this too.嗯,我也试试这个。

  1. Somebody builds a .NET application in C#, or .NET 'Intermediate Language', or another managed language.有人在 C# 或 .NET 'Intermediate Language' 或其他托管语言中构建 .NET 应用程序。

  2. The compiler for that language csc.exe (C#), or ilasm.exe (bytecode assembler), or whichever, produces a PE executable.该语言的编译器 csc.exe (C#) 或 ilasm.exe(字节码汇编器),或任何一个,生成一个 PE 可执行文件。 The PE executable has a specific structure that the compiler or assembler populates. PE 可执行文件具有编译器或汇编器填充的特定结构。 That includes:那包含着:

    • an entry point, and入口点,以及
    • a list of dynamic libraries that it uses (the IMPORT table).它使用的动态库列表(IMPORT 表)。 One of those libraries is mscoree.dll其中一个库是mscoree.dll
    • lots of metadata, including the targeted .NET runtime version大量元数据,包括目标 .NET 运行时版本
  3. When the executable is clicked on, ran from the command line, or executed from a Win32 API, the Windows loader implementation (in NTDLL.dll) takes over当单击可执行文件、从命令行运行或从 Win32 API 执行时, Windows 加载程序实现(在 NTDLL.dll 中)接管

  4. The loader code is responsible for getting the executable into memory, loading dynamic link libraries if needed, mapping linked libraries into a place the executable code can reach them, and updating the Import Address Table with the actual addresses of the mapped libraries.加载器代码负责将可执行文件放入 memory,在需要时加载动态链接库,将链接库映射到可执行代码可以到达的位置,并使用映射库的实际地址更新导入地址表。

  5. Once all is ready, the loader jumps to the entry point (through what I assume is some shenanigans switching from kernel space to user space, or to protected mode, since the application runs in it's own protected 32 or 64 bit memory space).一旦一切准备就绪,加载程序就会跳转到入口点(通过我假设是一些恶作剧从 kernel 空间切换到用户空间或保护模式,因为应用程序在它自己的受保护的 32 位或 64 位 memory 空间中运行)。 The entry point goes to mscoree.dll - the .NET Common Object Runtime Execution Engine, which was just mapped into the processes memory. The entry point goes to mscoree.dll - the .NET Common Object Runtime Execution Engine, which was just mapped into the processes memory. I've seen this DLL referred to as the .NET startup shim, and it allows the multiple installs of .NET to exist on one machine.我见过这个 DLL 被称为 .NET 启动垫片,它允许 .NET 的多次安装存在于一台机器上。 Mscoree.dll is the library you'll use if you are embedding a .NET language in your own regular application.如果您在自己的常规应用程序中嵌入 .NET 语言,则 Mscoree.dll 是您将使用的库。

  6. Mscoree.dll looks at the metadata loaded from the PE executable, specifically the CLR header, and the targeted .NET runtime version. Mscoree.dll 查看从 PE 可执行文件加载的元数据,特别是 CLR header 和目标 .NET 运行时版本。 From that it can CorBindToRuntimeEx 2 to the right CLR version.从它可以从 CorBindToRuntimeEx 2到正确的 CLR 版本。

  7. The CorBindToRuntimeEx loads the correct .NET runtime implementation (and returns a pointer to a COM interface allowing you to invoke that .NET runtime. This code is loaded from the dlls in %WINDIR%\Microsoft.NET\Framework\v#####. CorBindToRuntimeEx 加载正确的 .NET 运行时实现(并返回指向 COM 接口的指针,允许您调用 .NET 运行时。此代码是从 %WIND.

  8. I'm not sure who at this point, but probably the mscoree shim uses the .NET ICLRRuntimeHost interface pointer to invoke methods to initialize the .NET runtime, garbage collector, IL interpreter, JIT and IHostControl interfaces (that allow the .NET interpreter to talk back to the hosting process), and ultimately tells the Interpreter to start executing your compiled application's IL code.我不确定此时是谁,但 mscoree shim 可能使用 .NET ICLRRuntimeHost 接口指针来调用方法来初始化 .NET 运行时、垃圾收集器、IL 解释器、JIT 和 IHostControl 接口(这允许 Z303CB0EF9EDB9082AZ1 解释回话825托管进程),并最终告诉解释器开始执行您编译的应用程序的 IL 代码。

(I learnt a lot writing this - there is a ton of information behind the links, I certainly didn't get through all of it!) (我在写这篇文章时学到了很多——链接后面有很多信息,我当然没有全部看完!)

http://msdn.microsoft.com/en-us/library/xh0859k0.aspx http://msdn.microsoft.com/en-us/library/xh0859k0.aspx

http://my.safaribooksonline.com/book/programming/microsoft-dotnet/0735619883/a-tour-of-the-clr-hosting-api/ch02lev1sec3 http://my.safaribooksonline.com/book/programming/microsoft-dotnet/0735619883/a-tour-of-the-clr-hosting-api/ch02lev1sec3

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

Windows executables are Portable Executables , a format which gives Windows the information it needs to load and run the program. Windows 可执行文件是可移植的可执行文件,这种格式为 Windows 提供了加载和运行程序所需的信息。 When Windows encounters a .NET program it loads an instance of the CLR and hands execution of the program to the new CLR instance.当 Windows 遇到 .NET 程序时,它会加载 CLR 的实例并将程序的执行交给新的 CLR 实例。 Each running .NET program is hosted within it's own instance of the CLR.每个正在运行的 .NET 程序都托管在它自己的 CLR 实例中。

The CLR process loads the IL program and compiles it to Native Code (JIT) then executes the code, taking care of memory management and garbage collection for that program. CLR 进程加载 IL 程序并将其编译为本地代码 (JIT),然后执行代码,负责该程序的 memory 管理和垃圾收集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署.NET Web应用程序时会发生什么? - What happens when I deploy a .NET web application? 在IISIntegration上运行ASP.NET Core应用程序时,Program.Main会发生什么情况? - What happens of Program.Main when an ASP.NET Core application is run on IISIntegration? IIS 6.0上的asp.net Web应用程序(asmx ws)空闲数小时左右会发生什么? - what happens to asp.net web application (asmx ws) on IIS 6.0 when it's idle for hours or so? 当应用程序池在 ASP.NET MVC 中回收时会发生什么? - What happens when application pool re-cycles in ASP.NET MVC? 当.NET线程抛出异常时会发生什么? - What happens when a .NET thread throws an exception? 重新部署后,IIS启动线程会怎样? - What happens to a IIS started thread after a redeploy? 当我的应用程序消耗MSMQ消息然后失败时会发生什么? - What happens when my application consumes an MSMQ message but then fails? 用户单击.NET程序集(EXE)时会发生什么? - What happens when user click .NET assembly (EXE)? .NET控制台应用程序在Console.ReadLine()上阻止时会发生什么? - What happens when a .NET console app blocks on Console.ReadLine()? 在 .NET Framework 中编译代码时,引用会发生什么变化? - What happens to the references when a code is Compied in .NET Framework?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM