docs/PerformanceOptimizationSummary-CN.md
您报告的问题:应用程序在使用 WeiXinMPSDK 时出现偶发的 API 响应超时(>5秒),通过 Nginx 日志确认确实存在响应缓慢的情况。
Your reported issue: The application experiences intermittent API response timeouts (>5 seconds) when using WeiXinMPSDK, confirmed by Nginx logs showing slow responses.
经过深入分析,我们发现了两个关键的性能瓶颈:
After deep analysis, we identified two critical performance bottlenecks:
问题详情 / Problem Details:
Register() 方法使用 Task.WaitAll() 阻塞线程长达 10 秒Register() methods used Task.WaitAll() blocking threads for up to 10 secondsRegisterWxOpenAccount() 和 RegisterMpAccount() 都会触发此问题RegisterWxOpenAccount() and RegisterMpAccount() both triggered this issue// 您的代码 / Your code:
weixinRegister.RegisterWxOpenAccount(senparcWeixinSetting.Value, "助手");
weixinRegister.RegisterMpAccount(senparcWeixinSetting.Value.Items["通知公众号"]);
影响 / Impact:
问题详情 / Problem Details:
AddSenparcWeixin() 在 DI 注册阶段立即构建 ServiceProviderAddSenparcWeixin() immediately built ServiceProvider during DI registration影响 / Impact:
变更说明 / Change Description:
将所有 Register() 方法从阻塞模式改为 fire-and-forget 模式
Changed all Register() methods from blocking mode to fire-and-forget pattern
// 之前 / Before (会阻塞 10 秒 / blocks for 10s)
public static void Register(string appId, string appSecret, string name = null)
{
var task = RegisterAsync(appId, appSecret, name);
Task.WaitAll(new[] { task }, 10000); // ❌ 阻塞
}
// 之后 / After (不阻塞 / non-blocking)
public static void Register(string appId, string appSecret, string name = null)
{
_ = Task.Run(async () =>
{
try
{
await RegisterAsync(appId, appSecret, name).ConfigureAwait(false);
}
catch (Exception ex)
{
Senparc.CO2NET.Trace.SenparcTrace.SendCustomLog("注册出错", ex.Message);
}
});
}
受影响的容器 / Affected Containers:
变更说明 / Change Description: 直接从 IConfiguration 读取配置,避免构建 ServiceProvider
Read configuration directly from IConfiguration without building ServiceProvider
// 之前 / Before
using (var scope = services.BuildServiceProvider().CreateScope()) // ❌ 昂贵操作
{
var tenPayV3Setting = scope.ServiceProvider.GetService<...>();
...
}
// 之后 / After
var weixinSettingSection = configuration.GetSection("SenparcWeixinSetting"); // ✅ 直接读取
var tenPayV3Section = weixinSettingSection.GetSection("TenpayV3Setting");
...
# 升级到最新版本(包含此修复)
# Upgrade to latest version (includes this fix)
dotnet add package Senparc.Weixin.MP --version 16.21.0
dotnet add package Senparc.Weixin.WxOpen --version 16.21.0
✅ 好消息!您的代码无需任何修改。
✅ Good news! Your code requires no changes.
您现有的注册代码将继续正常工作,但性能会显著提升:
Your existing registration code will continue to work, but with significantly improved performance:
// 这段代码不需要修改,性能已自动优化 / This code needs no changes, performance is automatically improved
app.UseSenparcGlobal(env, senparcSetting.Value, globalRegister =>
{
}, true)
.UseSenparcWeixin(senparcWeixinSetting.Value, (weixinRegister, setting) =>
{
weixinRegister.RegisterWxOpenAccount(senparcWeixinSetting.Value, "助手");
weixinRegister.RegisterMpAccount(senparcWeixinSetting.Value.Items["通知公众号"]);
});
虽然不是必需的,但我们建议长期逐步迁移到异步 API:
While not required, we recommend gradually migrating to async APIs long-term:
// 推荐的异步方式 / Recommended async approach
await AccessTokenContainer.RegisterAsync(appId, appSecret, name);
升级后,建议监控以下指标确认改进效果:
After upgrading, we recommend monitoring these metrics to confirm improvements:
应用启动时间 / Application Startup Time
API 响应时间 / API Response Time
错误日志 / Error Logs
如果您有多个应用实例,建议配置 Redis 缓存:
If you have multiple application instances, we recommend configuring Redis cache:
services.AddSenparcGlobalServices(configuration)
.UseSenparcRedisCache(options =>
{
options.Configuration = "your-redis-connection-string";
});
正如维护者 @JeffreySu 提到的,如果部署在阿里云,配置腾讯云 DNS 可以提升稳定性:
As maintainer @JeffreySu mentioned, if deployed on Alibaba Cloud, configuring Tencent Cloud DNS can improve stability:
确保健康检查端点不依赖微信 API:
Ensure health check endpoints don't depend on WeChat APIs:
app.MapHealthChecks("/healthz"); // ✅ 这个应该很快 / This should be fast
如果升级后仍有问题,请提供:
If you still have issues after upgrading, please provide:
✅ 已完成 / Completed:
✅ 您需要做的 / What You Need:
我们相信这些优化将彻底解决您报告的性能问题!🎉
We're confident these optimizations will completely resolve your reported performance issues! 🎉
创建日期 / Created: 2026-01-22
针对问题 / Addresses Issue: 帮忙排查一下性能问题
SDK 版本 / SDK Version: v16.21.0+