diff --git a/Lagrange.OneBot/Core/Message/MessageService.cs b/Lagrange.OneBot/Core/Message/MessageService.cs index 4f5c3c97f..e708cef94 100644 --- a/Lagrange.OneBot/Core/Message/MessageService.cs +++ b/Lagrange.OneBot/Core/Message/MessageService.cs @@ -5,7 +5,7 @@ using Lagrange.Core.Message; using Lagrange.Core.Utility.Extension; using Lagrange.OneBot.Core.Entity.Message; -using Lagrange.OneBot.Core.Network.Service; +using Lagrange.OneBot.Core.Network; using Lagrange.OneBot.Database; namespace Lagrange.OneBot.Core.Message; @@ -15,11 +15,11 @@ namespace Lagrange.OneBot.Core.Message; /// public sealed class MessageService { - private readonly ILagrangeWebService _service; + private readonly LagrangeWebSvcCollection _service; private readonly ContextBase _context; private readonly Dictionary _entityToSegment; - public MessageService(BotContext bot, ILagrangeWebService service, ContextBase context) + public MessageService(BotContext bot, LagrangeWebSvcCollection service, ContextBase context) { _service = service; _context = context; diff --git a/Lagrange.OneBot/Core/Network/LagrangeWebSvcCollection.cs b/Lagrange.OneBot/Core/Network/LagrangeWebSvcCollection.cs new file mode 100644 index 000000000..e1199c995 --- /dev/null +++ b/Lagrange.OneBot/Core/Network/LagrangeWebSvcCollection.cs @@ -0,0 +1,29 @@ +using Lagrange.OneBot.Core.Network.Service; +using Microsoft.Extensions.Hosting; + +namespace Lagrange.OneBot.Core.Network; + +public class LagrangeWebSvcCollection : List, IHostedService +{ + public event EventHandler OnMessageReceived = delegate { }; + + public LagrangeWebSvcCollection(IEnumerable services) : base(services) + { + foreach (var service in this) service.OnMessageReceived += OnMessageReceived.Invoke; + } + + public async Task StartAsync(CancellationToken cancellationToken) + { + foreach (var service in this) await service.StartAsync(cancellationToken); + } + + public async Task StopAsync(CancellationToken cancellationToken) + { + foreach (var service in this) await service.StopAsync(cancellationToken); + } + + public async Task SendJsonAsync(T json, CancellationToken cancellationToken = default) + { + foreach (var service in this) await service.SendJsonAsync(json, cancellationToken); + } +} \ No newline at end of file diff --git a/Lagrange.OneBot/Core/Operation/OperationService.cs b/Lagrange.OneBot/Core/Operation/OperationService.cs index c33c832f4..4385de466 100644 --- a/Lagrange.OneBot/Core/Operation/OperationService.cs +++ b/Lagrange.OneBot/Core/Operation/OperationService.cs @@ -3,6 +3,7 @@ using Lagrange.Core; using Lagrange.Core.Utility.Extension; using Lagrange.OneBot.Core.Entity.Action; +using Lagrange.OneBot.Core.Network; using Lagrange.OneBot.Core.Network.Service; namespace Lagrange.OneBot.Core.Operation; @@ -10,10 +11,10 @@ namespace Lagrange.OneBot.Core.Operation; public sealed class OperationService { private readonly BotContext _bot; - private readonly ILagrangeWebService _service; + private readonly LagrangeWebSvcCollection _service; private readonly Dictionary _operations; - public OperationService(BotContext bot, ILagrangeWebService service) + public OperationService(BotContext bot, LagrangeWebSvcCollection service) { _bot = bot; _service = service; @@ -55,5 +56,9 @@ private async Task HandleOperation(string data) throw new Exception("action deserialized failed"); } } + catch + { + await _service.SendJsonAsync(new OneBotResult(null, 200, "failed")); + } } } \ No newline at end of file diff --git a/Lagrange.OneBot/LagrangeAppBuilder.cs b/Lagrange.OneBot/LagrangeAppBuilder.cs index aba542e61..66890d586 100644 --- a/Lagrange.OneBot/LagrangeAppBuilder.cs +++ b/Lagrange.OneBot/LagrangeAppBuilder.cs @@ -3,6 +3,7 @@ using Lagrange.Core.Common.Interface; using Lagrange.Core.Utility.Sign; using Lagrange.OneBot.Core.Message; +using Lagrange.OneBot.Core.Network; using Lagrange.OneBot.Core.Network.Service; using Lagrange.OneBot.Core.Operation; using Lagrange.OneBot.Database; @@ -85,6 +86,8 @@ public LagrangeAppBuilder ConfigureOneBot() { Services.AddSingleton(); } + + Services.AddSingleton(); Services.AddSingleton(); Services.AddSingleton();