C# 项目的依赖设置

不同版本的C#, 其项目依赖设置有不同的写法.

下面内容摘自 https://fossa.com/blog/managing-dependencies-net-csproj-packagesconfig/

.NET Framework pre-NuGet (before 2010)

Prior to the advent of .NET Core, a project was represented by a .csproj file, and all the dependencies were represented in the same file. If you wanted to reference a third-party library, you had to find it on the web, download it, place it in a folder, and add a project reference to it in the .csproj file.

.NET Framework with NuGet (2010 - 2016)

A project was represented by a .csproj file but the dependencies (including NuGet package references) were kept separately in a packages.config file. Here, you could get your third-party libraries from the NuGet central public collection and transfer your project to another machine without copying the libraries — simply copy the packages.config file and do a package restore.

Initial .NET Core (2015 - 2017)

The first .NET Core projects created in Visual Studio 2015 were represented by an .xproj file (similar to .csproj) and the dependencies (NuGet package references) stored in a project.json file. This approach is now deprecated.

.NET Core is born (2017 - today)

Since Visual Studio 2017, a project has been represented by a **.csproj ** file with dependencies (NuGet package references) listed in the **PackageReference ** section of this file. For some types of projects, dependencies are still kept in that separate packages.config file.

Disable nuget restore when build

nuget restore 会自动解析项目配置文件的依赖项, 并在需要的时候从nuget仓库下载依赖程序文件, 这个restore会在build/publish/run/test/new/pack时自动触发的, 但这会拉慢编译速度. 可以在项目根目录增加一个 Nuget.config 文件, 在文件中禁用自动 nuget restore.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <!-- Opts out of both Automatic Package Restore and MSBuild-Integrated Package Restore -->
    <add key="enabled" value="False" />

    <!-- Opts out of Automatic Package Restore in Visual Studio -->
    <add key="automatic" value="False" />
  </packageRestore>
</configuration>

手动执行restore命令为:

# restore 当前目录中的项目
dotnet restore

#restore 指定项目
dotnet restore ./projects/app1/app1.csproj
posted @   harrychinese  阅读(36)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示