问题
If I have a library project MyLib
which requires Nuget XYZ
, and I create application MyApp
which has a dependency on MyLib
, am I required to add the nuget dependencies explicitly to MyApp
or is VS supposed to figure this out?
I couldn't find a definitive answer to this basic question, and my tests are showing inconclusive results - I see some DLLs get copied to the /bin folder and others semingly don't. In my project references, the Nuget assemblies are not listed and I'm not sure if they should be or not... in a bit of a "DLL hell haze" now!
回答1:
From memory of working on a complex C#.NET app - yes I believe it is necessary to add the dependencies.
The application I developed had a structure like this:
Server --> Plugin 1 --> MongoDB NuGet --> Plugin 2 --> MongoDB NuGet
I found it was necessary to add the MongoDB NuGet reference to each of the plugins, and also to the server component. Once I did this it worked reliably (though it did have the downside that for each update to the NuGet library, I needed to update each plugin individually).
Happy to be corrected if there is a neater solution, just wanted to share my experience!
回答2:
Should nugets installed for library project also be installed to the application project?
This is up to you. Of course, adding the nuget package to the main project is not a trouble, it will not cause any problems, it is simple and convenient, but when you pay attention to the following points, there is no need to add the nuget package in addition.
Before all of these, there are two ways to reference nuget packages: packages.config format and PackageReference format.
Packages.config
format uses for the old net framework
projects, while PackageReference
format uses for new sdk style projects(net core
and net standard
).
However net framework projects can also use PackageReference(you can switch by Tools
-->Options
-->Nuget Package Manager
-->General
-->Package Management
), net core and net standard projects cannot use Packages.config
format.
Suggestion
1) If the library project is Net framework
, actually, you should use add the same nuget into the main project, in some cases, these dlls can be copied in your MyApp project's output path,
however, if you still want to use these dlls in code editor, the intellisense will not list it and in this situation, you should install the same nuget package in the main project.
Further, only these two projects are both using PackageReference format(Net Framework
library project with PackageReference and MyApp Net Framework
project with PackageReference)
2) If your library project is Net Standard
(PacakgesReference nuget format), and your MyApp project is Net Framework
with packages.config
, the dlls will miss in the main project. The workaround is to add these node in MyApp.csproj file:
<PropertyGroup>
<RestoreProjectStyle>PackageReference</RestoreProjectStyle>
</PropertyGroup>
Then you will get the intellisense in the main project.
-----------------------------------
Or your Myapp project is net framework with PackageReference format and you should not add that node, you can obtain what you want directly.
If MyApp project is Net Core(PackageReference), you do not have to worry about this.
================================
Conclusion
To meet your requirements, you would better use a Net Standard library project, and your MyApp project is best to use PackageReference nuget management format(Net Core
or Net Framework
with PackageReference format).
In addition, you should update your VS to the latest version for better experience in case there are some important fixes. Also, there is a similar issue you can refer to.
来源:https://stackoverflow.com/questions/62677931/should-nugets-installed-for-library-project-also-be-installed-to-the-application