.NET Core assembly search order

2019-04-08 17:06发布

问题:

In .NET Framework, the loader searches dependencies in current folder and then in GAC.

  1. But in .NET Core there is no GAC, so does .NET Core assembly loader only search assemblies in current folder or in global nuget cache(someuser/.nuget/packages folder) also?

  2. Also I found a /usr/local/share/dotnet/shared folder in my Mac(C:\Program Files\dotnet\shared in Windows) where all base libraries are located, like System.Runtime.dll, System.Collections.dll.
    Does assembly loader looks there too?

  3. Also I found that these base libraries are duplicated also in in global nuget cache.
    Why?

回答1:

The directories used by the PackageCompilationAssemblyResolver will be largely dependent on your environment. However, they may include:

  • C:\Program Files\dotnet\store\x64\netcoreapp2.0 (or equivalent path for your machine)
  • C:\Users\{user}\.nuget\packages
  • C:\Program Files\dotnet\sdk\NuGetFallbackFolder

Note that this is just for packages (i.e NuGet). Other assembly resolvers exist for references and application assemblies...

Note that if you are using an assembly resolver directly in your code, you can specify the paths used for resolution as follows:

ICompilationAssemblyResolver assemblyResolver = new CompositeCompilationAssemblyResolver
                        (new ICompilationAssemblyResolver[]
{
    new AppBaseCompilationAssemblyResolver(basePath),       //e.g. project path
    new ReferenceAssemblyPathResolver(defaultReferenceAssembliesPath, fallbackSearchPaths),
    new PackageCompilationAssemblyResolver(nugetPackageDirectory)   //e.g. C:\Users\\{user}\\.nuget\packages

});

For more details on managing the resolution of assemblies within you code, see this article - Resolving Assemblies in .NET Core



回答2:

.Net core is based on Nuget packages being cross platform. NET Core applications rely heavily on NuGet to resolve their dependencies, which simplifies development.

You can check link Is there any GAC equivalent for .NET Core?