Skip to content

Add CreateScopeForStatusCodePages property and UseStatusCodePagesWithReExecute overload #62768

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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 .azure/pipelines/ci-public.yml
Original file line number Diff line number Diff line change
Expand Up @@ -638,7 +638,7 @@ stages:
timeoutInMinutes: 240
steps:
- script: git submodule update --init
displayName: Update submodules
displayName: Update submodules ci-public
- script: ./restore.cmd
displayName: Run restore.cmd
- powershell: ./eng/build.ps1 -all -noBuildJava -noBuildNodeJS "-WarnAsError:false"
Expand Down
15 changes: 14 additions & 1 deletion .azure/pipelines/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,7 @@ extends:
timeoutInMinutes: 240
steps:
- script: git submodule update --init
displayName: Update submodules
displayName: Update submodules ci
- script: ./restore.cmd
displayName: Run restore.cmd
- script: ./eng/build.cmd -all -noBuildJava -noBuildNodeJS
Expand All @@ -708,6 +708,19 @@ extends:
-NoBuildDeps
-configuration Release
displayName: Run project template tests
- script: |
echo "Available templates:"
dotnet new list
echo ""
echo "Template package locations:"
dotnet new search --columns-all | findstr /i "template"
echo ""
echo "Template cache location:"
echo %USERPROFILE%\.templateengine
if exist "%USERPROFILE%\.templateengine" (
dir "%USERPROFILE%\.templateengine" /s /b
)
displayName: Check available templates and locations
- powershell: . ./activate.ps1; ./src/ProjectTemplates/scripts/Run-BlazorWeb-Locally.ps1 -Verbose
displayName: Run Blazor web app test script

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseExceptionHandler("/Error", createScopeForErrors: true);
}

reexecutionApp.UseStatusCodePagesWithReExecute("/not-found-reexecute", createScopeForErrors: true);
reexecutionApp.UseStatusCodePagesWithReExecute("/not-found-reexecute", createScopeForStatusCodePages: true);
reexecutionApp.UseStaticFiles();
reexecutionApp.UseRouting();
RazorComponentEndpointsStartup<TRootComponent>.UseFakeAuthState(reexecutionApp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
await context.Response.WriteAsync("Triggered a 404 status code.");
});
});
reexecutionApp.UseStatusCodePagesWithReExecute("/not-found-reexecute", createScopeForErrors: true);
reexecutionApp.UseStatusCodePagesWithReExecute("/not-found-reexecute", createScopeForStatusCodePages: true);
reexecutionApp.UseRouting();

reexecutionApp.UseAntiforgery();
Expand Down
8 changes: 5 additions & 3 deletions src/Middleware/Diagnostics/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#nullable enable
Microsoft.AspNetCore.Builder.ExceptionHandlerOptions.SuppressDiagnosticsCallback.get -> System.Func<Microsoft.AspNetCore.Diagnostics.ExceptionHandlerSuppressDiagnosticsContext!, bool>?
Microsoft.AspNetCore.Builder.ExceptionHandlerOptions.SuppressDiagnosticsCallback.set -> void
Microsoft.AspNetCore.Builder.StatusCodePagesOptions.CreateScopeForErrors.get -> bool
Microsoft.AspNetCore.Builder.StatusCodePagesOptions.CreateScopeForErrors.set -> void
Microsoft.AspNetCore.Builder.StatusCodePagesOptions.CreateScopeForStatusCodePages.get -> bool
Microsoft.AspNetCore.Builder.StatusCodePagesOptions.CreateScopeForStatusCodePages.set -> void
Microsoft.AspNetCore.Diagnostics.ExceptionHandledType
Microsoft.AspNetCore.Diagnostics.ExceptionHandledType.ExceptionHandlerDelegate = 3 -> Microsoft.AspNetCore.Diagnostics.ExceptionHandledType
Microsoft.AspNetCore.Diagnostics.ExceptionHandledType.ExceptionHandlerService = 1 -> Microsoft.AspNetCore.Diagnostics.ExceptionHandledType
Expand All @@ -17,4 +17,6 @@ Microsoft.AspNetCore.Diagnostics.ExceptionHandlerSuppressDiagnosticsContext.Exce
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerSuppressDiagnosticsContext.ExceptionHandlerSuppressDiagnosticsContext() -> void
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerSuppressDiagnosticsContext.HttpContext.get -> Microsoft.AspNetCore.Http.HttpContext!
Microsoft.AspNetCore.Diagnostics.ExceptionHandlerSuppressDiagnosticsContext.HttpContext.init -> void
static Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithReExecute(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, string! pathFormat, bool createScopeForErrors, string? queryFormat = null) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
static Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithReExecute(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, string! pathFormat, string? queryFormat = null, bool createScopeForStatusCodePages = false) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
*REMOVED*static Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithReExecute(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, string! pathFormat, string? queryFormat = null) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
static Microsoft.AspNetCore.Builder.StatusCodePagesExtensions.UseStatusCodePagesWithReExecute(this Microsoft.AspNetCore.Builder.IApplicationBuilder! app, string! pathFormat, string! queryFormat) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public static IApplicationBuilder UseStatusCodePages(this IApplicationBuilder ap
public static IApplicationBuilder UseStatusCodePagesWithReExecute(
this IApplicationBuilder app,
string pathFormat,
string? queryFormat = null)
string queryFormat)
{
ArgumentNullException.ThrowIfNull(app);

Expand All @@ -168,15 +168,15 @@ public static IApplicationBuilder UseStatusCodePagesWithReExecute(
/// </summary>
/// <param name="app"></param>
/// <param name="pathFormat"></param>
/// <param name="createScopeForErrors">Whether or not to create a new <see cref="IServiceProvider"/> scope.</param>
/// <param name="queryFormat"></param>
/// <param name="createScopeForStatusCodePages">Whether or not to create a new <see cref="IServiceProvider"/> scope.</param>
/// <returns></returns>
[SuppressMessage("ApiDesign", "RS0026:Do not add multiple overloads with optional parameters", Justification = "Required to maintain compatibility")]
public static IApplicationBuilder UseStatusCodePagesWithReExecute(
this IApplicationBuilder app,
string pathFormat,
bool createScopeForErrors,
string? queryFormat = null)
string? queryFormat = null,
bool createScopeForStatusCodePages = false)
{
ArgumentNullException.ThrowIfNull(app);

Expand All @@ -190,7 +190,7 @@ public static IApplicationBuilder UseStatusCodePagesWithReExecute(
Options.Create(new StatusCodePagesOptions()
{
HandleAsync = CreateHandler(pathFormat, queryFormat, newNext),
CreateScopeForErrors = createScopeForErrors,
CreateScopeForStatusCodePages = createScopeForStatusCodePages,
PathFormat = pathFormat
})).Invoke;
});
Expand All @@ -199,7 +199,7 @@ public static IApplicationBuilder UseStatusCodePagesWithReExecute(
var options = new StatusCodePagesOptions
{
HandleAsync = CreateHandler(pathFormat, queryFormat),
CreateScopeForErrors = createScopeForErrors,
CreateScopeForStatusCodePages = createScopeForStatusCodePages,
PathFormat = pathFormat
};
var wrappedOptions = new OptionsWrapper<StatusCodePagesOptions>(options);
Expand All @@ -222,8 +222,8 @@ private static Func<StatusCodeContext, Task> CreateHandler(string pathFormat, st
var originalQueryString = context.HttpContext.Request.QueryString;

var routeValuesFeature = context.HttpContext.Features.Get<IRouteValuesFeature>();
var oldScope = context.Options.CreateScopeForErrors ? context.HttpContext.RequestServices : null;
await using AsyncServiceScope? scope = context.Options.CreateScopeForErrors
var oldScope = context.Options.CreateScopeForStatusCodePages ? context.HttpContext.RequestServices : null;
await using AsyncServiceScope? scope = context.Options.CreateScopeForStatusCodePages
? context.HttpContext.RequestServices.GetRequiredService<IServiceScopeFactory>().CreateAsyncScope()
: null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private static string BuildResponseBody(int httpStatusCode)
/// replace it on <see cref="HttpContext.RequestServices"/> when re-executing the request.
/// </summary>
/// <remarks>The default value is <see langword="false"/>.</remarks>
public bool CreateScopeForErrors { get; set; }
public bool CreateScopeForStatusCodePages { get; set; }

internal string? PathFormat { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static void Main(string[] args)
#endif
}

app.UseStatusCodePagesWithReExecute("/not-found", createScopeForErrors: true);
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);

#if (HasHttpsProfile)
app.UseHttpsRedirection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
app.UseHsts();
#endif
}
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForErrors: true);
app.UseStatusCodePagesWithReExecute("/not-found", createScopeForStatusCodePages: true);

#if (HasHttpsProfile)
app.UseHttpsRedirection();
Expand Down
2 changes: 1 addition & 1 deletion src/ProjectTemplates/scripts/Run-BlazorWeb-Locally.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@
$templateArguments += "--use-program-main"
}

Import-Module -Name "$PSScriptRoot/Test-Template.psm1";
Import-Module -Name "$PSScriptRoot/Test-Template.psm1" -Force;

Test-Template `
-TemplateName "MyBlazorApp" `
Expand Down
17 changes: 11 additions & 6 deletions src/ProjectTemplates/scripts/Test-Template.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ function Test-Template {
$env:DOTNET_ROOT = "$PSScriptRoot/.dotnet";
$env:DOTNET_ROOT_X86 = "$PSScriptRoot/.dotnet";
$env:Path = "$PSScriptRoot/.dotnet;$env:Path";
Write-Verbose "PATH: $env:Path";
Write-Verbose "DOTNET_ROOT: $env:DOTNET_ROOT";
Write-Verbose "Running dotnet --info";
dotnet --info;
$tmpDir = "$PSScriptRoot/$templateName";
Remove-Item -Path $tmpDir -Recurse -ErrorAction Ignore;
Push-Location ..;
Expand All @@ -50,11 +54,12 @@ function Test-Template {

$PackageName = (Get-Item $PackagePath).Name;

if (-not (Test-Path "$($env:USERPROFILE)/.templateengine/packages/$PackageName")) {
Write-Verbose "Installing package from $PackagePath";
dotnet new install $PackagePath;
}
else {
# if (-not (Test-Path "$($env:USERPROFILE)/.templateengine/packages/$PackageName")) {
# Write-Verbose "Installing package from $PackagePath";
# dotnet new install $PackagePath;
# }
# else {
# always try to uninstall first
Write-Verbose "Uninstalling package from $PackagePath";
if (-not ($PackageName -match $PackagePattern)) {
Write-Error "$PackageName did not match $PackagePattern";
Expand All @@ -66,7 +71,7 @@ function Test-Template {

Write-Verbose "Installing package from $PackagePath";
dotnet new install $PackagePath;
}
# }


Write-Verbose "Creating directory $tmpDir"
Expand Down
Loading