From 7ed72fbf84795101e0aba3cffffc9d2ef9ee7473 Mon Sep 17 00:00:00 2001 From: NepPure Date: Wed, 17 Jan 2024 03:33:28 +0000 Subject: [PATCH] jsonex --- .../Core/Operation/OperationService.cs | 42 +++++++++++-------- 1 file changed, 24 insertions(+), 18 deletions(-) diff --git a/Lagrange.OneBot/Core/Operation/OperationService.cs b/Lagrange.OneBot/Core/Operation/OperationService.cs index 312c0e846..0b08f6fe0 100644 --- a/Lagrange.OneBot/Core/Operation/OperationService.cs +++ b/Lagrange.OneBot/Core/Operation/OperationService.cs @@ -21,47 +21,53 @@ public OperationService(BotContext bot, ILogger logger, LiteDa { _bot = bot; _logger = logger; - + _operations = new Dictionary(); foreach (var type in Assembly.GetExecutingAssembly().GetTypes()) { var attribute = type.GetCustomAttribute(); if (attribute != null) _operations[attribute.Api] = type; } - + var service = new ServiceCollection(); service.AddSingleton(context); service.AddSingleton(logger); - + foreach (var (_, type) in _operations) service.AddScoped(type); _service = service.BuildServiceProvider(); } public async Task HandleOperation(MsgRecvEventArgs e) { - if (JsonSerializer.Deserialize(e.Data) is { } action) + try { - try + if (JsonSerializer.Deserialize(e.Data) is { } action) { - if (_operations.TryGetValue(action.Action, out var type)) + try { - var handler = (IOperation)_service.GetRequiredService(type); - var result = await handler.HandleOperation(_bot, action.Params); - result.Echo = action.Echo; + if (_operations.TryGetValue(action.Action, out var type)) + { + var handler = (IOperation)_service.GetRequiredService(type); + var result = await handler.HandleOperation(_bot, action.Params); + result.Echo = action.Echo; - return result; - } + return result; + } - return new OneBotResult(null, 404, "failed") { Echo = action.Echo }; - } - catch (Exception ex) - { - _logger.LogWarning(ex, "Unexpected error encountered while handling message."); - return new OneBotResult(null, 200, "failed") { Echo = action.Echo }; + return new OneBotResult(null, 404, "failed") { Echo = action.Echo }; + } + catch (Exception ex) + { + _logger.LogWarning(ex, "Unexpected error encountered while handling message."); + return new OneBotResult(null, 200, "failed") { Echo = action.Echo }; + } } } + catch (Exception ex) + { + _logger.LogWarning(ex, "Json Serialization failed for such action"); + } - _logger.LogWarning("Json Serialization failed for such action"); return null; } } \ No newline at end of file