SignalR LogContextUserNameAndId; Add json SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve to program.cs;

This commit is contained in:
Loretta 2024-07-20 18:10:55 +02:00
parent e1f9e1748d
commit 535cae8961
2 changed files with 48 additions and 2 deletions

View File

@ -17,7 +17,9 @@ using TIAMWebApp.Server.Services;
using Microsoft.AspNetCore.Server.Kestrel.Core; using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.AspNetCore.ResponseCompression; using Microsoft.AspNetCore.ResponseCompression;
using System.IO.Compression; using System.IO.Compression;
using System.Text.Json.Serialization;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting; using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -49,6 +51,11 @@ builder.Services.AddScoped<LoggerApiController>();
builder.Services.AddSignalR(options => options.MaximumReceiveMessageSize = 102400 * 1024);//.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData)); builder.Services.AddSignalR(options => options.MaximumReceiveMessageSize = 102400 * 1024);//.AddMessagePackProtocol(options => options.SerializerOptions = MessagePackSerializerOptions.Standard.WithSecurity(MessagePackSecurity.UntrustedData));
builder.Services.Configure<JsonOptions>(options =>
{
options.SerializerOptions.ReferenceHandler = ReferenceHandler.Preserve;
});
builder.Services.AddCors(options => builder.Services.AddCors(options =>
{ {
options.AddPolicy(myAllowSpecificOrigins, policy => options.AddPolicy(myAllowSpecificOrigins, policy =>

View File

@ -16,6 +16,7 @@ using TIAM.Entities.Addresses;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel; using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions; using System.Linq.Expressions;
using AutoMapper; using AutoMapper;
using AyCode.Core.Helpers; using AyCode.Core.Helpers;
@ -24,6 +25,8 @@ using TIAM.Entities.Emails;
using TIAM.Services.Server; using TIAM.Services.Server;
using Profile = TIAM.Entities.Profiles.Profile; using Profile = TIAM.Entities.Profiles.Profile;
using Serialize.Linq.Serializers; using Serialize.Linq.Serializers;
using System.Security.Claims;
using AyCode.Core;
namespace TIAMWebApp.Server.Services; namespace TIAMWebApp.Server.Services;
@ -123,7 +126,16 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
{ {
_logger.Debug($"Server OnConnectedAsync; ConnectionId: {Context.ConnectionId}; UserIdentifier: {Context.UserIdentifier}"); _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(); await base.OnConnectedAsync();
//Clients.Caller.ConnectionId = Context.ConnectionId; //Clients.Caller.ConnectionId = Context.ConnectionId;
@ -137,6 +149,8 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
if (exception == null) _logger.Debug(logText); if (exception == null) _logger.Debug(logText);
else _logger.Error(logText, exception); else _logger.Error(logText, exception);
LogContextUserNameAndId();
//await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users"); //await Groups.RemoveFromGroupAsync(Context.ConnectionId, "SignalR Users");
await base.OnDisconnectedAsync(exception); await base.OnDisconnectedAsync(exception);
} }
@ -151,6 +165,8 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
try try
{ {
if (AcDomain.IsDeveloperVersion) LogContextUserNameAndId();
foreach (var methodsByDeclaringObject in _dynamicMethodCallModels) foreach (var methodsByDeclaringObject in _dynamicMethodCallModels)
{ {
if (!methodsByDeclaringObject.MethodsByMessageTag.TryGetValue(messageTag, out var methodInfoModel)) continue; if (!methodsByDeclaringObject.MethodsByMessageTag.TryGetValue(messageTag, out var methodInfoModel)) continue;
@ -225,7 +241,7 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
return; return;
} }
_logger.Debug($"Not found dynamic method for the tag! {tagName}"); _logger.Debug($"Not found dynamic method for the tag! {tagName}");
switch (messageTag) switch (messageTag)
@ -313,6 +329,12 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
protected async Task ResponseToCaller(int messageTag, ISignalRMessage message, int? requestId) protected async Task ResponseToCaller(int messageTag, ISignalRMessage message, int? requestId)
=> await SendMessageToClient(Clients.Caller, messageTag, message, 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) protected async Task SendMessageToClient(ISignalRHubItemServer sendTo, int messageTag, ISignalRMessage message, int? requestId = null)
{ {
var responseDataMessagePack = message.ToMessagePack(ContractlessStandardResolver.Options); var responseDataMessagePack = message.ToMessagePack(ContractlessStandardResolver.Options);
@ -325,4 +347,21 @@ public class DevAdminSignalRHub : Hub<ISignalRHubItemServer>, IAcSignalRHubServe
{ {
//await Clients.Group(groupId).Post("", messageTag, 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(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}");
}
} }