using System.Security.Cryptography;
using System.Text;
using Dpz.Core.Infrastructure;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
namespace Dpz.Core.AspNetCore;
public static class AspNetCoreServiceCollectionExtensions
{
public static IServiceCollection AddDpzAspNetCoreServices(this IServiceCollection services)
{
services.AddHttpContextAccessor();
services.AddScoped<IHttpCurrentUserService, HttpCurrentUserService>();
return services;
}
/// <summary>
/// 根据工作区根路径生成稳定的 UUID,让 DevTools 重启后仍能识别同一个工作区
/// </summary>
private static string CreateChromeDevToolsWorkspaceUuid()
{
var hash = SHA1.HashData(Encoding.UTF8.GetBytes(AppPath.Value));
var bytes = hash.AsSpan(0, 16).ToArray();
bytes[6] = (byte)((bytes[6] & 0x0F) | 0x50);
bytes[8] = (byte)((bytes[8] & 0x3F) | 0x80);
return new Guid(bytes).ToString();
}
private static readonly Lazy<string> AppPath = new(Directory.GetCurrentDirectory);
public static void UseDevToolsEndpoint(this WebApplication app)
{
var root = app.Configuration.IsDevelopment ? AppPath.Value : null;
var uuid = app.Configuration.IsDevelopment ? CreateChromeDevToolsWorkspaceUuid() : null;
app.MapGet(
"/.well-known/appspecific/com.chrome.devtools.json",
async context =>
{
await context.Response.WriteAsJsonAsync(new { workspace = new { root, uuid } });
}
);
}
public static void UseVersionEndpoint(this WebApplication app)
{
app.MapGet("/api/version", () => ApplicationTools.ApplicationVersion.Value);
}
}
⚠⚠ 以下内容为AI分析的结果,请根据实际情况进行判断。
代码解释
这是一个 ASP.NET Core 的扩展类库文件,提供了一组用于服务注册和中间件配置的扩展方法。
主要功能模块
1. 服务注册扩展方法 (AddDpzAspNetCoreServices)
public static IServiceCollection AddDpzAspNetCoreServices(this IServiceCollection services)
- 注册
HttpContextAccessor:用于在非控制器类中访问 HTTP 上下文 - 注册
IHttpCurrentUserService服务(作用域生命周期):实现类为HttpCurrentUserService,用于获取当前用户信息 - 返回服务集合以支持链式调用
2. Chrome DevTools 工作区 UUID 生成 (CreateChromeDevToolsWorkspaceUuid)
private static string CreateChromeDevToolsWorkspaceUuid()
用途:为 Chrome DevTools 生成一个稳定的工作区标识符
实现逻辑:
- 使用 SHA1 对应用根路径进行哈希运算
- 取前 16 字节作为 UUID 的基础数据
- 按照 UUID v5 规范修改特定字节:
bytes[6]:设置版本号为 5(0x50)bytes[8]:设置变体标识(0x80)
- 生成 GUID 字符串
这样即使应用重启,相同的工作区路径也会生成相同的 UUID。
3. DevTools 端点配置 (UseDevToolsEndpoint)
public static void UseDevToolsEndpoint(this WebApplication app)
- 只在开发环境下启用(通过
IsDevelopment判断) - 注册一个符合 Chrome DevTools 规范的端点:
/.well-known/appspecific/com.chrome.devtools.json - 返回 JSON 格式的工作区信息:
{ "workspace": { "root": "应用根路径", "uuid": "生成的UUID" } }
4. 版本信息端点 (UseVersionEndpoint)
public static void UseVersionEndpoint(this WebApplication app)
- 注册
/api/version端点 - 返回应用程序版本信息(从
ApplicationTools.ApplicationVersion.Value获取)
5. 辅助字段
private static readonly Lazy<string> AppPath
- 使用懒加载方式缓存应用根目录路径
- 线程安全,只在首次访问时计算
典型使用场景
// Program.cs 中
var builder = WebApplication.CreateBuilder(args);
// 注册服务
builder.Services.AddDpzAspNetCoreServices();
var app = builder.Build();
// 配置中间件
app.UseDevToolsEndpoint(); // 开发环境下启用 DevTools 集成
app.UseVersionEndpoint(); // 提供版本查询接口
app.Run();
这个工具类主要用于简化 ASP.NET Core 应用的配置,特别是开发工具集成和版本管理方面。
AI 正在分析代码…
评论加载中...