adapt to IApplicationBuilder configure

This commit is contained in:
xbotter 2023-06-27 06:16:21 +08:00
parent 3a7b3aa057
commit 8b680203dd
No known key found for this signature in database
GPG key ID: D299220A7FE5CF1E
6 changed files with 57 additions and 6 deletions

View file

@ -0,0 +1,12 @@
using Microsoft.AspNetCore.Builder;
using System;
using System.Collections.Generic;
using System.Text;
namespace BotSharp.Abstraction.Plugins
{
public interface IBotSharpAppPlugin: IBotSharpPlugin
{
void Configure(IApplicationBuilder app);
}
}

View file

@ -40,6 +40,8 @@ public static class BotSharpServiceCollectionExtensions
throw new ArgumentNullException(nameof(app));
}
app.ApplicationServices.GetRequiredService<PluginLoader>().Configure(app);
return app;
}
@ -77,5 +79,7 @@ public static class BotSharpServiceCollectionExtensions
var loader = new PluginLoader(services, config, pluginSettings);
loader.Load();
services.AddSingleton(loader);
}
}

View file

@ -1,3 +1,4 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using System.Drawing;
using System.IO;
@ -13,7 +14,7 @@ public class PluginLoader
private readonly PluginLoaderSettings _settings;
private static List<IBotSharpPlugin> _modules = new List<IBotSharpPlugin>();
public PluginLoader(IServiceCollection services,
public PluginLoader(IServiceCollection services,
IConfiguration config,
PluginLoaderSettings settings)
{
@ -59,4 +60,20 @@ public class PluginLoader
}
});
}
public void Configure(IApplicationBuilder app)
{
if(_modules.Count == 0)
{
Console.WriteLine($"No plugin loaded. Please check whether the Load() method is called.", Color.Yellow);
}
_modules.ForEach(module =>
{
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null)
{
(module as IBotSharpAppPlugin).Configure(app);
}
});
}
}

View file

@ -24,7 +24,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Senparc.Weixin.MP.MVC" Version="7.12.7" />
<PackageReference Include="Senparc.Weixin.MP.Middleware" Version="0.8.6" />
<PackageReference Include="System.Threading.Channels" Version="7.0.0" />
</ItemGroup>

View file

@ -18,6 +18,9 @@ namespace BotSharp.Plugin.WeChat
{
public class BotSharpMessageHandler : Senparc.Weixin.MP.MessageHandlers.MessageHandler<DefaultMpMessageContext>
{
public static Func<Stream, PostModel, int, IServiceProvider, BotSharpMessageHandler> GenerateMessageHandler = (stream, postModel, maxRecordCount, serviceProvider)
=> new BotSharpMessageHandler(stream, postModel, maxRecordCount, false /* 是否只允许处理加密消息,以提高安全性 */, serviceProvider: serviceProvider);
public BotSharpMessageHandler(Stream inputStream, PostModel postModel, int maxRecordCount = 0, bool onlyAllowEncryptMessage = false, DeveloperInfo developerInfo = null, IServiceProvider serviceProvider = null) : base(inputStream, postModel, maxRecordCount, onlyAllowEncryptMessage, developerInfo, serviceProvider)
{
}

View file

@ -4,16 +4,20 @@ using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Hosting;
using Senparc.Weixin.AspNet;
using Senparc.CO2NET.AspNet;
using Senparc.CO2NET;
using Senparc.Weixin.RegisterServices;
using System;
using System.Collections.Generic;
using System.Text;
using Senparc.Weixin;
using Senparc.Weixin.MP;
using Senparc.Weixin.MP.MessageHandlers.Middleware;
namespace BotSharp.Plugin.WeChat
{
public class WeChatPlugin : IBotSharpPlugin
public class WeChatPlugin : IBotSharpAppPlugin
{
public void RegisterDI(IServiceCollection services, IConfiguration config)
{
@ -26,9 +30,20 @@ namespace BotSharp.Plugin.WeChat
services.TryAddSingleton<IMessageQueue>(s => s.GetRequiredService<WeChatBackgroundService>());
}
public void ConfigurateApplication(IApplicationBuilder app)
public void Configure(IApplicationBuilder app)
{
// TODO: app.UseSenparcWeixin
var env = app.ApplicationServices.GetRequiredService<IHostEnvironment>();
var register = app.UseSenparcGlobal(env);
register.UseSenparcWeixin(null, (svc, settings) =>
{
svc.RegisterMpAccount(settings, "WeChat");
}, app.ApplicationServices);
app.UseMessageHandlerForMp("/WeixinAsync", BotSharpMessageHandler.GenerateMessageHandler, options =>
{
options.AccountSettingFunc = context => Senparc.Weixin.Config.SenparcWeixinSetting;
});
}
}
}