.NET Core 程序集搜索顺序 [英] .NET Core assembly search order

查看:28
本文介绍了.NET Core 程序集搜索顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 .NET Framework 中,加载程序在当前文件夹中搜索依赖项,然后在 GAC 中搜索.

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

  1. 但是在 .NET Core 中没有 GAC,所以 .NET Core 程序集加载器是否也只搜索当前文件夹或全局 nuget 缓存(someuser/.nuget/packages 文件夹)中的程序集?

  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?

我还在 Mac 中找到了一个/usr/local/share/dotnet/shared 文件夹(Windows 中为 C:Program Filesdotnetshared),所有基本库都位于该文件夹中,例如 System.Runtime.dll, System.Collections.dll.
程序集加载器也在那里吗?

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

我还发现这些基础库在全局 nuget 缓存中也有重复.
为什么?

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

推荐答案

PackageCompilationAssemblyResolver 使用的目录在很大程度上取决于您的环境.但是,它们可能包括:

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

  • C:Program Filesdotnetstorex64 etcoreapp2.0(或您机器的等效路径)
  • C:Users{user}.nugetpackages
  • C:Program FilesdotnetsdkNuGetFallbackFolder
  • C:Program Filesdotnetstorex64 etcoreapp2.0 (or equivalent path for your machine)
  • C:Users{user}.nugetpackages
  • C:Program FilesdotnetsdkNuGetFallbackFolder

请注意,这仅适用于包(即 NuGet).存在用于引用和应用程序程序集的其他程序集解析器...

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}\.nugetpackages

});

有关在代码中管理程序集解析的更多详细信息,请参阅本文 - Resolving.NET Core 中的程序集

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

这篇关于.NET Core 程序集搜索顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆