Repository navigation
SupplyParameterFromSession support for Blazor #65184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
239b61b
596df7f
1ed92f9
215c5a7
89422d9
b47e153
ba0c74a
3ee272d
5f9ca60
725040a
ed0b04d
ea2bc22
420c545
e5692c6
bbb7c86
f820c5d
331fd5c
d6e03c7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,181 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System.Collections.Concurrent; | ||
| using System.Reflection; | ||
| using System.Text.Json; | ||
| using Microsoft.AspNetCore.Components.Reflection; | ||
| using Microsoft.AspNetCore.Components.Rendering; | ||
| using Microsoft.AspNetCore.Components.Web; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
|
||
| internal partial class SessionCascadingValueSupplier | ||
| { | ||
| private static readonly ConcurrentDictionary<(Type, string), PropertyGetter> _propertyGetterCache = new(); | ||
| private static readonly JsonSerializerOptions _jsonOptions = new(JsonSerializerDefaults.Web); | ||
| private HttpContext? _httpContext; | ||
| private bool _onStartingRegistered; | ||
| private readonly Dictionary<string, Func<object?>> _valueCallbacks = new(StringComparer.OrdinalIgnoreCase); | ||
| private readonly ILogger<SessionCascadingValueSupplier> _logger; | ||
|
|
||
| public SessionCascadingValueSupplier(ILogger<SessionCascadingValueSupplier> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| internal void SetRequestContext(HttpContext httpContext) | ||
| { | ||
| _httpContext = httpContext; | ||
|
dariatiurina marked this conversation as resolved.
Resolved
|
||
| } | ||
|
|
||
| internal CascadingParameterSubscription CreateSubscription( | ||
| ComponentState componentState, | ||
| SupplyParameterFromSessionAttribute attribute, | ||
| CascadingParameterInfo parameterInfo) | ||
| { | ||
| if (!_onStartingRegistered && _httpContext is not null) | ||
| { | ||
| _onStartingRegistered = true; | ||
| _httpContext.Response.OnStarting(PersistAllValues); | ||
| } | ||
|
|
||
| var sessionKey = attribute.Name ?? parameterInfo.PropertyName; | ||
| var componentType = componentState.Component.GetType(); | ||
| var getter = _propertyGetterCache.GetOrAdd((componentType, parameterInfo.PropertyName), PropertyGetterFactory); | ||
| Func<object?> valueGetter = () => getter.GetValue(componentState.Component); | ||
| RegisterValueCallback(sessionKey, valueGetter); | ||
| return new SessionSubscription(this, sessionKey, parameterInfo.PropertyType, valueGetter); | ||
| } | ||
|
|
||
| private static PropertyGetter PropertyGetterFactory((Type type, string propertyName) key) | ||
| { | ||
| var (type, propertyName) = key; | ||
| var propertyInfo = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic); | ||
| if (propertyInfo is null) | ||
| { | ||
| throw new InvalidOperationException($"A property '{propertyName}' on component type '{type.FullName}' wasn't found."); | ||
| } | ||
| return new PropertyGetter(type, propertyInfo); | ||
| } | ||
|
|
||
| internal ISession? GetSession() => _httpContext?.Features.Get<ISessionFeature>()?.Session; | ||
|
|
||
| internal void RegisterValueCallback(string sessionKey, Func<object?> valueGetter) | ||
| { | ||
| if (!_valueCallbacks.TryAdd(sessionKey, valueGetter)) | ||
| { | ||
| throw new InvalidOperationException($"A callback is already registered for the session key '{sessionKey}'. Multiple components cannot use the same session key for multiple [SupplyParameterFromSession] attributes."); | ||
| } | ||
| } | ||
|
|
||
| internal Task PersistAllValues() | ||
| { | ||
| if (_valueCallbacks.Count == 0) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| var session = GetSession(); | ||
| if (session is null) | ||
| { | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| foreach (var (key, valueGetter) in _valueCallbacks) | ||
| { | ||
| var sessionKey = key.ToLowerInvariant(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we actually want the keys to be lower-cased? The TempData supplier does not do this, afaik.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| try | ||
| { | ||
| var value = valueGetter(); | ||
| if (value is not null) | ||
| { | ||
| var json = JsonSerializer.Serialize(value, value.GetType(), _jsonOptions); | ||
| session.SetString(sessionKey, json); | ||
| } | ||
| else | ||
| { | ||
| session.Remove(sessionKey); | ||
| } | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.SessionPersistFail(_logger, ex); | ||
| } | ||
| } | ||
| return Task.CompletedTask; | ||
| } | ||
|
|
||
| internal void DeleteValueCallback(string sessionKey) | ||
| { | ||
| _valueCallbacks.Remove(sessionKey); | ||
| } | ||
|
|
||
| private static partial class Log | ||
| { | ||
| [LoggerMessage(1, LogLevel.Warning, "Persisting of the session element failed.", EventName = "SessionPersistFail")] | ||
| public static partial void SessionPersistFail(ILogger logger, Exception exception); | ||
|
|
||
| [LoggerMessage(2, LogLevel.Warning, "Deserialization of the element from session failed.", EventName = "SessionDeserializeFail")] | ||
| public static partial void SessionDeserializeFail(ILogger logger, Exception exception); | ||
| } | ||
|
|
||
| internal partial class SessionSubscription : CascadingParameterSubscription | ||
| { | ||
| private readonly SessionCascadingValueSupplier _owner; | ||
| private readonly string _sessionKey; | ||
| private readonly Type _propertyType; | ||
| private readonly Func<object?> _currentValueGetter; | ||
| private bool _delivered; | ||
|
|
||
| public SessionSubscription( | ||
| SessionCascadingValueSupplier owner, | ||
| string sessionKey, | ||
| Type propertyType, | ||
| Func<object?> currentValueGetter) | ||
| { | ||
| _owner = owner; | ||
| _sessionKey = sessionKey; | ||
| _propertyType = propertyType; | ||
| _currentValueGetter = currentValueGetter; | ||
| } | ||
|
|
||
| public override object? GetCurrentValue() | ||
| { | ||
| if (_delivered) | ||
| { | ||
| return _currentValueGetter(); | ||
| } | ||
|
|
||
| _delivered = true; | ||
| var session = _owner.GetSession(); | ||
| if (session is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var json = session.GetString(_sessionKey.ToLowerInvariant()); | ||
| if (string.IsNullOrEmpty(json)) | ||
| { | ||
| return null; | ||
| } | ||
| return JsonSerializer.Deserialize(json, _propertyType, _jsonOptions); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.SessionDeserializeFail(_owner._logger, ex); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be silent and not throw? If this happens, it seems like a bug.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did the same thing in |
||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| _owner.DeleteValueCallback(_sessionKey); | ||
| } | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.