简体   繁体   English

如何在 nuget.config repositoryPath 和 csproj Reference HintPath 中使用变量

[英]How to use variables in nuget.config repositoryPath and csproj Reference HintPath

My company has NuGet package reference trouble due to our use of Multi-Homed Projects :由于我们使用多宿主项目,我的公司有 NuGet 包引用问题:

It's pretty common to include a project in mutliple solutions.在多个解决方案中包含一个项目是很常见的。 Unfortunately, NuGet cannot understand when this is the case.不幸的是,NuGet 无法理解这种情况。 And because NuGet uses a packages folder at the solution level, the Hint Paths in projects become relative to the solution.并且因为 NuGet 在解决方案级别使用包文件夹,项目中的提示路径变得相对于解决方案。 If there are multiple solutions using a project, these relative paths can easily break.如果有多个解决方案使用一个项目,这些相对路径很容易中断。

I'm trying to figure out a reasonable way to keep the Hint Paths from breaking.我试图找出一种合理的方法来防止提示路径中断。

One idea I had was to use an environment variable to point to a shared package folder (similar to how NuGet does it with .NET Core projects).我的一个想法是使用环境变量指向共享包文件夹(类似于 NuGet 在 .NET Core 项目中的做法)。

I've tried to create a nuget.config like the following:我尝试创建一个nuget.config所示的nuget.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>  
  <config>
    <add key="repositoryPath" value="%USERPROFILE%\.nuget\shared_packages" />
  </config>
</configuration>

And when I install a package it generates a Hint Path with a relative path:当我安装一个包时,它会生成一个带有相对路径的提示路径:

<Reference Include="QRCoder, Version=1.3.5.0, Culture=neutral, processorArchitecture=MSIL">
    <HintPath>..\..\..\..\Users\Username\.nuget\shared_packages\QRCoder.1.3.5\lib\net40\QRCoder.dll</HintPath>
</Reference>

This won't work on other machines so I have to manually edit the .csproj file to something like this:这在其他机器上不起作用,所以我必须手动编辑.csproj文件,如下所示:

<Reference Include="QRCoder, Version=1.3.5.0, Culture=neutral, processorArchitecture=MSIL">
    <HintPath>$(USERPROFILE)\.nuget\shared_packages\QRCoder.1.3.5\lib\net40\QRCoder.dll</HintPath>
</Reference>

Can I configure nuget.config (or something else?) to make the Hint Path use the literal value of $(USERPROFILE)\\.nuget\\shared_packages so I don't have to manually edit the .csproj file every time?我可以配置nuget.config (或其他东西?)以使提示路径使用$(USERPROFILE)\\.nuget\\shared_packages的文字值,这样我就不必每次都手动编辑.csproj文件吗?

How to use variables in nuget.config repositoryPath and csproj Reference HintPath如何在 nuget.config repositoryPath 和 csproj Reference HintPath 中使用变量

Just like Matt said, " NuGet always uses relative paths for references if you are using a packages.config file. ".正如 Matt 所说,“如果您使用的是 packages.config 文件NuGet 总是使用相对路径作为引用。 ”。 And, AFAIK, there is no such configurein nuget.config (or something else?) to make the Hint Path use the literal value of $(USERPROFILE)\\.nuget\\shared_packages .而且,AFAIK, nuget.config (或其他东西?)中没有这样的配置来使提示路径使用$(USERPROFILE)\\.nuget\\shared_packages的文字值。

To resolve this question, you can use Package references ( PackageReference ) instead of packages.config in project files.要解决此问题,您可以在项目文件中使用包引用 ( PackageReference ) 而不是 packages.config 。 With PackageReference , your packages are point to the shared package folder (similar to how NuGet does it with .NET Core projects), even if you are using the .NET framework.使用PackageReference ,您的包指向共享包文件夹(类似于 NuGet 对.NET Core项目的处理方式),即使您使用的是 .NET 框架。

To use the PackageReference , go to the Tools->NuGet PackageManager->Package Manage Settings:要使用PackageReference ,请转到 Tools->NuGet PackageManager->Package Manage Settings:

在此处输入图片说明

Then you can add your packages to your project, all the packages are stored in the C:\\Users\\<UserName>\\.nuget\\packages .然后你可以将你的包添加到你的项目中,所有的包都存储在C:\\Users\\<UserName>\\.nuget\\packages

Besides, you can Migrate from packages.config to PackageReference for those projects which install the packages with packages.config .此外,对于那些使用packages.config安装包的项目,您可以从 packages.config 迁移到 PackageReference

Hope this helps.希望这可以帮助。

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

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