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 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);
if (mt != null)
{
var pt = mt.GetParameters()[0].ParameterType;
try
{
mt.Invoke(callClass, pt == typeof(string)
? new object[] { reqBody }
: new[] { JsonSerializer.Deserialize(reqBody, pt) });
: new[]
{
JsonSerializer.Deserialize(reqBody, pt, new JsonSerializerOptions
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase
})
});
EventServerChannel.BasicAck(ea.DeliveryTag, false);
}
catch (Exception)
catch (Exception e)
{
EventServerChannel.BasicNack(ea.DeliveryTag, false, true);
}
}
else
{
Logger.LogError("Event Callback not available: {Ev}", method.Key);
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;
}
+13 -6
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,8 +37,6 @@ public partial class Synapse
{
var callClass = Sp.CreateScope().ServiceProvider.GetRequiredService(method.Class);
var mt = callClass.GetType().GetMethod(method.Method);
if (mt != null)
{
try
{
var pt = mt.GetParameters()[0].ParameterType;
@@ -48,25 +47,33 @@ public partial class Synapse
}
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);
}
}
catch (SimApiException e)
{
Logger.LogDebug("RPC错误: {Err}", e.Message);
res = new SimApiBaseResponse(e.Code, e.Message);
}
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),
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
});
Console.WriteLine(returnJson);
var reply = $"client.{ea.BasicProperties.ReplyTo}.{ea.BasicProperties.AppId}";
var props = RpcServerChannel.CreateBasicProperties();
props.AppId = Options.AppId;
+7
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,9 +76,15 @@ public partial class Synapse
RunRpcClient();
Logger.LogInformation("Rpc Client Ready, Client Timeout: {OptionsRpcTimeout}s", Options.RpcTimeout);
}
if (RpcRegistry.Count > 0)
{
RunRpcServer();
}
if (EventRegistry.Count > 0)
{
RunEventServer();
}
}
public SimApiBaseResponse<T> Rpc<T>(string appName, string method, dynamic param)