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
+10 -14
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 callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
var mt = callClass.GetType().GetMethod(method.Method); var mt = callClass.GetType().GetMethod(method.Method);
if (mt != null)
{
var pt = mt.GetParameters()[0].ParameterType; var pt = mt.GetParameters()[0].ParameterType;
try try
{ {
mt.Invoke(callClass, pt == typeof(string) mt.Invoke(callClass, pt == typeof(string)
? new object[] { reqBody } ? new object[] { reqBody }
: new[] { JsonSerializer.Deserialize(reqBody, pt) }); : new[]
{
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
})
});
EventServerChannel.BasicAck(ea.DeliveryTag, false); EventServerChannel.BasicAck(ea.DeliveryTag, false);
} }
catch (Exception) catch (Exception e)
{ {
EventServerChannel.BasicNack(ea.DeliveryTag, false, true); Logger.LogError("Event Processor Error: {Err}", e.Message);
}
}
else
{
Logger.LogError("Event Callback not available: {Ev}", method.Key);
EventServerChannel.BasicNack(ea.DeliveryTag, false, false); 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;
} }
+13 -6
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,8 +37,6 @@ 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; var pt = mt.GetParameters()[0].ParameterType;
@@ -48,25 +47,33 @@ public partial class Synapse
} }
else else
{ {
var ret = mt.Invoke(callClass, new[] { JsonSerializer.Deserialize(reqBody, pt) }); var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
var ret = mt.Invoke(callClass, new[] { paramObj });
res = new SimApiBaseResponse<object>(ret); res = new SimApiBaseResponse<object>(ret);
} }
} }
catch (SimApiException e) catch (SimApiException e)
{ {
Logger.LogDebug("RPC错误: {Err}", e.Message);
res = new SimApiBaseResponse(e.Code, e.Message); res = new SimApiBaseResponse(e.Code, e.Message);
} }
catch (Exception e) catch (Exception e)
{ {
res = new SimApiBaseResponse(500, e.ToString()); 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;
+7
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,9 +76,15 @@ 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);
} }
if (RpcRegistry.Count > 0)
{
RunRpcServer(); RunRpcServer();
}
if (EventRegistry.Count > 0)
{
RunEventServer(); RunEventServer();
} }
}
public SimApiBaseResponse<T> Rpc<T>(string appName, string method, dynamic param) public SimApiBaseResponse<T> Rpc<T>(string appName, string method, dynamic param)