dotnet 是入口

C# 项目几乎所有操作通过 dotnet CLI:

dotnet --help           # 看所有命令
dotnet new --list       # 看模板

创建项目

dotnet new console -n HelloApp
dotnet new web -n MyApi
dotnet new classlib -n MyLib
dotnet new xunit -n MyTests
dotnet new sln -n MySolution

-n 指定名字 + 目录。

Solution(多项目集合)

mkdir Sample && cd Sample
dotnet new sln -n Sample

dotnet new web -n Sample.Api
dotnet new classlib -n Sample.Core
dotnet new xunit -n Sample.Tests

dotnet sln add Sample.Api Sample.Core Sample.Tests

# 项目间引用
dotnet add Sample.Api reference Sample.Core
dotnet add Sample.Tests reference Sample.Core

最终结构:

Sample/
├── Sample.sln
├── Sample.Api/
├── Sample.Core/
└── Sample.Tests/

还原依赖

dotnet restore

dotnet build / dotnet run 之前会自动 restore——通常不用手跑。

构建

dotnet build                    # Debug
dotnet build -c Release         # Release(优化)
dotnet build --no-restore       # 跳过 restore

输出到 bin/Debug/net8.0/

dotnet run                      # 编译 + 跑
dotnet run -c Release
dotnet run --project Sample.Api
dotnet run -- arg1 arg2         # -- 后面是程序参数

测试

dotnet test                     # 跑所有测试
dotnet test --filter "Name~Login"   # 名字含 Login 的
dotnet test --logger "console;verbosity=detailed"
dotnet test --collect:"XPlat Code Coverage"   # 覆盖率

发布

dotnet publish -c Release -o ./out

# 自包含(带运行时,~70MB)
dotnet publish -c Release --self-contained -r linux-x64

# 单文件(一个 exe)
dotnet publish -c Release --self-contained -r win-x64 -p:PublishSingleFile=true

# AOT(提前编译成原生码,启动快、不要 JIT)
dotnet publish -c Release -p:PublishAot=true

详见 第 20 篇 roadmap 部署部分。

NuGet 包

dotnet add package Newtonsoft.Json
dotnet add package Serilog --version 3.1.1
dotnet add package Microsoft.EntityFrameworkCore --prerelease
dotnet remove package PackageName
dotnet list package                            # 已装包
dotnet list package --outdated                 # 看升级
dotnet list package --vulnerable               # 安全漏洞

全局工具

dotnet tool install -g dotnet-ef               # EF Core 命令工具
dotnet tool install -g dotnet-script           # REPL
dotnet tool install -g csharp-ls               # 语言服务器(手装时)
dotnet tool list -g                            # 看已装
dotnet tool update -g <name>

项目本地工具(团队共享):

dotnet new tool-manifest                       # 创建 .config/dotnet-tools.json
dotnet tool install dotnet-ef --local
# 队友克隆后:
dotnet tool restore

模板自定义

dotnet new 的"项目级"配置 .template.json——少用,但能自家做内部模板。dotnet new install 装自家模板。

.csproj 常改的字段

<PropertyGroup>
    <TargetFramework>net8.0</TargetFramework>          <!-- 改运行时版本 -->
    <LangVersion>12</LangVersion>                       <!-- 锁 C# 版本 -->
    <Nullable>enable</Nullable>                         <!-- 可空引用类型 -->
    <TreatWarningsAsErrors>true</TreatWarningsAsErrors> <!-- 警告即错误 -->
    <RootNamespace>MyApp</RootNamespace>
    <AssemblyName>myapp</AssemblyName>
    <Version>1.0.0</Version>
</PropertyGroup>

<ItemGroup>
    <PackageReference Include="X" Version="1.0" />
    <ProjectReference Include="..\Other\Other.csproj" />
    <None Update="config.json"><CopyToOutputDirectory>Always</CopyToOutputDirectory></None>
</ItemGroup>

常用一句话命令

dotnet format              # 格式化(按 .editorconfig)
dotnet watch run           # 文件变化时自动重启
dotnet ef migrations add Init   # EF Core 加迁移
dotnet new gitignore       # .gitignore

工程惯例

  • 项目结构:<RootNs>.<SubProject> 命名(Sample.Core / Sample.Api / Sample.Tests)
  • .editorconfig 放仓库根目录,统一缩进 / 命名
  • Directory.Build.props 多项目共享 csproj 属性
  • nuget.config 仓库根,私有 NuGet 源配置

→ 下一篇 值类型 vs 引用类型