简体   繁体   English

Visual Studio 中的 Nuget package 内容

[英]Nuget package content in Visual Studio

I created a small project in Visual Studio 2019 that uses the content of a NuGet package (SQLite).我在 Visual Studio 2019 中创建了一个小项目,该项目使用 NuGet package (SQLite) 的内容。 When I build my project in Visual Studio and run it in debug mode, everything works fine.当我在 Visual Studio 中构建我的项目并在调试模式下运行它时,一切正常。 But when I copy the built files from the output directory to another directory or to another computer it fails, telling me, that it's not finding files from the NuGet package.但是,当我将构建的文件从 output 目录复制到另一个目录或另一台计算机时,它会失败,告诉我,它没有从 NuGet ZEFE90A8E604A7C840E88D03A67F6B7D8 中找到文件。

For the NuGet package I'm using it's missing two files.对于 NuGet package 我使用它缺少两个文件。 When I'm copying these two missing files from the NuGet package directory to my project files, it's working again.当我将这两个丢失的文件从 NuGet package 目录复制到我的项目文件时,它又可以工作了。

But is this really the way to go?但这真的是通往 go 的路吗? Let's say my project is getting bigger and is needing more and more NuGet packages, do I really have to guess for every NuGet package, which files I might need from it and where I might find these files on my hard drive?假设我的项目越来越大,并且需要越来越多的 NuGet 包,我真的必须为每个 NuGet package 猜测,我可能需要从我的硬盘驱动器上找到哪些文件?

Another thing is that I found the files from the NuGet package somewhere in my Windows user directory, not in my code directory.另一件事是我在我的 Windows 用户目录的某个地方找到了 NuGet package 中的文件,而不是在我的代码目录中。 So, if I'll archive my code and give it to someone else or use it some time later, the NuGet packages are missing from the code.因此,如果我将我的代码存档并将其提供给其他人或稍后使用它,则代码中缺少 NuGet 包。

So, here are my questions:所以,这是我的问题:

1) Is there a way to tell Visual Studio to copy all the necessary files from all used NuGet packages to the output directory automatically? 1)有没有办法告诉Visual Studio自动将所有使用的NuGet包中的所有必要文件复制到output目录?

2) Is it possible to store the downloaded NuGet packages somewhere closer to my source code instead of in my windows user directory? 2) 是否可以将下载的 NuGet 包存储在更靠近我的源代码的位置,而不是在我的 windows 用户目录中? I'd like to just archive one directory to backup all of my source code.我只想归档一个目录来备份我的所有源代码。

I took a quick look on post build events to maybe copy all the missing files to the output directory in a post build event, but that's can't be the way to go, since I still need the knowledge exactly which files I need to copy and where to find them, and the path to said files might change from computer to computer.我快速查看了构建后事件,可能会在构建后事件中将所有丢失的文件复制到 output 目录,但这不是 go 的方法,因为我仍然需要确切知道我需要复制哪些文件以及在哪里可以找到它们,以及所述文件的路径可能会因计算机而异。

I think you are using PackageReference Nuget Mangement format to manage your nuget packages in your projects.我认为您正在使用PackageReference Nuget 管理格式来管理项目中的 nuget 包。

PackageReference nuget format directly references the nuget from the local nuget cache and will not copy the nuget packages into your project. PackageReference nuget format directly references the nuget from the local nuget cache and will not copy the nuget packages into your project. It seems like Link reference .好像是链接参考 It is the feature of PackageReference .这是PackageReference的特性。

When you move your project into another agent and then want to use its content, you should make sure that the nuget package is still in your current agent environment.当您将项目移动到另一个代理然后想要使用其内容时,您应该确保 nuget package 仍在您当前的代理环境中。

So, first of all , you should do a nuget restore .所以,首先,你应该做一个 nuget 恢复 And then you can get the missing packages back on your agent.然后您可以将丢失的包裹找回给您的代理。

Right-click on your solution on the Solution Explorer --> Restore Nuget Packages解决方案资源管理器中右键单击您的解决方案 --> Restore Nuget Packages

Is there a way to tell Visual Studio to copy all the necessary files from all used NuGet packages to the output directory automatically?有没有办法告诉 Visual Studio 自动将所有使用的 NuGet 包中的所有必要文件复制到 output 目录?

Actually , previously, there was a problem with packagereference that from time to time, the content in the nuget package was lost and could not be completely copied to the output path.其实之前packagereference有一个问题,时不时会出现nuget package中的内容丢失,无法完全复制到Z78E6221F6393D1356681DB398F14CEDZ路径下。 See this .看到这个

So you could try to add this in your xxx.csproj file:因此,您可以尝试将其添加到您的xxx.csproj文件中:

<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

Is it possible to store the downloaded NuGet packages somewhere closer to my source code instead of in my windows user directory?是否可以将下载的 NuGet 包存储在更靠近我的源代码的位置,而不是在我的 windows 用户目录中? I'd like to just archive one directory to backup all of my source code.我只想归档一个目录来备份我的所有源代码。

Windows user directory is nuget global caches and it will download all the packages in such folder in your agent and then projects references the nuget package from there. Windows user directory is nuget global caches and it will download all the packages in such folder in your agent and then projects references the nuget package from there.

And also note that :还要注意

It works according to the current user rather than the current machine.它根据当前用户而不是当前机器工作。 It is designed by that.它就是这样设计的。 All nuget packages are stored in the current user-level directory.所有 nuget 包都存储在当前用户级目录中。 Switching OS accounts cannot get the nuget packages in the other user directory.切换OS账号无法获取其他用户目录下的nuget包。

And if you want to modify its path, you should modify the Nuget.Config ( C:\Users\xxx(use r name)\AppData\Roaming\NuGet\NuGet.Config )file:如果你想修改它的路径,你应该修改Nuget.Config ( C:\Users\xxx(use r name)\AppData\Roaming\NuGet\NuGet.Config )

add these node:添加这些节点:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
....
<config>

    <add key="globalPackagesFolder" value="xxxxx(the custom path)" />
</config>
....

</configuration>

And then restart VS and nuget packages which you install later will be installed in such package.然后重启 VS 和 nuget 包,你以后安装的包会安装在这样的 package 中。

Update 1更新 1

First, please check your agent and make sure that the Internet can access fast.首先,请检查您的代理并确保互联网可以快速访问。

For that paid nuget packages, you should do other operation and then you can install it from the nuget server.对于付费的 nuget 包,你应该做其他操作,然后你可以从 nuget 服务器安装它。

If the nuget package needs to be paid or be deleted from the nuget server, restore operation will not find the package or need your further extra step. If the nuget package needs to be paid or be deleted from the nuget server, restore operation will not find the package or need your further extra step. And then restore operation will succeed.然后恢复操作就会成功。

For those old abandoned nuget package, it exists under your old agent's global nuget cache , so in your old agent, you can find it.对于那些旧的废弃nuget package,它存在于你的旧代理的全局nuget缓存下,所以在你的旧代理中,你可以找到它。

But when you migrate your project to the new agent, since the nuget package is removed from the nuget server, so the new agent will not find it and cannot restore such package. But when you migrate your project to the new agent, since the nuget package is removed from the nuget server, so the new agent will not find it and cannot restore such package. If you still have the old nuget package in your old agent, you can move to the new environment and then need to configure its new address to the nuget package source , and then restore operation will succeed. If you still have the old nuget package in your old agent, you can move to the new environment and then need to configure its new address to the nuget package source , and then restore operation will succeed.

After all, you should make sure that every nuget package can be found and have no extra requirements on nuget.org server or do some operation to them since your project is too old and the nuget packages have had big changes. After all, you should make sure that every nuget package can be found and have no extra requirements on nuget.org server or do some operation to them since your project is too old and the nuget packages have had big changes. You should check them carefully.你应该仔细检查它们。

Now I was able to take a look at Perry's comments and answers, but I have to say... NuGet is not even close to being convenient to use or close to work as one would expect it to.现在我可以看看佩里的评论和答案,但我不得不说...... NuGet 甚至不像人们期望的那样方便使用或接近工作。

Adding添加

<PropertyGroup>
    <CopyLocalLockFileAssemblies>True</CopyLocalLockFileAssemblies>
</PropertyGroup>

to my project file did indeed copy the referenced file from the NuGet package (SQLite) to the build output folder.到我的项目文件确实将引用的文件从 NuGet package (SQLite) 复制到构建 output 文件夹。 One of the necessary files was missing, but I guess that is a problem of SQLite, not NuGet (I do not directly reference this file, but if it is missing, an error message tells me so).其中一个必要的文件丢失了,但我猜这是 SQLite 的问题,而不是 NuGet(我不直接引用这个文件,但如果它丢失了,一条错误消息会告诉我)。

But I was not able to get the NuGet packages to be stored closer to the source code.但我无法将 NuGet 包存储在更靠近源代码的位置。 I know, Nuget is able to do so, because the NuGet packages for NUnit are stored in a folder called "packages" right inside the project folder.我知道,Nuget 能够这样做,因为 NUnit 的 NuGet 包存储在项目文件夹内一个名为“包”的文件夹中。

But adding但添加

<config>
    <add key="globalPackagesFolder" value="xxxxx(the custom path)" />
</config>

to the NuGet.config file (like suggested), re-starting Visual Studio and re-building my project did add a lot of packages to my custom path (130 MB), but not the only package I'm referencing on purpose (SQLite).到 NuGet.config 文件(如建议的那样),重新启动 Visual Studio 并重新构建我的项目确实向我的自定义路径(130 MB)添加了很多包,但不是唯一的 package 我故意引用(SQLite )。

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

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