在 csproj Class 库中使用 project.json

Using project.json in csproj Class Library

是否可以使用新的 project.json 文件将 NuGet 包添加到标准 .NET 4.6 Class 库中?如果可以,怎么做?

Is it possible to use the new project.json file to add NuGet packages to a standard .NET 4.6 Class Library?

我不确定 "standard" 是什么意思,但是如果您 运行 在完整的 .NET 运行时中,那么不会。

加载依赖项是运行时的责任。 CoreCLR 运行时使用 coreclr.dll 而不是使用 clr.dll 的完整 .NET 框架。

使用 CoreCLR 时,使用的 LoaderContainer 知道 project.json 文件,因此会在其中搜索要加载的依赖项。 ASP.NET 5 文档中的 DNX Structure 描述了卸载依赖项的过程。

如果您真的想了解它是如何完成的细节,您可以查看 GitHub 上的 DefaultHost.cs and ApplicationHostContext.cs inside the aspnet/dnx 解决方案。

Layer 1 : CLR Native Host:

This layer specific to the version of the CLR that you are using and has two main responsibilities:

  1. Boot the CLR, how this is achieved depends on the version of the CLR. For Core CLR the process involves loading coreclr.dll, configuring and starting the runtime, and creating the AppDomain that all managed code will run in.

  2. Calling the Managed Entry Point, Layer 2. When the entry point of the Native Host returns this process will then cleanup and shutdown the CLR. i.e unload the app domain and stop the runtime.

Layer 2 : Managed Entry Point

This layer is the first layer that is written in managed code, it is responsible for:

  • Creating the LoaderContainer that will contain the required ILoaders. An ILoader is responsible for loading an assembly by name. When the CLR asks for an assembly to be resolved the LoaderContainer will resolve the required assembly using its ILoaders.

  • Provide the root ILoader that will load assemblies, and satisfy dependencies, from the provided --lib provided when running the native process. This is usually the DNX package itself.

  • Call the main entry point of the provided program.

Layer 3: Application host / Application

If a user compiles their entire application to assemblies on disk in the libpath then this layer is your application. To do this you pass the name of the assembly containing your applications entry point in the [ProgramName] argument and layer 2 will invoke it directly. However, in all other scenarios you would use an application host to resolve app dependencies and run your app. Microsoft.Net.ApplicationHost is the application host provided in the runtime, and has a few responsibilities:

  • Walks the dependencies in the project.json and builds up the closure of dependencies the app will use. The dependency walking logic is described in more detail in the dependency resolution documentation

  • Adds an ILoader to the LoaderContainer that can load assemblies from various sources, NuGet, Roslyn, etc.

  • Calls the entry point of the assembly whose name is given as the next argument to the native process