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 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);
var mt = callClass.GetType().GetMethod(method.Method);
if (mt != null)
{
var pt = mt.GetParameters()[0].ParameterType;
try
mt.Invoke(callClass, pt == typeof(string)
? new object[] { reqBody }
: new[]
{
mt.Invoke(callClass, pt == typeof(string)
? new object[] { reqBody }
: new[] { JsonSerializer.Deserialize(reqBody, pt) });
EventServerChannel.BasicAck(ea.DeliveryTag, false);
}
catch (Exception)
{
EventServerChannel.BasicNack(ea.DeliveryTag, false, true);
}
}
else
{
Logger.LogError("Event Callback not available: {Ev}", method.Key);
EventServerChannel.BasicNack(ea.DeliveryTag, false, false);
}
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
})
});
EventServerChannel.BasicAck(ea.DeliveryTag, false);
}
catch (Exception e)
{
Logger.LogError("Event Processor Error: {Err}", e.Message);
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.Unicode;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Serialization;
using RabbitMQ.Client.Events;
using SimApi.Communications;
using SimApi.Helpers;
@@ -64,7 +65,11 @@ public partial class Synapse
if (ResponseCache.TryGetValue(props.MessageId, out var value))
{
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);
break;
}
+27 -20
View File
@@ -9,6 +9,7 @@ using Microsoft.Extensions.Logging;
using RabbitMQ.Client.Events;
using SimApi.Communications;
using SimApi.Exceptions;
using SimApi.Helpers;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace SimApi;
@@ -36,37 +37,43 @@ public partial class Synapse
{
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
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;
if (pt == typeof(string))
{
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);
}
var ret = mt.Invoke(callClass, new[] { reqBody });
res = new SimApiBaseResponse<object>(ret);
}
catch (SimApiException e)
else
{
res = new SimApiBaseResponse(e.Code, e.Message);
}
catch (Exception e)
{
res = new SimApiBaseResponse(500, e.ToString());
var paramObj = JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
});
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),
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
Console.WriteLine(returnJson);
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
var props = RpcServerChannel.CreateBasicProperties();
props.AppId = Options.AppId;
+9 -2
View File
@@ -2,6 +2,7 @@ using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text.Json;
using Microsoft.Extensions.Logging;
using RabbitMQ.Client;
using RabbitMQ.Client.Exceptions;
@@ -75,8 +76,14 @@ public partial class Synapse
RunRpcClient();
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
}
RunRpcServer();
RunEventServer();
if (RpcRegistry.Count > 0)
{
RunRpcServer();
}
if (EventRegistry.Count > 0)
{
RunEventServer();
}
}