Skip to content

Commit 94b25ef

Browse files
committed
Ensure HttpApplicaiton raises SessionStart
This fixes two issues that combined to not raise this event: - A typo sent the event SessionEnd instead of SessionStart - Wrapped ASP.NET Core session didn't implement the IsNewSession - it was always false. Now we check if there are any keys to determine if it is a new session
1 parent 9e50058 commit 94b25ef

File tree

3 files changed

+82
-2
lines changed

3 files changed

+82
-2
lines changed

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/SessionEventsMiddleware.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public async Task InvokeAsync(HttpContextCore context)
2424

2525
if (session is { IsNewSession: true })
2626
{
27-
await app.RaiseEventAsync(ApplicationEvent.SessionEnd);
27+
await app.RaiseEventAsync(ApplicationEvent.SessionStart);
2828
}
2929

3030
await _next(context);

src/Microsoft.AspNetCore.SystemWebAdapters.CoreServices/SessionState/Wrapped/AspNetCoreSessionState.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public AspNetCoreSessionState(ISession session, ISessionKeySerializer serializer
2626
_throwOnUnknown = throwOnUnknown;
2727
_logger = factory.CreateLogger<AspNetCoreSessionState>();
2828

29+
IsNewSession = !session.Keys.Any();
2930
IsReadOnly = isReadOnly;
3031
}
3132

@@ -99,7 +100,7 @@ public object? this[string key]
99100

100101
public int Timeout { get; set; } = 20;
101102

102-
public bool IsNewSession => false;
103+
public bool IsNewSession { get; }
103104

104105
public int Count => _session.Keys.Count();
105106

test/Microsoft.AspNetCore.SystemWebAdapters.CoreServices.Tests/SessionState/SessionIntegrationTests.cs

+79
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System;
66
using System.Net.Http;
77
using System.Threading.Tasks;
8+
using System.Web;
89
using System.Web.SessionState;
910
using Microsoft.AspNetCore.Builder;
1011
using Microsoft.AspNetCore.Hosting;
@@ -52,6 +53,84 @@ public async Task TestOverrideSessionStateBehavior(string endpoint, string expec
5253
Assert.Equal(expected, actual);
5354
}
5455

56+
[InlineData(true)]
57+
[InlineData(false)]
58+
[Theory]
59+
public async Task SessionStartEvent(bool abandon)
60+
{
61+
// Arrange
62+
using var host = await new HostBuilder()
63+
.ConfigureWebHost(webBuilder =>
64+
{
65+
webBuilder
66+
.UseTestServer(options =>
67+
{
68+
options.AllowSynchronousIO = true;
69+
})
70+
.ConfigureServices(services =>
71+
{
72+
services.AddRouting();
73+
services.AddControllers();
74+
services.AddSystemWebAdapters()
75+
.AddHttpApplication<SessionApplication>()
76+
.AddWrappedAspNetCoreSession();
77+
78+
services.AddDistributedMemoryCache();
79+
})
80+
.Configure(app =>
81+
{
82+
app.UseRouting();
83+
app.UseSession();
84+
app.UseSystemWebAdapters();
85+
86+
if (abandon)
87+
{
88+
app.Use((ctx, next) =>
89+
{
90+
ctx.AsSystemWeb().Session!.Abandon();
91+
return next(ctx);
92+
});
93+
}
94+
95+
app.UseEndpoints(endpoints =>
96+
{
97+
endpoints.MapGet("/", ctx => Task.CompletedTask)
98+
.RequireSystemWebAdapterSession();
99+
});
100+
});
101+
})
102+
.StartAsync();
103+
104+
// Act
105+
var result = await host.GetTestClient().GetStringAsync(new Uri("/", UriKind.Relative));
106+
107+
// Assert
108+
if (abandon)
109+
{
110+
Assert.Equal("SessionStart::SessionEnd", result);
111+
}
112+
else
113+
{
114+
115+
Assert.Equal("SessionStart::", result);
116+
}
117+
118+
await host.StopAsync();
119+
}
120+
121+
private class SessionApplication : HttpApplication
122+
{
123+
protected void Session_Start(Object sender, EventArgs e)
124+
{
125+
HttpContext.Current?.Response.Write("SessionStart::");
126+
}
127+
128+
protected void Session_End(Object sender, EventArgs e)
129+
{
130+
HttpContext.Current?.Response.Write("SessionEnd");
131+
}
132+
}
133+
55134
private static async Task<string> GetAsync(string endpoint)
56135
{
57136
using var host = await new HostBuilder()

0 commit comments

Comments
 (0)