Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/Services/SessionState/JsonSessionKeySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ public JsonSessionKeySerializer(IOptions<JsonSessionSerializerOptions> options,
[LoggerMessage(0, LogLevel.Error, "Unexpected JSON serialize/deserialization error for '{Key}' expected type '{Type}'")]
private partial void LogException(Exception e, string key, string type);


[LoggerMessage(1, LogLevel.Warning, "Session key '{Key}' is registered as {RegisteredType} but was actually {FoundType}")]
private partial void UnexpectedType(string key, Type registeredType, Type foundType);

public bool TryDeserialize(string key, byte[] bytes, out object? obj)
{
if (_options.Value.KnownKeys.TryGetValue(key, out var type))
Expand All @@ -43,16 +47,23 @@ public bool TryDeserialize(string key, byte[] bytes, out object? obj)

public bool TrySerialize(string key, object value, out byte[] bytes)
{
if (_options.Value.KnownKeys.TryGetValue(key, out var type) && type == value.GetType())
if (_options.Value.KnownKeys.TryGetValue(key, out var type))
{
try
if (type == value.GetType())
{
bytes = JsonSerializer.SerializeToUtf8Bytes(value, type);
return true;
try
{
bytes = JsonSerializer.SerializeToUtf8Bytes(value, type);
return true;
}
catch (JsonException e)
{
LogException(e, key, type.Name);
}
}
catch (JsonException e)
else
{
LogException(e, key, type.Name);
UnexpectedType(key, type, value.GetType());
}
}

Expand Down