🐛 Trying to fix Pass service issues

This commit is contained in:
2025-11-23 03:02:51 +08:00
parent d553ca2ca7
commit 2024972832
3 changed files with 55 additions and 4 deletions

View File

@@ -170,12 +170,12 @@ public class BroadcastEventHandler(
await nats.PublishAsync( await nats.PublishAsync(
AccountStatusUpdatedEvent.Type, AccountStatusUpdatedEvent.Type,
GrpcTypeHelper.ConvertObjectToByteString(new AccountStatusUpdatedEvent ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent
{ {
AccountId = evt.AccountId, AccountId = evt.AccountId,
Status = status, Status = status,
UpdatedAt = SystemClock.Instance.GetCurrentInstant() UpdatedAt = SystemClock.Instance.GetCurrentInstant()
}).ToByteArray() }, GrpcTypeHelper.SerializerOptionsWithIgnore)).ToByteArray()
); );
logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId);
@@ -209,12 +209,12 @@ public class BroadcastEventHandler(
await nats.PublishAsync( await nats.PublishAsync(
AccountStatusUpdatedEvent.Type, AccountStatusUpdatedEvent.Type,
GrpcTypeHelper.ConvertObjectToByteString(new AccountStatusUpdatedEvent ByteString.CopyFromUtf8(JsonSerializer.Serialize(new AccountStatusUpdatedEvent
{ {
AccountId = evt.AccountId, AccountId = evt.AccountId,
Status = status, Status = status,
UpdatedAt = SystemClock.Instance.GetCurrentInstant() UpdatedAt = SystemClock.Instance.GetCurrentInstant()
}).ToByteArray() }, GrpcTypeHelper.SerializerOptionsWithIgnore)).ToByteArray()
); );
logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId); logger.LogInformation("Broadcasted status update for user {AccountId}", evt.AccountId);

View File

@@ -211,6 +211,7 @@ public class CacheServiceRedis : ICacheService
}, },
ReferenceHandler = ReferenceHandler.Preserve, ReferenceHandler = ReferenceHandler.Preserve,
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals, NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
Converters = { new ByteStringConverter() }
}; };
_jsonOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb); _jsonOptions.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
_jsonOptions.PropertyNameCaseInsensitive = true; _jsonOptions.PropertyNameCaseInsensitive = true;

View File

@@ -0,0 +1,50 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;
using Google.Protobuf;
namespace DysonNetwork.Shared.Data;
public class ByteStringConverter : JsonConverter<ByteString>
{
public override ByteString Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
if (reader.TokenType == JsonTokenType.Null)
{
return ByteString.Empty;
}
if (reader.TokenType == JsonTokenType.String)
{
return ByteString.FromBase64(reader.GetString());
}
//This is a temporary workaround to fix the issue that the old data in the cache is not in base64 format.
if (reader.TokenType == JsonTokenType.StartObject)
{
var value = string.Empty;
while (reader.Read())
{
if (reader.TokenType == JsonTokenType.EndObject)
{
return ByteString.CopyFromUtf8(value);
}
if (reader.TokenType == JsonTokenType.PropertyName && reader.GetString() == "$values")
{
if (reader.Read() && reader.TokenType == JsonTokenType.String)
{
value = reader.GetString();
}
}
}
}
throw new JsonException("Expected a string or an object with a '$values' property.");
}
public override void Write(Utf8JsonWriter writer, ByteString value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.ToBase64());
}
}