|
5 | 5 | using System;
|
6 | 6 | using System.Net.Http;
|
7 | 7 | using System.Threading.Tasks;
|
| 8 | +using System.Web; |
8 | 9 | using System.Web.SessionState;
|
9 | 10 | using Microsoft.AspNetCore.Builder;
|
10 | 11 | using Microsoft.AspNetCore.Hosting;
|
@@ -52,6 +53,84 @@ public async Task TestOverrideSessionStateBehavior(string endpoint, string expec
|
52 | 53 | Assert.Equal(expected, actual);
|
53 | 54 | }
|
54 | 55 |
|
| 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 | + |
55 | 134 | private static async Task<string> GetAsync(string endpoint)
|
56 | 135 | {
|
57 | 136 | using var host = await new HostBuilder()
|
|
0 commit comments