Compare commits
No commits in common. "942bac61fdc8c2272c047aacff207d45a73b0fe3" and "5f661991f9cccc826750f9774745496abba4d0a8" have entirely different histories.
942bac61fd
...
5f661991f9
|
|
@ -18,9 +18,6 @@
|
||||||
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.Components.Web" Version="8.0.10" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="8.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="8.0.10" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Core" Version="1.2.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="8.0.0" />
|
|
||||||
<PackageReference Include="Microsoft.Extensions.Identity.Core" Version="8.0.10" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|
@ -39,9 +36,6 @@
|
||||||
<Reference Include="AyCode.Services">
|
<Reference Include="AyCode.Services">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="AyCode.Services.Server">
|
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.Server.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="AyCode.Utils">
|
<Reference Include="AyCode.Utils">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Utils.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|
@ -50,11 +44,9 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Folder Include="Layouts\" />
|
<Folder Include="Layouts\" />
|
||||||
<Folder Include="Pages\" />
|
<Folder Include="Pages\" />
|
||||||
<Folder Include="Services\Logins\" />
|
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\AyCode.Blazor.Models.Server\AyCode.Blazor.Models.Server.csproj" />
|
|
||||||
<ProjectReference Include="..\AyCode.Blazor.Models\AyCode.Blazor.Models.csproj" />
|
<ProjectReference Include="..\AyCode.Blazor.Models\AyCode.Blazor.Models.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,196 +0,0 @@
|
||||||
using System.Linq.Expressions;
|
|
||||||
using System.Security.Claims;
|
|
||||||
using AyCode.Blazor.Models.Server.Models;
|
|
||||||
using AyCode.Core;
|
|
||||||
using AyCode.Core.Extensions;
|
|
||||||
using AyCode.Core.Helpers;
|
|
||||||
using AyCode.Core.Loggers;
|
|
||||||
using AyCode.Services.Server.SignalRs;
|
|
||||||
using AyCode.Services.SignalRs;
|
|
||||||
using MessagePack;
|
|
||||||
using MessagePack.Resolvers;
|
|
||||||
using Microsoft.AspNetCore.SignalR;
|
|
||||||
using Microsoft.Extensions.Configuration;
|
|
||||||
|
|
||||||
namespace AyCode.Blazor.Components.Services;
|
|
||||||
|
|
||||||
public abstract class AcWebSignalRHubBase<TSignalRTags, TLogger>(IConfiguration configuration, TLogger logger)
|
|
||||||
: Hub<IAcSignalRHubItemServer>, IAcSignalRHubServer where TSignalRTags : AcSignalRTags where TLogger : AcLoggerBase
|
|
||||||
{
|
|
||||||
protected readonly List<DynamicMethodCallModel<SignalRAttribute>> DynamicMethodCallModels = [];
|
|
||||||
//protected readonly TIAM.Core.Loggers.Logger<AcWebSignalRHubBase<TSignalRTags>> Logger = new(logWriters.ToArray());
|
|
||||||
protected TLogger Logger = logger;
|
|
||||||
protected IConfiguration Configuration = configuration;
|
|
||||||
|
|
||||||
//private readonly ServiceProviderAPIController _serviceProviderApiController;
|
|
||||||
//private readonly TransferDataAPIController _transferDataApiController;
|
|
||||||
|
|
||||||
//_serviceProviderApiController = serviceProviderApiController;
|
|
||||||
//_transferDataApiController = transferDataApiController;
|
|
||||||
|
|
||||||
// https://docs.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-3.1#strongly-typed-hubs
|
|
||||||
public override async Task OnConnectedAsync()
|
|
||||||
{
|
|
||||||
Logger.Debug($"Server OnConnectedAsync; ConnectionId: {Context.ConnectionId}; UserIdentifier: {Context.UserIdentifier}");
|
|
||||||
|
|
||||||
LogContextUserNameAndId();
|
|
||||||
|
|
||||||
await base.OnConnectedAsync();
|
|
||||||
|
|
||||||
//Clients.Caller.ConnectionId = Context.ConnectionId;
|
|
||||||
//Clients.Caller.UserIdentifier = Context.UserIdentifier;
|
|
||||||
}
|
|
||||||
|
|
||||||
public override async Task OnDisconnectedAsync(Exception? exception)
|
|
||||||
{
|
|
||||||
var logText = $"Server OnDisconnectedAsync; ConnectionId: {Context.ConnectionId}; UserIdentifier: {Context.UserIdentifier};";
|
|
||||||
|
|
||||||
if (exception == null) Logger.Debug(logText);
|
|
||||||
else Logger.Error(logText, exception);
|
|
||||||
|
|
||||||
LogContextUserNameAndId();
|
|
||||||
|
|
||||||
await base.OnDisconnectedAsync(exception);
|
|
||||||
}
|
|
||||||
|
|
||||||
public virtual Task OnReceiveMessage(int messageTag, byte[]? message, int? requestId)
|
|
||||||
{
|
|
||||||
return ProcessOnReceiveMessage(messageTag, message, requestId, null);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task ProcessOnReceiveMessage(int messageTag, byte[]? message, int? requestId, Func<string, Task>? notFoundCallback)
|
|
||||||
{
|
|
||||||
var tagName = ConstHelper.NameByValue<TSignalRTags>(messageTag);
|
|
||||||
var logText = $"Server OnReceiveMessage; {nameof(requestId)}: {requestId}; ConnectionId: {Context.ConnectionId}; {tagName}";
|
|
||||||
|
|
||||||
if (message is { Length: 0 }) Logger.Warning($"message.Length == 0! {logText}");
|
|
||||||
else Logger.Info($"[{message?.Length:N0}b] {logText}");
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
if (AcDomain.IsDeveloperVersion) LogContextUserNameAndId();
|
|
||||||
|
|
||||||
foreach (var methodsByDeclaringObject in DynamicMethodCallModels)
|
|
||||||
{
|
|
||||||
if (!methodsByDeclaringObject.MethodsByMessageTag.TryGetValue(messageTag, out var methodInfoModel)) continue;
|
|
||||||
|
|
||||||
object[]? paramValues = null;
|
|
||||||
|
|
||||||
logText = $"Found dynamic method for the tag! method: {methodsByDeclaringObject.InstanceObject.GetType().Name}.{methodInfoModel.MethodInfo.Name}";
|
|
||||||
|
|
||||||
if (methodInfoModel.ParamInfos is { Length: > 0 })
|
|
||||||
{
|
|
||||||
Logger.Debug($"{logText}({string.Join(", ", methodInfoModel.ParamInfos.Select(x => x.Name))}); {tagName}");
|
|
||||||
|
|
||||||
paramValues = new object[methodInfoModel.ParamInfos.Length];
|
|
||||||
|
|
||||||
var firstParamType = methodInfoModel.ParamInfos[0].ParameterType;
|
|
||||||
if (methodInfoModel.ParamInfos.Length > 1 || firstParamType == typeof(string) || firstParamType.IsEnum || firstParamType.IsValueType || firstParamType == typeof(DateTime))
|
|
||||||
{
|
|
||||||
var msg = message!.MessagePackTo<SignalPostJsonDataMessage<IdMessage>>();
|
|
||||||
|
|
||||||
for (var i = 0; i < msg.PostData.Ids.Count; i++)
|
|
||||||
{
|
|
||||||
//var obj = (string)msg.PostData.Ids[i];
|
|
||||||
//if (msg.PostData.Ids[i] is Guid id)
|
|
||||||
//{
|
|
||||||
// if (id.IsNullOrEmpty()) throw new NullReferenceException($"PostData.Id.IsNullOrEmpty(); Ids: {msg.PostData.Ids}");
|
|
||||||
// paramValues[i] = id;
|
|
||||||
//}
|
|
||||||
//else if (Guid.TryParse(obj, out id))
|
|
||||||
//{
|
|
||||||
// if (id.IsNullOrEmpty()) throw new NullReferenceException($"PostData.Id.IsNullOrEmpty(); Ids: {msg.PostData.Ids}");
|
|
||||||
// paramValues[i] = id;
|
|
||||||
//}
|
|
||||||
//else if (Enum.TryParse(methodInfoModel.ParameterType, obj, out var enumObj))
|
|
||||||
//{
|
|
||||||
// paramValues[i] = enumObj;
|
|
||||||
//}
|
|
||||||
//else paramValues[i] = Convert.ChangeType(obj, methodInfoModel.ParameterType);
|
|
||||||
|
|
||||||
var obj = msg.PostData.Ids[i];
|
|
||||||
//var config = new MapperConfiguration(cfg =>
|
|
||||||
//{
|
|
||||||
// cfg.CreateMap(obj.GetType(), methodInfoModel.ParameterType);
|
|
||||||
//});
|
|
||||||
|
|
||||||
//var mapper = new Mapper(config);
|
|
||||||
//paramValues[i] = mapper.Map(obj, methodInfoModel.ParameterType);
|
|
||||||
|
|
||||||
//paramValues[i] = obj;
|
|
||||||
|
|
||||||
var a = Array.CreateInstance(methodInfoModel.ParamInfos[i].ParameterType, 1);
|
|
||||||
|
|
||||||
if (methodInfoModel.ParamInfos[i].ParameterType == typeof(Expression))
|
|
||||||
{
|
|
||||||
//var serializer = new ExpressionSerializer(new JsonSerializer());
|
|
||||||
//paramValues[i] = serializer.DeserializeText((string)(obj.JsonTo(a.GetType()) as Array)?.GetValue(0)!);
|
|
||||||
}
|
|
||||||
else paramValues[i] = (obj.JsonTo(a.GetType()) as Array)?.GetValue(0)!;
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else paramValues[0] = message!.MessagePackTo<SignalPostJsonDataMessage<object>>(MessagePackSerializerOptions.Standard).PostDataJson.JsonTo(firstParamType)!;
|
|
||||||
}
|
|
||||||
else Logger.Debug($"{logText}(); {tagName}");
|
|
||||||
|
|
||||||
var responseDataJson = new SignalResponseJsonMessage(messageTag, SignalResponseStatus.Success, methodInfoModel.MethodInfo.InvokeMethod(methodsByDeclaringObject.InstanceObject, paramValues));
|
|
||||||
var responseDataJsonKiloBytes = System.Text.Encoding.Unicode.GetByteCount(responseDataJson.ResponseData!) / 1024;
|
|
||||||
|
|
||||||
//File.WriteAllText(Path.Combine("h:", $"{requestId}.json"), responseDataJson.ResponseData);
|
|
||||||
|
|
||||||
Logger.Info($"[{responseDataJsonKiloBytes}kb] responseData serialized to json");
|
|
||||||
await ResponseToCaller(messageTag, responseDataJson, requestId);
|
|
||||||
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
Logger.Debug($"Not found dynamic method for the tag! {tagName}");
|
|
||||||
notFoundCallback?.Invoke(tagName);
|
|
||||||
}
|
|
||||||
catch (Exception ex)
|
|
||||||
{
|
|
||||||
Logger.Error($"Server OnReceiveMessage; {ex.Message}; {tagName}", ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
await ResponseToCaller(messageTag, new SignalResponseJsonMessage(messageTag, SignalResponseStatus.Error), requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected async Task ResponseToCaller(int messageTag, ISignalRMessage message, int? requestId)
|
|
||||||
=> await SendMessageToClient(Clients.Caller, messageTag, message, requestId);
|
|
||||||
|
|
||||||
public async Task SendMessageToUserId(string userId, int messageTag, ISignalRMessage message, int? requestId)
|
|
||||||
=> await SendMessageToClient(Clients.User(userId), messageTag, message, requestId);
|
|
||||||
|
|
||||||
public async Task SendMessageToConnectionId(string connectionId, int messageTag, ISignalRMessage message, int? requestId)
|
|
||||||
=> await SendMessageToClient(Clients.Client(Context.ConnectionId), messageTag, message, requestId);
|
|
||||||
|
|
||||||
protected async Task SendMessageToClient(IAcSignalRHubItemServer sendTo, int messageTag, ISignalRMessage message, int? requestId = null)
|
|
||||||
{
|
|
||||||
var responseDataMessagePack = message.ToMessagePack(ContractlessStandardResolver.Options);
|
|
||||||
Logger.Info($"[{(responseDataMessagePack.Length/1024)}kb] Server sending responseDataMessagePack to client; {nameof(requestId)}: {requestId}; ConnectionId: {Context.ConnectionId}; {ConstHelper.NameByValue<TSignalRTags>(messageTag)}");
|
|
||||||
|
|
||||||
await sendTo.OnReceiveMessage(messageTag, responseDataMessagePack, requestId);
|
|
||||||
}
|
|
||||||
|
|
||||||
public async Task SendMessageToGroup(string groupId, int messageTag, string message)
|
|
||||||
{
|
|
||||||
//await Clients.Group(groupId).Post("", messageTag, message);
|
|
||||||
}
|
|
||||||
|
|
||||||
//[Conditional("DEBUG")]
|
|
||||||
private void LogContextUserNameAndId()
|
|
||||||
{
|
|
||||||
string? userName = null;
|
|
||||||
var userId = Guid.Empty;
|
|
||||||
|
|
||||||
if (Context.User != null)
|
|
||||||
{
|
|
||||||
userName = Context.User.Identity?.Name;
|
|
||||||
Guid.TryParse((string?)Context.User.FindFirstValue(ClaimTypes.NameIdentifier), out userId);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (AcDomain.IsDeveloperVersion) Logger.WarningConditional($"SignalR.Context; userName: {userName}; userId: {userId}");
|
|
||||||
else Logger.Debug($"SignalR.Context; userName: {userName}; userId: {userId}");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using System.Runtime.CompilerServices;
|
|
||||||
|
|
||||||
namespace AyCode.Blazor.Components.Services;
|
|
||||||
|
|
||||||
public static class ExtensionMethods
|
|
||||||
{
|
|
||||||
public static object? InvokeMethod(this MethodInfo methodInfo, object obj, params object[]? parameters)
|
|
||||||
{
|
|
||||||
if (methodInfo.GetCustomAttribute(typeof(AsyncStateMachineAttribute)) is AsyncStateMachineAttribute isAsyncTask)
|
|
||||||
{
|
|
||||||
dynamic awaitable = methodInfo.Invoke(obj, parameters)!;
|
|
||||||
return awaitable.GetAwaiter().GetResult();
|
|
||||||
}
|
|
||||||
|
|
||||||
return methodInfo.Invoke(obj, parameters);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
namespace AyCode.Blazor.Components.Services.Logins;
|
|
||||||
|
|
||||||
public class AcWebAuthService
|
|
||||||
{
|
|
||||||
public virtual void Logout()
|
|
||||||
{}
|
|
||||||
}
|
|
||||||
|
|
@ -58,6 +58,7 @@
|
||||||
<PackageReference Include="MessagePack" Version="2.5.187" />
|
<PackageReference Include="MessagePack" Version="2.5.187" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Client" Version="8.0.10" />
|
||||||
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="8.0.10" />
|
<PackageReference Include="Microsoft.AspNetCore.SignalR.Common" Version="8.0.10" />
|
||||||
|
<PackageReference Include="Serialize.Linq" Version="3.1.160" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -31,9 +31,6 @@
|
||||||
<Reference Include="AyCode.Models.Server">
|
<Reference Include="AyCode.Models.Server">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Models.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
<Reference Include="AyCode.Services">
|
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.dll</HintPath>
|
|
||||||
</Reference>
|
|
||||||
<Reference Include="AyCode.Services.Server">
|
<Reference Include="AyCode.Services.Server">
|
||||||
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.Server.dll</HintPath>
|
<HintPath>..\..\AyCode.Core\AyCode.Services.Server\bin\Debug\net8.0\AyCode.Services.Server.dll</HintPath>
|
||||||
</Reference>
|
</Reference>
|
||||||
|
|
@ -42,8 +39,4 @@
|
||||||
</Reference>
|
</Reference>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
|
||||||
<Folder Include="Services\" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
using System.Collections.Concurrent;
|
|
||||||
using System.Reflection;
|
|
||||||
using AyCode.Services.SignalRs;
|
|
||||||
|
|
||||||
namespace AyCode.Blazor.Models.Server.Models;
|
|
||||||
|
|
||||||
public class DynamicMethodCallModel<TAttribute> where TAttribute : TagAttribute
|
|
||||||
{
|
|
||||||
public object InstanceObject { get; init; }
|
|
||||||
public ConcurrentDictionary<int, MethodInfoModel<TAttribute>> MethodsByMessageTag { get; init; } = new();
|
|
||||||
|
|
||||||
|
|
||||||
public DynamicMethodCallModel(Type instanceObjectType) : this(instanceObjectType, null!)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public DynamicMethodCallModel(Type instanceObjectType, params object[] constructorParams) : this(Activator.CreateInstance(instanceObjectType, constructorParams)!)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public DynamicMethodCallModel(object instanceObject)
|
|
||||||
{
|
|
||||||
InstanceObject = instanceObject;
|
|
||||||
|
|
||||||
foreach (var methodInfo in instanceObject.GetType().GetMethods())
|
|
||||||
{
|
|
||||||
if (methodInfo.GetCustomAttribute(typeof(TAttribute)) is not TAttribute attribute) continue;
|
|
||||||
|
|
||||||
if (MethodsByMessageTag.ContainsKey(attribute.MessageTag))
|
|
||||||
throw new Exception($"Multiple SignaRMessageTag! messageTag: {attribute.MessageTag}; methodName: {methodInfo.Name}");
|
|
||||||
|
|
||||||
MethodsByMessageTag[attribute.MessageTag] = new MethodInfoModel<TAttribute>(attribute, methodInfo!);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
using System.Reflection;
|
|
||||||
using AyCode.Services.SignalRs;
|
|
||||||
|
|
||||||
namespace AyCode.Blazor.Models.Server.Models;
|
|
||||||
|
|
||||||
public class MethodInfoModel<TAttribute> where TAttribute : TagAttribute
|
|
||||||
{
|
|
||||||
public ParameterInfo[]? ParamInfos { get; init; } = null;
|
|
||||||
public TAttribute Attribute { get; init; }
|
|
||||||
public MethodInfo MethodInfo { get; init; }
|
|
||||||
|
|
||||||
public MethodInfoModel(TAttribute attribute, MethodInfo methodInfo)
|
|
||||||
{
|
|
||||||
Attribute = attribute;
|
|
||||||
MethodInfo = methodInfo;
|
|
||||||
|
|
||||||
var parameters = methodInfo.GetParameters();
|
|
||||||
|
|
||||||
//if (parameters.Length > 1)
|
|
||||||
// throw new Exception("MethodInfoModel; parameters.Length > 1");
|
|
||||||
|
|
||||||
ParamInfos = parameters;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue