Repository navigation
SupplyParameterFromTempData support for Blazor #65306
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
Merged
dariatiurina
merged 76 commits into
dotnet:main
from
dariatiurina:49683-supply-parameter-from-tempdata
Apr 17, 2026
+733
−13
Merged
Changes from all commits
Commits
Show all changes
76 commits
Select commit
Hold shift + click to select a range
7c557e2
Basic E2E test
dariatiurina 8f21417
First simple implementation that holds values no matter what
dariatiurina 43608b0
Made TempData connected to the HttpContext and improved TempData
dariatiurina 5612308
Fixed E2E test to be SSR
dariatiurina c73f370
Add for manual testing
dariatiurina 9948bcd
Added new E2E tests
dariatiurina edb2244
Merge branch 'main' into 49683-tempdata
dariatiurina a9385a6
Merge branch '49683-tempdata' of https://github.com/dariatiurina/aspn…
dariatiurina 9869aa7
Added XML comment
dariatiurina d93d266
Added new tests
dariatiurina 7630764
Added inheritance from IDictionary
dariatiurina 8b5bbe2
Added IDataProtector to TempDataService
dariatiurina 1d2fc6d
Fix + Enumerator
dariatiurina 31ead26
Fix + unit tests
dariatiurina 68f401d
Merge branch 'main' into 49683-tempdata
dariatiurina 21a3c3b
Fix
dariatiurina 7b50676
TempDataTest
dariatiurina ecebef8
Fix
dariatiurina 6002ab1
Clean-up
dariatiurina 902cdc9
Add limit for encoded value
dariatiurina eff02b6
Small decoupling
dariatiurina 1f23e69
Moved to Endpoints
dariatiurina 98ece70
Lazy loading
dariatiurina bfcace9
Added ITempDataProvider and CookieTempDataProvider
dariatiurina e5e26c6
Fixed visibility modifiers for TempData and CookieTempDataProvider
dariatiurina 5817356
Decoupling and small fix
dariatiurina 677c014
Update serializer
dariatiurina c5b0365
Fixes
dariatiurina 8f91662
Fix
dariatiurina 968a345
Fixed lazy loading in TempData
dariatiurina fab6474
Added options to the ServiceExtensions for Cookie TempData
dariatiurina 0e36b05
Added CookieChunkingManager
dariatiurina 5789c74
Clean-up
dariatiurina 90d1a91
Fix serializer
dariatiurina 03fbc76
Added SessionStorageTempDataProvider
dariatiurina 082a035
Minor fixes
dariatiurina 10fd4d9
Feedback
dariatiurina 077baae
Some small improvements
dariatiurina 12a538d
Changed options
dariatiurina 533f1d4
Made ITempDataProvider internal
dariatiurina 77aa240
Made loadFunc non nullable in the constructor
dariatiurina 90310c4
fix
dariatiurina 4ecd5ed
Changed JsonTempDataSerializer
dariatiurina c95e54c
fix
dariatiurina fb958f8
Change naming for test component
dariatiurina e5bc396
Changed AddTempData
dariatiurina eb5a42e
Changed RazorComponentsServiceCollectionExtensions
dariatiurina dd473d7
Changed to internal
dariatiurina c0e729b
Feedback
dariatiurina a0d0dff
Fix
dariatiurina c146364
[SupplyParameterFromTempData] and tests for it
dariatiurina 5ecedf6
Merge branch 'main' into 49683-supply-parameter-from-tempdata
dariatiurina 2063a75
E2E test and fixes
dariatiurina 7f25222
Merge branch '49683-supply-parameter-from-tempdata' of https://github…
dariatiurina 2f301f4
Feedback
dariatiurina ffb0962
Added usage of the existing reflection
dariatiurina 61df8be
Feedback
dariatiurina a7596b1
Update src/Components/Components/src/SupplyParameterFromTempDataAttri…
dariatiurina 326b38e
Update src/Components/test/E2ETest/Tests/TempDataCookieTest.cs
dariatiurina 23a2437
Update src/Components/test/E2ETest/Tests/TempDataSessionStorageTest.cs
dariatiurina e4576ae
Update src/Components/test/E2ETest/Tests/TempDataCookieTest.cs
dariatiurina 6503334
Update src/Components/test/testassets/Components.TestServer/RazorComp…
dariatiurina c7181e3
Update src/Components/Endpoints/src/TempData/ITempDataValueMapper.cs
dariatiurina e542502
Update src/Components/test/E2ETest/Tests/TempDataSessionStorageTest.cs
dariatiurina ef55229
Fix
dariatiurina f755d97
Fixed namespace
dariatiurina fef2126
Changed layering
dariatiurina 269272e
Fix
dariatiurina 5284b0f
Changed the design
dariatiurina 43bf05e
Fixed the "duplicate implementation types" error
dariatiurina 68113d2
Merge branch 'main' into 49683-supply-parameter-from-tempdata
dariatiurina d4ca5ac
Addressing feedback
dariatiurina 47aa7a7
Merge branch '49683-supply-parameter-from-tempdata' of https://github…
dariatiurina 849c7b2
Merge branch 'main' into 49683-supply-parameter-from-tempdata
dariatiurina 27dbe93
Feedback
dariatiurina cca4f91
Fix
dariatiurina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/Components/Components/src/CascadingParameterSubscription.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| /// <summary> | ||
| /// This class represents an active subscription to a cascading parameter. The implementation is responsible for providing the current value and notifying about changes by causing the subscriber component to re-render when necessary. | ||
| /// </summary> | ||
| public abstract class CascadingParameterSubscription : IDisposable | ||
| { | ||
| /// <summary> | ||
| /// Function that returns the current value for the subscription. | ||
| /// </summary> | ||
| public abstract object? GetCurrentValue(); | ||
|
|
||
| /// <inheritdoc/> | ||
| public abstract void Dispose(); | ||
| } |
49 changes: 49 additions & 0 deletions
49
src/Components/Components/src/CascadingParameterValueProvider.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Components.Infrastructure; | ||
| using Microsoft.AspNetCore.Components.Rendering; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components; | ||
|
|
||
| internal class CascadingParameterValueProvider<TAttribute> : ICascadingValueSupplier | ||
| where TAttribute : CascadingParameterAttributeBase | ||
| { | ||
| private readonly Dictionary<ComponentSubscriptionKey, CascadingParameterSubscription> _subscriptions = new(); | ||
| private readonly Func<ComponentState, TAttribute, CascadingParameterInfo, CascadingParameterSubscription> _subscribeFactory; | ||
|
|
||
| public CascadingParameterValueProvider(Func<ComponentState, TAttribute, CascadingParameterInfo, CascadingParameterSubscription> subscribeFactory) | ||
| { | ||
| _subscribeFactory = subscribeFactory; | ||
| } | ||
|
|
||
| public bool IsFixed => false; | ||
|
|
||
| public bool CanSupplyValue(in CascadingParameterInfo parameterInfo) | ||
| => parameterInfo.Attribute is TAttribute; | ||
|
|
||
| public object? GetCurrentValue(object? key, in CascadingParameterInfo parameterInfo) | ||
| { | ||
| var subscriptionKey = new ComponentSubscriptionKey((ComponentState)key!, parameterInfo.PropertyName); | ||
| if (_subscriptions.TryGetValue(subscriptionKey, out var subscription)) | ||
| { | ||
| return subscription.GetCurrentValue(); | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| public void Subscribe(ComponentState subscriber, in CascadingParameterInfo parameterInfo) | ||
| { | ||
| var key = new ComponentSubscriptionKey(subscriber, parameterInfo.PropertyName); | ||
| _subscriptions[key] = _subscribeFactory(subscriber, (TAttribute)parameterInfo.Attribute, parameterInfo); | ||
| } | ||
|
|
||
| public void Unsubscribe(ComponentState subscriber, in CascadingParameterInfo parameterInfo) | ||
| { | ||
| var key = new ComponentSubscriptionKey(subscriber, parameterInfo.PropertyName); | ||
| if (_subscriptions.Remove(key, out var subscription)) | ||
| { | ||
| subscription.Dispose(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
src/Components/Endpoints/src/TempData/TempDataCascadingValueSupplier.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,175 @@ | ||
| // 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 Microsoft.AspNetCore.Components.Reflection; | ||
| using Microsoft.AspNetCore.Components.Rendering; | ||
| using Microsoft.AspNetCore.Components.Web; | ||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
|
||
| internal partial class TempDataCascadingValueSupplier | ||
| { | ||
| private static readonly ConcurrentDictionary<(Type, string), PropertyGetter> _propertyGetterCache = new(); | ||
| private HttpContext? _httpContext; | ||
| private readonly Dictionary<string, Func<object?>> _valueCallbacks = new(StringComparer.OrdinalIgnoreCase); | ||
|
dariatiurina marked this conversation as resolved.
Resolved
|
||
| private readonly ILogger<TempDataCascadingValueSupplier> _logger; | ||
|
|
||
| public TempDataCascadingValueSupplier(ILogger<TempDataCascadingValueSupplier> logger) | ||
| { | ||
| _logger = logger; | ||
| } | ||
|
|
||
| internal void SetRequestContext(HttpContext httpContext) | ||
| { | ||
| _httpContext = httpContext; | ||
| } | ||
|
|
||
| internal CascadingParameterSubscription CreateSubscription( | ||
| ComponentState componentState, | ||
| SupplyParameterFromTempDataAttribute attribute, | ||
| CascadingParameterInfo parameterInfo) | ||
| { | ||
| var tempDataKey = 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(tempDataKey, valueGetter); | ||
| return new TempDataSubscription(this, tempDataKey, 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 ITempData? GetTempData() => _httpContext is null | ||
| ? null | ||
| : TempDataProviderServiceCollectionExtensions.GetOrCreateTempData(_httpContext); | ||
|
|
||
| internal void RegisterValueCallback(string tempDataKey, Func<object?> valueGetter) | ||
| { | ||
| if (!_valueCallbacks.TryAdd(tempDataKey, valueGetter)) | ||
| { | ||
| throw new InvalidOperationException($"A callback is already registered for the TempData key '{tempDataKey}'. Multiple components cannot use the same TempData key for multiple [SupplyParameterFromTempData] attributes."); | ||
| } | ||
| } | ||
|
|
||
| internal void PersistValues(ITempData tempData) | ||
| { | ||
| if (_valueCallbacks.Count == 0) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| foreach (var (key, valueGetter) in _valueCallbacks) | ||
| { | ||
| object? value = null; | ||
| try | ||
| { | ||
| value = valueGetter(); | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.TempDataPersistFail(_logger, ex); | ||
| continue; | ||
| } | ||
| tempData[key] = value; | ||
| } | ||
| } | ||
|
|
||
| internal void DeleteValueCallback(string tempDataKey) | ||
| { | ||
| _valueCallbacks.Remove(tempDataKey); | ||
| } | ||
|
|
||
| private static partial class Log | ||
| { | ||
| [LoggerMessage(1, LogLevel.Warning, "Persisting of the TempData element failed.", EventName = "TempDataPersistFail")] | ||
| public static partial void TempDataPersistFail(ILogger logger, Exception exception); | ||
|
|
||
| [LoggerMessage(2, LogLevel.Warning, "Deserialization of the element from TempData failed.", EventName = "TempDataDeserializeFail")] | ||
| public static partial void TempDataDeserializeFail(ILogger logger, Exception exception); | ||
| } | ||
|
|
||
| internal partial class TempDataSubscription : CascadingParameterSubscription | ||
| { | ||
| private readonly TempDataCascadingValueSupplier _owner; | ||
| private readonly string _tempDataKey; | ||
| private readonly Type _underlyingType; | ||
| private readonly bool _isEnum; | ||
| private readonly Func<object?> _currentValueGetter; | ||
| private bool _delivered; | ||
|
|
||
| public TempDataSubscription( | ||
| TempDataCascadingValueSupplier owner, | ||
| string tempDataKey, | ||
| Type propertyType, | ||
| Func<object?> currentValueGetter) | ||
| { | ||
| _owner = owner; | ||
| _tempDataKey = tempDataKey; | ||
| _underlyingType = Nullable.GetUnderlyingType(propertyType) ?? propertyType; | ||
| _isEnum = _underlyingType.IsEnum; | ||
| _currentValueGetter = currentValueGetter; | ||
| } | ||
|
|
||
| public override object? GetCurrentValue() | ||
| { | ||
| if (_delivered) | ||
| { | ||
| // After the first delivery, return the component's current property value | ||
| // to avoid overriding modifications the component made during rendering. | ||
| return _currentValueGetter(); | ||
| } | ||
|
|
||
| _delivered = true; | ||
|
|
||
| var tempData = _owner.GetTempData(); | ||
| if (tempData is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var value = tempData.Get(_tempDataKey); | ||
| if (value is null) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| if (_isEnum && value is int intValue) | ||
| { | ||
| return Enum.ToObject(_underlyingType, intValue); | ||
| } | ||
|
|
||
| if (!_underlyingType.IsAssignableFrom(value.GetType())) | ||
| { | ||
| return null; | ||
| } | ||
|
|
||
| return value; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| Log.TempDataDeserializeFail(_owner._logger, ex); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| public override void Dispose() | ||
| { | ||
| _owner.DeleteValueCallback(_tempDataKey); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a public API and a top-level
IServiceCollectionextension that usesComponentStatein its signature. WhileComponentStateis also public, it is in theRenderingnamespace and is explicitly documented as being an internal implementation detail ofRenderer. Is that intentional? (@javiercn)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a trade-off between this and having to put TempData and all other similar concepts in Components. The main consumers for this are other libraries on the stack, and while I don't love it. The alternative of having to put the attribute on a core assembly and a bunch of the infrastructure there, is worse.
The key tension here is that hosts, lack the ability to define their own host specific cascading values that aren't top level (AddCascadingValue exists, but it's a single value (like we do for HttpContext))
We can review what we use componentstate for (we use it as a key) and consider doing something else (like wrapping it into a struct and call it something like StateKey) but I don't think we get much out of it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tradeoff is what we do in persistent component state and what we would need to also do in SupplyParameterFromSession, so we will end up with N host specific method registrations in the core assembly. With this approach, the hosts can register them in their setups using this common method.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds reasonable. Thanks for the explanation 👍