Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,6 @@ public static ISystemWebAdapterBuilder AddHttpApplication<TApp>(this ISystemWebA
return builder;
}

internal static void UseHttpApplication(this IApplicationBuilder app)
{
if (app.AreHttpApplicationEventsRequired())
{
app.UseMiddleware<HttpApplicationMiddleware>();
app.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}
}

internal static void UseHttpApplicationEvent(this IApplicationBuilder app, params ApplicationEvent[] preEvents)
=> app.UseHttpApplicationEvent(preEvents, Array.Empty<ApplicationEvent>());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Web;

using static System.FormattableString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,11 @@ internal static void UseSystemWebAdapterFeatures(this IApplicationBuilder app)
return;
}

app.UseMiddleware<RegisterAdapterFeaturesMiddleware>();
app.UseMiddleware<PreBufferRequestStreamMiddleware>();
app.UseMiddleware<BufferResponseStreamMiddleware>();

app.UseMiddleware<SetDefaultResponseHeadersMiddleware>();
app.UseMiddleware<SingleThreadedRequestMiddleware>();
app.UseMiddleware<CurrentPrincipalMiddleware>();

app.UseHttpApplication();
}

public static void UseSystemWebAdapters(this IApplicationBuilder app)
Expand Down Expand Up @@ -122,6 +118,13 @@ public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.UseMiddleware<SetHttpContextTimestampMiddleware>();
builder.UseMiddleware<RegisterAdapterFeaturesMiddleware>();

if (builder.AreHttpApplicationEventsRequired())
{
builder.UseMiddleware<HttpApplicationMiddleware>();
Copy link
Member Author

Choose a reason for hiding this comment

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

This is being moved so that HttpApplication is available if someone wants to manually call the output caching before we get something in. Otherwise, the call to app.UseOutputCaching() would have to be after app.UseSystemWebAdapters

builder.UseHttpApplicationEvent(ApplicationEvent.BeginRequest);
}

next(builder);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public event System.EventHandler ResolveRequestCache { add { } remove { } }
public event System.EventHandler UpdateRequestCache { add { } remove { } }
public void CompleteRequest() { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
public void Dispose() { }
public virtual string GetVaryByCustomString(System.Web.HttpContext context, string custom) { throw new System.PlatformNotSupportedException("Only supported when running on ASP.NET Core or System.Web");}
}
public sealed partial class HttpApplicationState : System.Collections.Specialized.NameObjectCollectionBase
{
Expand Down
10 changes: 10 additions & 0 deletions src/Microsoft.AspNetCore.SystemWebAdapters/HttpApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public HttpContext Context

public void CompleteRequest() => Context.Response.End();

public virtual string? GetVaryByCustomString(HttpContext context, string custom)
{
if (string.Equals(custom, "browser", StringComparison.OrdinalIgnoreCase))
{
return context?.Request.Browser.Type;
}

return null;
}

public event EventHandler? BeginRequest
{
add => AddEvent(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static async Task<List<string>> RunAsync(string action, string eventName
})
.ConfigureServices(services =>
{
services.AddSingleton<IStartupFilter>(new ModuleTestStartup(notifier, action));
services.AddRouting();
services.AddSystemWebAdapters()
.AddHttpApplication(options =>
Expand All @@ -137,17 +138,6 @@ private static async Task<List<string>> RunAsync(string action, string eventName
{
app.UseRouting();

app.Use(async (ctx, next) =>
{
ctx.Features.Set(notifier);
try
{
await next(ctx);
}
catch (InvalidOperationException) when (action == ModuleTestModule.Throw)
{
}
});
app.UseAuthenticationEvents();
app.UseAuthorizationEvents();
app.UseSystemWebAdapters();
Expand All @@ -168,6 +158,37 @@ private sealed class NotificationCollection : List<string>
{
}

private sealed class ModuleTestStartup : IStartupFilter
{
private readonly NotificationCollection _collection;
private readonly string _action;

public ModuleTestStartup(NotificationCollection collection, string action)
{
_collection = collection;
_action = action;
}

public Action<IApplicationBuilder> Configure(Action<IApplicationBuilder> next)
=> builder =>
{
builder.Use(async (ctx, next) =>
{
ctx.Features.Set(_collection);

try
{
await next(ctx);
}
catch (InvalidOperationException) when (_action == ModuleTestModule.Throw)
{
}
});

next(builder);
};
}

private sealed class ModuleTestModule : EventsModule
{
protected override void InvokeEvent(HttpContext context, string name)
Expand Down