Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

<PropertyGroup>
<Nullable>enable</Nullable>
<LangVersion>11</LangVersion>
<LangVersion>12</LangVersion>
</PropertyGroup>

<PropertyGroup>
Expand Down
8 changes: 5 additions & 3 deletions samples/Modules/ModulesFramework/Handler.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ModulesFramework
Expand All @@ -11,6 +8,11 @@ public class Handler : IHttpHandler

public void ProcessRequest(HttpContext context)
{
context.Response.Write($"State: {context.CurrentNotification}\n");
context.Response.Flush();
context.Response.Write($"State: {context.CurrentNotification}\n");
context.Response.Flush();
context.Response.Write($"State: {context.CurrentNotification}\n");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ namespace Microsoft.AspNetCore.SystemWebAdapters.Features;

internal sealed class HttpApplicationFeature : IHttpApplicationFeature, IHttpResponseEndFeature, IRequestExceptionFeature, IDisposable
{
private static readonly HashSet<ApplicationEvent> _suppressThrow =
[
ApplicationEvent.LogRequest,
ApplicationEvent.PostLogRequest,
ApplicationEvent.EndRequest,
ApplicationEvent.PreSendRequestHeaders,
];

private readonly IHttpResponseEndFeature _previous;
private readonly ObjectPool<HttpApplication> _pool;

Expand All @@ -22,6 +30,13 @@ public HttpApplicationFeature(HttpContextCore context, IHttpResponseEndFeature p
_contextOrApplication = context;
_pool = pool;
_previous = previousEnd;

context.Response.OnStarting(static state =>
{
var context = (HttpContextCore)state;

return context.Features.GetRequired<IHttpApplicationFeature>().RaiseEventAsync(ApplicationEvent.PreSendRequestHeaders).AsTask();
}, context);
}

public RequestNotification CurrentNotification { get; set; }
Expand All @@ -48,12 +63,12 @@ private HttpApplication InitializeApplication(HttpContextCore context)

ValueTask IHttpApplicationFeature.RaiseEventAsync(ApplicationEvent @event)
{
RaiseEvent(@event, suppressThrow: false);
RaiseEvent(@event);
return ValueTask.CompletedTask;
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Must handle all exceptions here")]
private void RaiseEvent(ApplicationEvent appEvent, bool suppressThrow)
private void RaiseEvent(ApplicationEvent appEvent)
{
(CurrentNotification, IsPostNotification) = appEvent switch
{
Expand All @@ -77,6 +92,7 @@ private void RaiseEvent(ApplicationEvent appEvent, bool suppressThrow)
ApplicationEvent.LogRequest => (RequestNotification.LogRequest, false),
ApplicationEvent.PostLogRequest => (RequestNotification.LogRequest, true),
ApplicationEvent.EndRequest => (RequestNotification.EndRequest, false),
ApplicationEvent.ExecuteRequestHandler => (RequestNotification.ExecuteRequestHandler, false),

// Remaining events just continue using the existing notifications
_ => (CurrentNotification, IsPostNotification),
Expand All @@ -91,7 +107,7 @@ private void RaiseEvent(ApplicationEvent appEvent, bool suppressThrow)
AddError(ex);
Application.InvokeEvent(ApplicationEvent.Error);

if (!suppressThrow)
if (!_suppressThrow.Contains(appEvent))
{
ThrowIfErrors();
}
Expand Down Expand Up @@ -119,10 +135,9 @@ async Task IHttpResponseEndFeature.EndAsync()

IsEnded = true;

RaiseEvent(ApplicationEvent.LogRequest, suppressThrow: true);
RaiseEvent(ApplicationEvent.PostLogRequest, suppressThrow: true);
RaiseEvent(ApplicationEvent.EndRequest, suppressThrow: true);
RaiseEvent(ApplicationEvent.PreSendRequestHeaders, suppressThrow: true);
RaiseEvent(ApplicationEvent.LogRequest);
RaiseEvent(ApplicationEvent.PostLogRequest);
RaiseEvent(ApplicationEvent.EndRequest);

await _previous.EndAsync();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public static void UseSystemWebAdapters(this IApplicationBuilder app)
}

app.UseHttpApplicationEvent(
preEvents: new[] { ApplicationEvent.PreRequestHandlerExecute },
preEvents: new[] { ApplicationEvent.PreRequestHandlerExecute, ApplicationEvent.ExecuteRequestHandler },
postEvents: new[] { ApplicationEvent.PostRequestHandlerExecute });
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ public enum ApplicationEvent
SessionEnd,

Disposed,

ExecuteRequestHandler,
}

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<PackageReference Include="AutoFixture" Version="4.15.0" />
<PackageReference Include="Moq" Version="4.16.1" />
<PackageReference Include="Microsoft.AspNetCore.TestHost" Version="6.0.13" />
<PackageReference Include="System.Collections.Immutable" Version="8.0.0" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated to your PR, but we should think about setting up dependabot or some other way to automatically bump these dependencies in the future.

</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\src\Microsoft.AspNetCore.SystemWebAdapters.CoreServices\Microsoft.AspNetCore.SystemWebAdapters.CoreServices.csproj" />
Expand Down
Loading