fix BackgroundService

This commit is contained in:
xbotter 2023-06-27 10:23:30 +08:00
parent 9774a601b2
commit d143e02927
No known key found for this signature in database
GPG key ID: A3F32F44E9F160E1
4 changed files with 18 additions and 16 deletions

View file

@ -72,7 +72,10 @@ public class PluginLoader
{
if (module.GetType().GetInterface(nameof(IBotSharpAppPlugin)) != null)
{
(module as IBotSharpAppPlugin).Configure(app);
if (_settings.Plugins.Contains(module.GetType().Name))
{
(module as IBotSharpAppPlugin).Configure(app);
}
}
});
}

View file

@ -11,7 +11,7 @@ using System.Threading.Tasks;
namespace BotSharp.Plugin.WeChat
{
public class WeChatBackgroundService : IHostedService, IMessageQueue
public class WeChatBackgroundService : BackgroundService, IMessageQueue
{
private readonly Channel<WeChatMessage> _queue;
private readonly IServiceProvider _service;
@ -21,7 +21,7 @@ namespace BotSharp.Plugin.WeChat
IServiceProvider service,
ILogger<WeChatBackgroundService> logger)
{
this._service = service;
this._logger = logger;
this._queue = Channel.CreateUnbounded<WeChatMessage>();
@ -65,7 +65,12 @@ namespace BotSharp.Plugin.WeChat
await Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendTextAsync(appId, openid, content);
}
public async Task StartAsync(CancellationToken cancellationToken)
public async Task EnqueueAsync(WeChatMessage message)
{
await _queue.Writer.WriteAsync(message);
}
protected override async Task ExecuteAsync(CancellationToken cancellationToken)
{
while (!cancellationToken.IsCancellationRequested)
{
@ -80,15 +85,5 @@ namespace BotSharp.Plugin.WeChat
}
}
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
public async Task EnqueueAsync(WeChatMessage message)
{
await _queue.Writer.WriteAsync(message);
}
}
}

View file

@ -34,7 +34,9 @@ namespace BotSharp.Plugin.WeChat
services = services.AddSenparcGlobalServices(config);
}
services.AddHostedService<WeChatBackgroundService>();
services.AddSingleton<WeChatBackgroundService>();
services.AddHostedService(s => s.GetRequiredService<WeChatBackgroundService>());
services.TryAddSingleton<IMessageQueue>(s => s.GetRequiredService<WeChatBackgroundService>());
}
@ -53,6 +55,8 @@ namespace BotSharp.Plugin.WeChat
app.UseMessageHandlerForMp("/WeChatAsync", BotSharpMessageHandler.GenerateMessageHandler, options =>
{
options.AccountSettingFunc = context => Senparc.Weixin.Config.SenparcWeixinSetting;
options.EnbleResponseLog = false;
options.EnableRequestLog = false;
});
logger.LogInformation("WeChat Message Handler is running on /WeChatAsync.");

View file

@ -77,7 +77,7 @@
"BotSharp.Plugin.AzureOpenAI",
"BotSharp.Plugin.MetaAI",
"BotSharp.Plugin.Qdrant",
"BotSharp.Plugin.PaddleSharp"
"BotSharp.Plugin.PaddleSharp",
"BotSharp.Plugin.WeChat"
],
"Plugins": [