Is it possible to use the new project.json file to add NuGet packages to a standard .NET 4.6 Class Library? If so, how?
1 Answers
Is it possible to use the new project.json file to add NuGet packages to a standard .NET 4.6 Class Library?
I'm not sure what "standard" means, but if you're running in the full .NET runtime, then no.
The responsibility of loading dependencies is of the runtime. The CoreCLR runtime uses coreclr.dll
as opposed to the full .NET framework which uses clr.dll
.
When using CoreCLR, the LoaderContainer
that is used is aware of the project.json
file, and thus searches there for which dependencies to load. The process off loading dependencies is described in DNX Structure inside the ASP.NET 5 Documentation.
If you really want to get into the details of how it's done, you can look at DefaultHost.cs
and ApplicationHostContext.cs
inside the aspnet/dnx solution on GitHub.
Layer 1 : CLR Native Host:
This layer specific to the version of the CLR that you are using and has two main responsibilities:
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.
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

- 146,575
- 32
- 257
- 321