-
Notifications
You must be signed in to change notification settings - Fork 4.4k
/
Program.cs
89 lines (66 loc) · 2.61 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllersWithViews();
//使用本地缓存必须添加
builder.Services.AddMemoryCache();
#region 添加微信配置(一行代码)
//Senparc.Weixin 注册(必须)
builder.Services.AddSenparcWeixin(builder.Configuration);
#endregion
var app = builder.Build();
#region 启用微信配置(一句代码)
//手动获取配置信息可使用以下方法
//var senparcWeixinSetting = app.Services.GetService<IOptions<SenparcWeixinSetting>>()!.Value;
//启用微信配置(必须)
var registerService = app.UseSenparcWeixin(app.Environment,
null /* 不为 null 则覆盖 appsettings 中的 SenpacSetting 配置*/,
null /* 不为 null 则覆盖 appsettings 中的 SenpacWeixinSetting 配置*/,
register => { /* CO2NET 全局配置 */ },
(register, weixinSetting) =>
{
//注册公众号信息(可以执行多次,注册多个公众号)
register.RegisterMpAccount(weixinSetting, "【盛派网络小助手】公众号");
});
#region 使用 MessageHadler 中间件,用于取代创建独立的 Controller
//MessageHandler 中间件介绍:https://www.cnblogs.com/szw/p/Wechat-MessageHandler-Middleware.html
//使用公众号的 MessageHandler 中间件(不再需要创建 Controller)
app.UseMessageHandlerForMp("/WeixinAsync", CustomMessageHandler.GenerateMessageHandler, options =>
{
//获取默认微信配置
var weixinSetting = Senparc.Weixin.Config.SenparcWeixinSetting;
//[必须] 设置微信配置
options.AccountSettingFunc = context => weixinSetting;
//[可选] 设置最大文本长度回复限制(超长后会调用客服接口分批次回复)
options.TextResponseLimitOptions = new TextResponseLimitOptions(2048, weixinSetting.WeixinAppId);
});
#endregion
#endregion
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
#region 此部分代码为 Sample 共享文件需要而添加,实际项目无需添加
#if DEBUG
//app.UseStaticFiles(new StaticFileOptions
//{
// FileProvider = new ManifestEmbeddedFileProvider(Assembly.GetExecutingAssembly(), "wwwroot"),
// RequestPath = new PathString("")
//});
app.UseStaticFiles(new StaticFileOptions
{
FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"..", "..", "Shared", "Senparc.Weixin.Sample.Shared", "wwwroot")),
RequestPath = new PathString("")
});
#endif
#endregion
app.UseRouting();
app.UseAuthorization();
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
app.Run();