简体   繁体   English

在.Net标准2.0项目中添加dll参考

[英]Adding dll reference in .Net standard 2.0 project

I want to add Dll reference of system assemblies System.Runtime.Caching assembly in my .net Standard 2.0 project(using VS2019).我想在我的 .net 标准 2.0 项目(使用 VS2019)中添加系统程序集System.Runtime.Caching程序集的 Dll 引用。 Due to some policy restrictions nuget usage is not possible so we need to add a dll reference(assuming target system has same version of .net framework installed),由于某些政策限制 nuget 无法使用,因此我们需要添加 dll 参考(假设目标系统安装了相同版本的 .net 框架),

How can add this reference so that it works in different systems without using path?如何添加此引用以使其在不使用路径的情况下在不同系统中工作?

I tried following without any success:我尝试了以下但没有成功:

  1. Adding with reference manager dialog.添加引用管理器对话框。 This adds hint path(relative) which might not work in some systems(or when directory is changed).这会添加提示路径(相对),这可能在某些系统中不起作用(或在更改目录时)。
  2. Adding <PackageReference> adds nuget package which is restricted hence can not be used.添加<PackageReference>会添加 nuget package,因此无法使用。
  3. Tried adding override to Microsoft.Common.targets as <AssemblySearchPaths Condition=" '$(AssemblySearchPaths)' == ''">{GAC}</AssemblySearchPaths> in csproj so that GAC is searched first.尝试在 csproj 中将覆盖添加到 Microsoft.Common.targets 作为<AssemblySearchPaths Condition=" '$(AssemblySearchPaths)' == ''">{GAC}</AssemblySearchPaths>以便首先搜索 GAC。 Note: Third party package management or nuget local feed is not being accepted.注意:不接受第三方 package 管理或 nuget 本地供稿。 Also msbuild.common.targets file has {TargetFrameworkDirectory} option for resolving assemblies, so I tried adding <Reference Include="System.Xml"> to check if msbuild would search in target framework binaries but I still get same error.此外 msbuild.common.targets 文件具有 {TargetFrameworkDirectory} 用于解析程序集的选项,因此我尝试添加<Reference Include="System.Xml">以检查 msbuild 是否会在目标框架二进制文件中搜索,但我仍然遇到相同的错误。

In old style .net framework csproj structure(not SDK style) Adding something like <Reference Include="System.Xml"> used to work at runtime and compile time both, but it does not anymore.在旧样式中 .net 框架 csproj 结构(不是 SDK 样式)添加类似<Reference Include="System.Xml">曾经在运行时和编译时都可以工作的东西,但它不再适用了。

Does DLL reference as mentioned above does not work in .net standard and core anymore?上面提到的 DLL 参考是否在 .net 标准和核心中不再起作用?

You don't want to fight with current .NET/MSBuild architecture and NuGet.您不想与当前的 .NET/MSBuild 架构和 NuGet 抗争。 If you cannot create a local NuGet server you need to provide your path to libraries on your own.如果您无法创建本地 NuGet 服务器,则需要自行提供库路径。 MSBuild is smart enough to find packages but if you will not provide any feed he will not resolve all of them. MSBuild 足够聪明,可以找到包,但如果您不提供任何提要,他将无法解决所有问题。 Under the hood, MSBuild has hardcoded paths that search for libraries as a fallback but these paths change over time when the framework evolves.在底层,MSBuild 具有硬编码路径,用于搜索库作为后备,但随着框架的发展,这些路径会随着时间而变化。

.NET Framework isn't equal to .NET Core, it's the standalone solution not related to Windows anymore so you shouldn't assume it will work as the previous framework. .NET 框架不等于 .NET 核心,它是与 Windows 无关的独立解决方案,所以你不应该假设它会像以前的框架一样工作。

Only one stable solution is to include all libs inside the project folder.只有一种稳定的解决方案是在项目文件夹中包含所有库。

Reference still works but you need to supply a path like that(otherwise MSBuild probably will struggle to find libraries):参考仍然有效,但您需要提供这样的路径(否则 MSBuild 可能很难找到库):

  <ItemGroup>
    <Reference Include="lib\$(TargetFramework)\*.dll" HintPath="%(FilePath)" />
  </ItemGroup>

I think you should speak with your client and tell him he needs a more robust solution than fighting with a chosen framework because it's detrimental for him and you.我认为您应该与您的客户交谈并告诉他,他需要一个比使用所选框架更强大的解决方案,因为这对他和您都是有害的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM