-
Notifications
You must be signed in to change notification settings - Fork 10.9k
Fix TempData and SupplyParameterFromSession persistence for streaming SSR case #66832
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 22 commits into
dotnet:main
from
dariatiurina:66745-streamingssr-tempdata-sessiondata
Jun 16, 2026
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1acf121
Fix OnStarting for streaming SSR case
dariatiurina 376d91c
Clean-up
dariatiurina ee4ce7e
Fix
dariatiurina 3fffa0c
Merge branch '66745-streamingssr-tempdata-sessiondata' of https://git…
dariatiurina 27c8524
TempData fix
dariatiurina b8da91a
Fix tests
dariatiurina 0bc4de3
Fix TempData case
dariatiurina 16a6e52
Fix clean-up
dariatiurina e43b92a
Feedback on tests
dariatiurina cc816f9
Applied feedbacks
dariatiurina 18d36f6
Feedback
dariatiurina 29d04cf
Clean-up
dariatiurina 42664d8
Merge branch 'main' into 66745-streamingssr-tempdata-sessiondata
dariatiurina 295808b
Fix warning
dariatiurina fb4827f
Clean-up
dariatiurina fef336d
Fix logging
dariatiurina 346eaa4
Add testing
dariatiurina 7fe1b48
Improve logging message
dariatiurina c194ae4
Upgrade logging
dariatiurina cda1185
Improve null session warning
dariatiurina 315c172
Improve null session warning
dariatiurina 1c27423
Fixed test
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
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
73 changes: 73 additions & 0 deletions
73
src/Components/Endpoints/src/SessionEstablishmentHelper.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,73 @@ | ||
| // 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.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
|
||
| internal static partial class SessionEstablishmentHelper | ||
| { | ||
| private const string SessionEstablishmentKey = "__AspNetCore.Components.Endpoints.SessionEstablishment"; | ||
| private const string LoggedResponseHasStartedKey = "__AspNetCore.Components.Endpoints.SessionEstablishment.LoggedResponseHasStarted"; | ||
| private const string LoggedSessionDoesNotExistKey = "__AspNetCore.Components.Endpoints.SessionEstablishment.LoggedSessionDoesNotExist"; | ||
|
|
||
| public static void TryRegisterSessionEstablishment(HttpContext context) | ||
| { | ||
| var loggerFactory = context.RequestServices.GetRequiredService<ILoggerFactory>(); | ||
| var session = context.Features.Get<ISessionFeature>()?.Session; | ||
|
|
||
| if (session == null) | ||
| { | ||
| if (!context.Items.ContainsKey(LoggedSessionDoesNotExistKey)) | ||
| { | ||
| Log.SessionDoesNotExist(loggerFactory.CreateLogger(typeof(SessionEstablishmentHelper).FullName!)); | ||
| context.Items[LoggedSessionDoesNotExistKey] = true; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (context.Response.HasStarted) | ||
| { | ||
| if (!context.Items.ContainsKey(LoggedResponseHasStartedKey)) | ||
| { | ||
| Log.ResponseHasStarted(loggerFactory.CreateLogger(typeof(SessionEstablishmentHelper).FullName!)); | ||
| context.Items[LoggedResponseHasStartedKey] = true; | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if (context.Items.ContainsKey(SessionEstablishmentKey)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| context.Items[SessionEstablishmentKey] = true; | ||
| context.Response.OnStarting(static state => | ||
| { | ||
| var session = (ISession)state!; | ||
| session.Set(SessionEstablishmentKey, []); | ||
| session.Remove(SessionEstablishmentKey); | ||
| return Task.CompletedTask; | ||
| }, session); | ||
| } | ||
|
|
||
| private static partial class Log | ||
| { | ||
| [LoggerMessage(1, LogLevel.Warning, | ||
| "Session state was not persisted to the next request. " + | ||
| "The response has already started, so the session cookie can no longer be issued. " + | ||
| "To avoid this, place at least one [SupplyParameterFromSession] (or use Session.Set directly) before any await that triggers the first response flush.", | ||
| EventName = "SessionStateNotPersistedAfterResponseStarted")] | ||
| public static partial void ResponseHasStarted(ILogger logger); | ||
|
|
||
| [LoggerMessage(2, LogLevel.Warning, | ||
| "Session state was not persisted to the next request. " + | ||
| "No session is available session middleware was not registered. " + | ||
| "To fix this, add 'builder.Services.AddSession()' and 'app.UseSession()' to your app.", | ||
| EventName = "SessionDoesNotExist")] | ||
| public static partial void SessionDoesNotExist(ILogger logger); | ||
| } | ||
| } |
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
171 changes: 171 additions & 0 deletions
171
src/Components/Endpoints/test/Session/SessionEstablishmentHelperTest.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,171 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| #nullable enable | ||
|
|
||
| using Microsoft.AspNetCore.Http; | ||
| using Microsoft.AspNetCore.Http.Features; | ||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Testing; | ||
|
|
||
| namespace Microsoft.AspNetCore.Components.Endpoints; | ||
|
|
||
| public class SessionEstablishmentHelperTest | ||
| { | ||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsSessionDoesNotExist_WhenNoSessionFeature() | ||
| { | ||
| var sink = new TestSink(); | ||
| var httpContext = CreateHttpContext(sink, session: null, responseHasStarted: false); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
|
|
||
| var write = Assert.Single(sink.Writes); | ||
| Assert.Equal(LogLevel.Warning, write.LogLevel); | ||
| Assert.Equal("SessionDoesNotExist", write.EventId.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsResponseHasStarted_WhenSessionAvailableButResponseStarted() | ||
| { | ||
| var sink = new TestSink(); | ||
| var httpContext = CreateHttpContext(sink, session: new TestSession(), responseHasStarted: true); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
|
|
||
| var write = Assert.Single(sink.Writes); | ||
| Assert.Equal(LogLevel.Warning, write.LogLevel); | ||
| Assert.Equal("SessionStateNotPersistedAfterResponseStarted", write.EventId.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_DoesNotLog_WhenSessionAvailableAndResponseNotStarted() | ||
| { | ||
| var sink = new TestSink(); | ||
| var httpContext = CreateHttpContext(sink, session: new TestSession(), responseHasStarted: false); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
|
|
||
| Assert.Empty(sink.Writes); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsSessionDoesNotExistOncePerRequest() | ||
| { | ||
| var sink = new TestSink(); | ||
| var httpContext = CreateHttpContext(sink, session: null, responseHasStarted: false); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
|
|
||
| var write = Assert.Single(sink.Writes); | ||
| Assert.Equal("SessionDoesNotExist", write.EventId.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsSessionDoesNotExistOncePerEachRequest() | ||
| { | ||
| var sink = new TestSink(); | ||
| var first = CreateHttpContext(sink, session: null, responseHasStarted: false); | ||
| var second = CreateHttpContext(sink, session: null, responseHasStarted: false); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(first); | ||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(second); | ||
|
|
||
| Assert.Equal(2, sink.Writes.Count); | ||
| Assert.All(sink.Writes, write => Assert.Equal("SessionDoesNotExist", write.EventId.Name)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsResponseHasStartedOncePerRequest() | ||
| { | ||
| var sink = new TestSink(); | ||
| var httpContext = CreateHttpContext(sink, session: new TestSession(), responseHasStarted: true); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(httpContext); | ||
|
|
||
| var write = Assert.Single(sink.Writes); | ||
| Assert.Equal("SessionStateNotPersistedAfterResponseStarted", write.EventId.Name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryRegisterSessionEstablishment_LogsResponseHasStartedOncePerEachRequest() | ||
| { | ||
| var sink = new TestSink(); | ||
| var first = CreateHttpContext(sink, session: new TestSession(), responseHasStarted: true); | ||
| var second = CreateHttpContext(sink, session: new TestSession(), responseHasStarted: true); | ||
|
|
||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(first); | ||
| SessionEstablishmentHelper.TryRegisterSessionEstablishment(second); | ||
|
|
||
| Assert.Equal(2, sink.Writes.Count); | ||
| Assert.All(sink.Writes, write => Assert.Equal("SessionStateNotPersistedAfterResponseStarted", write.EventId.Name)); | ||
| } | ||
|
|
||
| private static DefaultHttpContext CreateHttpContext(TestSink sink, ISession? session, bool responseHasStarted) | ||
| { | ||
| var responseFeature = new TestHttpResponseFeature { HasStarted = responseHasStarted }; | ||
| return CreateHttpContext(sink, session, responseFeature); | ||
| } | ||
|
|
||
| private static DefaultHttpContext CreateHttpContext(TestSink sink, ISession? session, TestHttpResponseFeature responseFeature) | ||
| { | ||
| var services = new ServiceCollection(); | ||
| services.AddSingleton<ILoggerFactory>(new TestLoggerFactory(sink, enabled: true)); | ||
|
|
||
| var httpContext = new DefaultHttpContext | ||
| { | ||
| RequestServices = services.BuildServiceProvider(), | ||
| }; | ||
|
|
||
| if (session is not null) | ||
| { | ||
| httpContext.Features.Set<ISessionFeature>(new TestSessionFeature(session)); | ||
| } | ||
|
|
||
| httpContext.Features.Set<IHttpResponseFeature>(responseFeature); | ||
| return httpContext; | ||
| } | ||
|
|
||
| private sealed class TestSession : ISession | ||
| { | ||
| private readonly Dictionary<string, byte[]> _store = new(); | ||
|
|
||
| public bool IsAvailable => true; | ||
| public string Id => "test-session"; | ||
| public IEnumerable<string> Keys => _store.Keys; | ||
|
|
||
| public void Clear() => _store.Clear(); | ||
| public Task CommitAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
| public Task LoadAsync(CancellationToken cancellationToken = default) => Task.CompletedTask; | ||
| public void Remove(string key) => _store.Remove(key); | ||
| public void Set(string key, byte[] value) => _store[key] = value; | ||
| public bool TryGetValue(string key, [System.Diagnostics.CodeAnalysis.NotNullWhen(true)] out byte[]? value) => _store.TryGetValue(key, out value); | ||
| } | ||
|
|
||
| private sealed class TestSessionFeature : ISessionFeature | ||
| { | ||
| public TestSessionFeature(ISession session) => Session = session; | ||
|
|
||
| public ISession Session { get; set; } | ||
| } | ||
|
|
||
| private sealed class TestHttpResponseFeature : IHttpResponseFeature | ||
| { | ||
| public int OnStartingCount { get; private set; } | ||
|
|
||
| public int StatusCode { get; set; } = 200; | ||
| public string? ReasonPhrase { get; set; } | ||
| public IHeaderDictionary Headers { get; set; } = new HeaderDictionary(); | ||
| public Stream Body { get; set; } = new MemoryStream(); | ||
| public bool HasStarted { get; set; } | ||
|
|
||
| public void OnCompleted(Func<object, Task> callback, object state) | ||
| { | ||
| } | ||
|
|
||
| public void OnStarting(Func<object, Task> callback, object state) => OnStartingCount++; | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.