fix some bug

This commit is contained in:
2023-10-20 14:13:53 +08:00
parent ef721b227b
commit caeb697421
4 changed files with 61 additions and 46 deletions
+19 -23
View File
@@ -28,32 +28,28 @@ public partial class Synapse
var key = ea.RoutingKey.Replace("event.", string.Empty); var key = ea.RoutingKey.Replace("event.", string.Empty);
var method = EventRegistry.FirstOrDefault(x => x.Key == key); var method = EventRegistry.FirstOrDefault(x => x.Key == key);
if (method != null) var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
var mt = callClass.GetType().GetMethod(method.Method);
var pt = mt.GetParameters()[0].ParameterType;
try
{ {
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class); mt.Invoke(callClass, pt == typeof(string)
var mt = callClass.GetType().GetMethod(method.Method); ? new object[] { reqBody }
if (mt != null) : new[]
{
var pt = mt.GetParameters()[0].ParameterType;
try
{ {
mt.Invoke(callClass, pt == typeof(string) JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
? new object[] { reqBody } {
: new[] { JsonSerializer.Deserialize(reqBody, pt) }); PropertyNamingPolicy = JsonNamingPolicy.CamelCase
EventServerChannel.BasicAck(ea.DeliveryTag, false); })
} });
catch (Exception) EventServerChannel.BasicAck(ea.DeliveryTag, false);
{ }
EventServerChannel.BasicNack(ea.DeliveryTag, false, true); catch (Exception e)
} {
} Logger.LogError("Event Processor Error: {Err}", e.Message);
else EventServerChannel.BasicNack(ea.DeliveryTag, false, false);
{
Logger.LogError("Event Callback not available: {Ev}", method.Key);
EventServerChannel.BasicNack(ea.DeliveryTag, false, false);
}
} }
}; };
EventServerChannel.BasicConsume(queue, true, "", false, false, null, consumer); EventServerChannel.BasicConsume(queue, false, "", false, false, null, consumer);
} }
} }
+6 -1
View File
@@ -5,6 +5,7 @@ using System.Text.Encodings.Web;
using System.Text.Json; using System.Text.Json;
using System.Text.Unicode; using System.Text.Unicode;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using RabbitMQ.Client.Events; using RabbitMQ.Client.Events;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Helpers; using SimApi.Helpers;
@@ -64,7 +65,11 @@ public partial class Synapse
if (ResponseCache.TryGetValue(props.MessageId, out var value)) if (ResponseCache.TryGetValue(props.MessageId, out var value))
{ {
response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>( response = JsonSerializer.Deserialize<SimApiBaseResponse<object>>(
Encoding.UTF8.GetString(value)); Encoding.UTF8.GetString(value), new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All)
});
ResponseCache.Remove(props.MessageId); ResponseCache.Remove(props.MessageId);
break; break;
} }
+27 -20
View File
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
using RabbitMQ.Client.Events; using RabbitMQ.Client.Events;
using SimApi.Communications; using SimApi.Communications;
using SimApi.Exceptions; using SimApi.Exceptions;
using SimApi.Helpers;
using JsonSerializer = System.Text.Json.JsonSerializer; using JsonSerializer = System.Text.Json.JsonSerializer;
namespace SimApi; namespace SimApi;
@@ -36,37 +37,43 @@ public partial class Synapse
{ {
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class); var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
var mt = callClass.GetType().GetMethod(method.Method); var mt = callClass.GetType().GetMethod(method.Method);
if (mt != null) try
{ {
try var pt = mt.GetParameters()[0].ParameterType;
if (pt == typeof(string))
{ {
var pt = mt.GetParameters()[0].ParameterType; var ret = mt.Invoke(callClass, new[] { reqBody });
if (pt == typeof(string)) res = new SimApiBaseResponse<object>(ret);
{
var ret = mt.Invoke(callClass, new[] { reqBody });
res = new SimApiBaseResponse<object>(ret);
}
else
{
var ret = mt.Invoke(callClass, new[] { JsonSerializer.Deserialize(reqBody, pt) });
res = new SimApiBaseResponse<object>(ret);
}
} }
catch (SimApiException e) else
{ {
res = new SimApiBaseResponse(e.Code, e.Message); var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
} {
catch (Exception e) PropertyNamingPolicy = JsonNamingPolicy.CamelCase
{ });
res = new SimApiBaseResponse(500, e.ToString()); var ret = mt.Invoke(callClass, new[] { paramObj });
res = new SimApiBaseResponse<object>(ret);
} }
} }
catch (SimApiException e)
{
Logger.LogDebug("RPC错误: {Err}", e.Message);
res = new SimApiBaseResponse(e.Code, e.Message);
}
catch (Exception e)
{
Logger.LogDebug("RPC调用失败: {Err}", e.Message);
res = new SimApiBaseResponse(500, e.Message);
}
} }
var returnJson = JsonSerializer.Serialize((SimApiBaseResponse<object>)res, new JsonSerializerOptions
var returnJson = JsonSerializer.Serialize((object)res, new JsonSerializerOptions
{ {
Encoder = JavaScriptEncoder.Create(UnicodeRanges.All), Encoder = JavaScriptEncoder.Create(UnicodeRanges.All),
PropertyNamingPolicy = JsonNamingPolicy.CamelCase, PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
}); });
Console.WriteLine(returnJson);
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}"; var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
var props = RpcServerChannel.CreateBasicProperties(); var props = RpcServerChannel.CreateBasicProperties();
props.AppId = Options.AppId; props.AppId = Options.AppId;
+9 -2
View File
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text.Json;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using RabbitMQ.Client; using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions; using RabbitMQ.Client.Exceptions;
@@ -75,8 +76,14 @@ public partial class Synapse
RunRpcClient(); RunRpcClient();
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout); Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
} }
RunRpcServer(); if (RpcRegistry.Count > 0)
RunEventServer(); {
RunRpcServer();
}
if (EventRegistry.Count > 0)
{
RunEventServer();
}
} }