QYQ.Base.Common 8.6.2

dotnet add package QYQ.Base.Common --version 8.6.2
                    
NuGet\Install-Package QYQ.Base.Common -Version 8.6.2
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="QYQ.Base.Common" Version="8.6.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="QYQ.Base.Common" Version="8.6.2" />
                    
Directory.Packages.props
<PackageReference Include="QYQ.Base.Common" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add QYQ.Base.Common --version 8.6.2
                    
#r "nuget: QYQ.Base.Common, 8.6.2"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package QYQ.Base.Common@8.6.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=QYQ.Base.Common&version=8.6.2
                    
Install as a Cake Addin
#tool nuget:?package=QYQ.Base.Common&version=8.6.2
                    
Install as a Cake Tool

公共组件

QYQ.Base.Common 是一个面向 ASP.NET Core 8 的公共组件库,封装常用的中间件、认证、配置与依赖注入扩展,帮助快速搭建统一的 Web API 基础设施。

功能概览

  • 统一返回格式:内置 ApiResult<T>ApiResultFilter,保证接口响应字段一致。
  • 全局异常处理UseErrorHandling 捕获未处理异常并输出标准化错误。
  • HTTP 请求日志UseQYQHttpLogging 结合 QYQHttpLoggingOptions 控制忽略路径、请求/响应体大小与慢查询阈值。
  • JWT 身份验证AddQYQAuthentication 读取 jwt 节点自动配置 JwtBearer
  • Serilog 集成AddQYQSerilog 读取配置文件完成 Serilog 注册并附加机器名、上下文信息。
  • Apollo 配置AddQYQApollo 根据 apollo 节点装配配置中心,支持通过 Cluster 覆盖集群。
  • 批量依赖注入AddMultipleService 基于 IDependency 标记接口按生命周期批量注册,实现约定优于配置。

安装

在目标项目目录执行:

dotnet add package QYQ.Base.Common

或者在 .csproj 中添加引用:

<PackageReference Include="QYQ.Base.Common" Version="8.6.1" />

环境要求

  • .NET 8.0 及以上。
  • 若使用 Apollo,需要在配置文件中提供 apollo 节点并可选指定 Cluster
  • Serilog、JWT 等功能依赖对应配置节点,缺失时会抛出异常或使用默认值。

配置示例

appsettings.json 中准备与组件对应的配置:

{
  "jwt": {
    "Issuer": "your-issuer",
    "Audience": "your-audience",
    "SecretKey": "your-secret-key-should-be-longer-than-16",
    "AccessExpiration": 60,
    "RefreshExpiration": 1440
  },
  "Serilog": {
    "Using": ["Serilog.Sinks.Console"],
    "MinimumLevel": "Information",
    "WriteTo": [ { "Name": "Console" } ]
  },
  "QYQHttpLoggingOptions": {
    "IgnorePath": ["/api/Health", "/swagger"],
    "MaxRequestBodySize": 1048576,
    "MaxResponseLenght": 1048576,
    "LongQueryThreshold": 100
  },
  "apollo": {
    "AppId": "your-app-id",
    "Env": "DEV",
    "Meta": { "DEV": "http://your.apollo.meta" }
  }
}

快速上手

1. 注册服务与中间件

var builder = WebApplication.CreateBuilder(args);

builder.Configuration.AddQYQApollo();
builder.Services.AddQYQAuthentication(builder.Configuration);
builder.Services.AddQYQSerilog(builder.Configuration);
builder.Services.AddQYQHttpLogging(options =>
{
    options.IgnorePath.Add("/health");
    options.LongQueryThreshold = 200;
});
builder.Services.AddMultipleService("Your.Assembly.Pattern");
builder.Services.AddControllers(options =>
{
    options.Filters.Add<ApiResultFilter>();
});

var app = builder.Build();

app.UseErrorHandling();
app.UseAuthentication();
app.UseAuthorization();
app.UseQYQHttpLogging();

app.MapControllers();
app.Run();

2. ApiResult 统一返回格式

ApiResult<T> 提供标准化的接口返回模型,包含 CodeMessageData 三个字段。配合 QYQBaseController 可快速构建统一响应:

[ApiController]
[Route("api/[controller]")]
public class DemoController : QYQBaseController
{
    [HttpGet("hello")]
    public ApiResult<string> Hello()
    {
        return Json(ApiResultCode.Success, "world");
    }
}

如果启用 ApiResultFilter,模型验证错误会自动转换为统一结构的响应,便于前端处理。

3. Apollo 配置扩展

AddQYQApollo 会读取 apollo 节点的 MetaEnv 信息构造配置中心地址,并允许通过 Cluster 覆盖集群名称。

4. JWT 鉴权扩展

AddQYQAuthentication 依赖 jwt 节点参数(发行方、受众、密钥与过期时间)自动装配 JwtBearer,并将参数注入 IOptions<JwtSettings> 供业务使用。

5. Serilog 与 HTTP 日志

AddQYQSerilog 直接读取 Serilog 配置,附加机器名与上下文信息;UseQYQHttpLogging 会记录请求方法、路径、耗时以及请求/响应体,且可通过 QYQHttpLoggingOptions 控制忽略路径与大小阈值。

6. 批量依赖注入

AddMultipleService 会扫描匹配到的程序集,查找实现 IDependency 族接口的类型,并根据 ISingletonDependencyIScopeDependency 或默认瞬时生命周期完成批量注册,减少手动配置。


如需更多示例或详细实现,请查看源码。若有问题,欢迎通过仓库 Issue 反馈。

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
8.6.2 150 1/6/2026
8.6.1 804 12/3/2025
8.6.0 492 11/20/2025
8.5.9 647 8/29/2025
8.5.8 251 8/26/2025
8.5.7 193 8/18/2025
8.5.6 1,009 4/24/2025
8.5.5 270 4/8/2025
8.5.4 227 4/8/2025
8.5.3 241 4/8/2025
8.5.2 171 4/6/2025
8.5.1.2 173 4/5/2025
Loading failed