From 535cae89618ebcf720b5ec4816d2624c98b7596e Mon Sep 17 00:00:00 2001 From: Loretta Date: Sat, 20 Jul 2024 18:10:55 +0200 Subject: [PATCH] SignalR LogContextUserNameAndId; Add json SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve to program.cs; --- TIAMWebApp/Server/Program.cs | 7 +++ .../Server/Services/DevAdminSignalRhub.cs | 43 ++++++++++++++++++- 2 files changed, 48 insertions(+), 2 deletions(-) diff --git a/TIAMWebApp/Server/Program.cs b/TIAMWebApp/Server/Program.cs index b5a515dc..6623dbd0 100644 --- a/TIAMWebApp/Server/Program.cs +++ b/TIAMWebApp/Server/Program.cs @@ -17,7 +17,9 @@ using TIAMWebApp.Server.Services; using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.ResponseCompression; using System.IO.Compression; +using System.Text.Json.Serialization; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; +using Microsoft.AspNetCore.Http.Json; using Microsoft.EntityFrameworkCore; var builder = WebApplication.CreateBuilder(args); @@ -49,6 +51,11 @@ builder.Services.AddScoped(); builder.Services.AddSignalR(options => options.MaximumReceiveMessageSize = 102400 * 1024);//.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData)); +builder.Services.Configure(options => +{ + options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve; +}); + builder.Services.AddCors(options => { options.AddPolicy(myAllowSpecificOrigins, policy => diff --git a/TIAMWebApp/Server/Services/DevAdminSignalRhub.cs b/TIAMWebApp/Server/Services/DevAdminSignalRhub.cs index 9d402896..e026cefa 100644 --- a/TIAMWebApp/Server/Services/DevAdminSignalRhub.cs +++ b/TIAMWebApp/Server/Services/DevAdminSignalRhub.cs @@ -16,6 +16,7 @@ using TIAM.Entities.Addresses; using Microsoft.AspNetCore.Hosting; using System.Collections.Generic; using System.ComponentModel; +using System.Diagnostics; using System.Linq.Expressions; using AutoMapper; using AyCode.Core.Helpers; @@ -24,6 +25,8 @@ using TIAM.Entities.Emails; using TIAM.Services.Server; using Profile = TIAM.Entities.Profiles.Profile; using Serialize.Linq.Serializers; +using System.Security.Claims; +using AyCode.Core; namespace TIAMWebApp.Server.Services; @@ -123,7 +126,16 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe { _logger.Debug($"Server OnConnectedAsync; ConnectionId: {Context.ConnectionId}; UserIdentifier: {Context.UserIdentifier}"); - //await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users"); + LogContextUserNameAndId(); + + ////insert or updatde them into database. + //var CId = _context.UserIdToCId.Find(userId); + //CId.ConnectionId = connectionid; + //_context.Update(CId); + //await _context.SaveChangesAsync(); + //await base.OnConnectedAsync(); + ////await Groups.AddToGroupAsync(Context.ConnectionId, "SignalR Users"); + await base.OnConnectedAsync(); //Clients.Caller.ConnectionId = Context.ConnectionId; @@ -137,6 +149,8 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe if (exception == null) _logger.Debug(logText); else _logger.Error(logText, exception); + LogContextUserNameAndId(); + //await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users"); await base.OnDisconnectedAsync(exception); } @@ -151,6 +165,8 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe try { + if (AcDomain.IsDeveloperVersion) LogContextUserNameAndId(); + foreach (var methodsByDeclaringObject in _dynamicMethodCallModels) { if (!methodsByDeclaringObject.MethodsByMessageTag.TryGetValue(messageTag, out var methodInfoModel)) continue; @@ -225,7 +241,7 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe return; } - + _logger.Debug($"Not found dynamic method for the tag! {tagName}"); switch (messageTag) @@ -313,6 +329,12 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe 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(ISignalRHubItemServer sendTo, int messageTag, ISignalRMessage message, int? requestId = null) { var responseDataMessagePack = message.ToMessagePack(ContractlessStandardResolver.Options); @@ -325,4 +347,21 @@ public class DevAdminSignalRHub : Hub, IAcSignalRHubServe { //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(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}"); + } + } \ No newline at end of file