I am trying to call this specific c#-signalR-client function from my c#-SignalR-server which takes a highly complex model class as parameter:
Task OnError_InitSession(InitSessionResponseModel? response)
{
Console.WriteLine($"OnError_InitSession(): '{response?.Error}')
}
This is how the InitSessionResponseModel-model (Scroll all the way down) is built including all nested classes:
public class ApplicationRoleClaim : Microsoft.AspNetCore.Identity.IdentityRoleClaim<Guid>
{
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationRole : Microsoft.AspNetCore.Identity.IdentityRole<Guid>
{
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
public virtual ICollection<ApplicationRoleClaim> RoleClaims { get; set; }
}
public class ApplicationUserRole : Microsoft.AspNetCore.Identity.IdentityUserRole<Guid>
{
public virtual ApplicationUser User { get; set; }
public virtual ApplicationRole Role { get; set; }
}
public class ApplicationUserToken : Microsoft.AspNetCore.Identity.IdentityUserToken<Guid>
{
[Required, Key]
public int Id { get; set; }
public virtual ApplicationUser User { get; set; }
[Required, PersonalData]
public override Guid UserId { get; set; } = default!;
[Required, ProtectedPersonalData]
public override string? Value { get; set; }
[DefaultValue(3600),Required] public long Duration { get; set; }
[DefaultValue(true), Required] public bool Enabled { get; set; }
[Required] public DateTime Created { get; set; }
}
public class ApplicationUserClaim : Microsoft.AspNetCore.Identity.IdentityUserClaim<Guid>
{
public virtual ApplicationUser User { get; set; }
}
public class ApplicationUser :
Microsoft.AspNetCore.Identity.IdentityUser<Guid>
{
public override Guid Id { get; set; } = default!;
[DefaultValue("(getdate())")][Required] public DateTime Created { get; set; }
public virtual ICollection<ApplicationUserClaim> Claims { get; set; }
public virtual ICollection<ApplicationUserLogin> Logins { get; set; }
public virtual ICollection<ApplicationUserToken> Tokens { get; set; }
public virtual ICollection<ApplicationUserRole> UserRoles { get; set; }
}
public class ApplicationUserLogin :
Microsoft.AspNetCore.Identity.IdentityUserLogin<Guid>
{
public virtual ApplicationUser User { get; set; }
public int Id { get; set; }
[Required]
public DateTime Created { get; set; }
public string? ConnectionId { get; set; }
public string? UserAgent { get; set; }
public string? IPAddress { get; set; }
public string? Language { get; set; }
}
public partial class TablePlayer
{
public Guid Id { get; set; }
public Guid TableId { get; set; }
public Guid PlayerId { get; set; }
public short RoleId { get; set; }
public DateTime Joined { get; set; }
public DateTime? Exited { get; set; }
}
public partial class Player
{
public Guid Id { get; set; }
public string Username { get; set; } = null!;
public short RoleId { get; set; }
public short StatusId { get; set; }
public long Chips { get; set; }
public bool AcceptedTos { get; set; }
}
public record PlayerDetailModel
{
public TablePlayer? TableData { get; set; }
public Player? PlayerData { get; set; }
public ApplicationUserLogin? LoginData { get; set; }
}
public class BoardPosition
{
public BoardPosition() { }
public BoardPosition(int x, int y)
{
X = x;
Y = y;
}
public int X { get; set; }
public int Y { get; set; }
public bool IsValidPositionX()
=> X is >= 0 and <= 24;
public bool IsValidPositionY()
=> Y is >= 0 or <= 24;
}
public class BaseResponseModel
{
public bool IsSuccess { get; set; } = false;
public string? Error { get; set; } = string.Empty;
public DateTime Created { get; set; } = DateTime.Now;
}
public class InitSessionResponseModel : BaseResponseModel
{
public List<PlayerDetailModel> PlayersPlaying { get; set; } = new();
public List<PlayerDetailModel> PlayersSpectating { get; set; } = new();
public Guid? RoundId { get; set; }
public Guid? TurnId { get; set; }
public List<BoardPosition> CheckerPositions_Player1 { get; set; } = new();
public List<BoardPosition> CheckerPositions_Player2 { get; set; } = new();
public long CurrentStake { get; set; }
public long ScorePlayer1 { get; set; }
public long ScorePlayer2 { get; set; }
public TABLE_STATUSES? TableStatus { get; set; }//simple enum
public TABLE_DOUBLINGS Doubling { get; set; }//simple enum
public int Dice1 { get; set; }
public int Dice2 { get; set; }
public int MovesRemaining { get; set; }
public int[] DiceRollsUnplayed { get; set; } = [];
public DateTime TimeLeft { get; set; }
public int TurnPlayerNo { get; set; }
public Guid TurnPlayerId { get; set; }
public bool IsGameOver { get; set; }
public bool IsRoundOpener { get; set; }
public int TurnNumber { get; set; }
}
SignalR.Server’s configuration:
builder.Services.AddSignalR(options => { options.EnableDetailedErrors = true; })
.AddNewtonsoftJsonProtocol(optionsJSON =>
{
optionsJSON.PayloadSerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
optionsJSON.PayloadSerializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full;
optionsJSON.PayloadSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
optionsJSON.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
optionsJSON.PayloadSerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
});
Client configuration:
_Connection = new HubConnectionBuilder()
.WithUrl(HUB_ADDRESS, options =>
{
options.HttpMessageHandlerFactory = (message) =>
{
if (message is HttpClientHandler clientHandler)
// always verify the SSL certificate
clientHandler.ServerCertificateCustomValidationCallback +=
(sender, certificate, chain, sslPolicyErrors) => { return true; };
return message;
};
options.WebSocketConfiguration = wsc => wsc.RemoteCertificateValidationCallback = (sender, certificate, chain, policyErrors) => true;
//options.UseDefaultCredentials = true;
options.AccessTokenProvider = async () => await Task.FromResult(token);
})
.AddNewtonsoftJsonProtocol(optionsJSON =>
{
optionsJSON.PayloadSerializerSettings.TypeNameHandling = Newtonsoft.Json.TypeNameHandling.All;
optionsJSON.PayloadSerializerSettings.TypeNameAssemblyFormatHandling = TypeNameAssemblyFormatHandling.Full;
optionsJSON.PayloadSerializerSettings.MissingMemberHandling = MissingMemberHandling.Ignore;
optionsJSON.PayloadSerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
optionsJSON.PayloadSerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
})
.Build();
Attempted client-function calls from server:
//fails silently, no exceptions thrown.
await Clients.Client(Context.ConnectionId).OnError_InitSession(new InitSessionResponseModel() { Error = "ERROR!"});
//succeeds!
await Clients.Client(Context.ConnectionId).OnError_InitSession(null);
This is the only failing client function. My guess is the Microsoft.AspNetCore.Identity.Identity-models break the deserialization proccess, since all my other client functions with nested classes work fine except this one beeing the only class with properties inheriting from Microsoft.AspNetCore.Identity.Identity.
Can anything be done with the serializersettings to fix this?
Thanks
Installed packages
Server (.NET 8.0):
- “Microsoft.AspNet.SignalR.Core” Version=”2.4.3″
- “Microsoft.AspNetCore.Authentication.Google” Version=”8.0.7″
- “Microsoft.AspNetCore.Authentication.JwtBearer” Version=”8.0.7″
- “Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore”
Version=”8.0.7″ - “Microsoft.AspNetCore.Identity.EntityFrameworkCore”
Version=”8.0.7″ - “Microsoft.AspNetCore.Identity.UI” Version=”8.0.7″
- “Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson”
Version=”8.0.7″ - “Microsoft.EntityFrameworkCore” Version=”8.0.7″
- “Microsoft.EntityFrameworkCore.Design” Version=”8.0.7″
- “Microsoft.Extensions.DependencyInjection” Version=”8.0.0″
- “Swashbuckle.AspNetCore” Version=”6.7.0″
- “Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson” Version=”8.0.7″
- “Newtonsoft.Json” Version=”13.0.3″
Client (.netstandard2.1):
- “Microsoft.AspNetCore.SignalR.Client” Version=”8.0.7″
- “Microsoft.AspNetCore.SignalR.Client.SourceGenerator”
Version=”7.0.0-preview.7.22376.6″ - “Microsoft.AspNetCore.SignalR.Protocols.NewtonsoftJson”
Version=”8.0.7″ - “Newtonsoft.Json” Version=”13.0.3″