用途
该技能用于管理 .NET 10 运行时、C# 13 语法以及项目配置,适用于配置项目、使用现代 C# 特性、设置服务默认项等场景。资料说明该代码库使用 .NET 10(LTS)与 C# 13,并启用了严格的可空引用类型、隐式 using 和 XML 文档生成。所有服务遵循相同的项目配置模式,并通过 Sorcha.ServiceDefaults 共享基础设施。
主要能力
- 项目配置:以 Microsoft.NET.Sdk.Web 为目标,设置 net10.0、ImplicitUsings、Nullable、GenerateDocumentationFile,并通过 NoWarn 抑制 CS1591。
- 服务默认项设置:在构建器中先调用 AddServiceDefaults,再注册服务特定依赖,添加来自 ServiceDefaults 的 JWT 认证,最后映射默认端点并启用认证与授权。
- 现代 C# 特性与常见模式:包括主构造函数(用于带 DI 的服务)、集合表达式(默认值)、required 成员(DTO 与契约)、record(值对象与 DTO)、原始字符串字面量(多行文档)。
- 全局 using:通过 GlobalUsings.cs 统一引入 System 系列命名空间以及项目特定命名空间。
- 测试项目配置:在 ItemGroup 中引入 Xunit、Moq、FluentAssertions。
- 文档资源获取:通过 Context7 获取最新 .NET 文档,使用 mcp__context7__resolve-library-id 搜索 dotnet,优先选择以 /websites/ 开头的网站文档,再用 mcp__context7__query-docs 查询;资料给出库 ID 为 /websites/learn_microsoft_en-us_dotnet,并列出推荐查询,如“C# 13 new features primary constructors”“collection expressions syntax”“required members properties”“nullable reference types”。
使用方式
项目配置文件示例:
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<NoWarn>$(NoWarn);CS1591</NoWarn>
</PropertyGroup>
</Project>
服务设置模式示例:
var builder = WebApplication.CreateBuilder(args);
// Always call AddServiceDefaults first
builder.AddServiceDefaults();
// Add service-specific dependencies
builder.Services.AddScoped<IMyService, MyService>();
// Add JWT authentication (shared from ServiceDefaults)
builder.AddJwtAuthentication();
var app = builder.Build();
app.MapDefaultEndpoints();
app.UseAuthentication();
app.UseAuthorization();
全局 using 示例:
// GlobalUsings.cs
global using System;
global using System.Collections.Generic;
global using System.Linq;
global using System.Threading;
global using System.Threading.Tasks;
global using Sorcha.MyProject.Domain; // Project-specific
测试项目配置示例:
<ItemGroup>
<Using Include="Xunit" />
<Using Include="Moq" />
<Using Include="FluentAssertions" />
</ItemGroup>
适用场景
- 为使用 .NET 10 与 C# 13 的项目进行统一配置。
- 在服务中遵循统一的项目配置模式并共享 Sorcha.ServiceDefaults 基础设施。
- 应用现代 C# 语法与代码模式,如主构造函数、集合表达式、required 成员、record、原始字符串字面量。
- 按统一模式设置认证、依赖注入与端点映射。
- 配置单元测试项目并引入常用断言与 mock 库。
相关技能
资料列出相关技能包括:aspire(.NET Aspire 编排与服务默认项)、minimal-apis(使用 MapGet/MapPost 配置端点)、xunit(测试项目配置与模式)、entity-framework(EF Core 与仓储集成)。
限制
资料未说明该技能支持的具体版本兼容范围、支持的运行平台、收费方式与价格,也未提供完整使用步骤的更多细节,这些信息暂未提供。