Skip to content

Expose Use as a public method on WebApplication #38011

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

Merged
merged 1 commit into from
Nov 2, 2021
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
1 change: 1 addition & 0 deletions src/DefaultBuilder/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ Microsoft.AspNetCore.Builder.WebApplication.Services.get -> System.IServiceProvi
Microsoft.AspNetCore.Builder.WebApplication.StartAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Microsoft.AspNetCore.Builder.WebApplication.StopAsync(System.Threading.CancellationToken cancellationToken = default(System.Threading.CancellationToken)) -> System.Threading.Tasks.Task!
Microsoft.AspNetCore.Builder.WebApplication.Urls.get -> System.Collections.Generic.ICollection<string!>!
Microsoft.AspNetCore.Builder.WebApplication.Use(System.Func<Microsoft.AspNetCore.Http.RequestDelegate!, Microsoft.AspNetCore.Http.RequestDelegate!>! middleware) -> Microsoft.AspNetCore.Builder.IApplicationBuilder!
Microsoft.AspNetCore.Builder.WebApplicationBuilder
Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build() -> Microsoft.AspNetCore.Builder.WebApplication!
Microsoft.AspNetCore.Builder.WebApplicationBuilder.Configuration.get -> Microsoft.Extensions.Configuration.ConfigurationManager!
Expand Down
7 changes: 6 additions & 1 deletion src/DefaultBuilder/src/WebApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,12 @@ IApplicationBuilder IApplicationBuilder.New()
return newBuilder;
}

IApplicationBuilder IApplicationBuilder.Use(Func<RequestDelegate, RequestDelegate> middleware)
/// <summary>
/// Adds the middleware to the application request pipeline.
/// </summary>
/// <param name="middleware">The middleware.</param>
/// <returns>An instance of <see cref="IApplicationBuilder"/> after the operation has completed.</returns>
public IApplicationBuilder Use(Func<RequestDelegate, RequestDelegate> middleware)
{
ApplicationBuilder.Use(middleware);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1835,6 +1835,26 @@ public void ConfigurationProviderTypesArePreserved()
Assert.Single(((IConfigurationRoot)app.Configuration).Providers.OfType<RandomConfigurationProvider>());
}

[Fact]
public async Task CanUseMiddleware()
{
var builder = WebApplication.CreateBuilder();
builder.WebHost.UseTestServer();
await using var app = builder.Build();

app.Use(next =>
{
return context => context.Response.WriteAsync("Hello World");
});

await app.StartAsync();

var client = app.GetTestClient();

var response = await client.GetStringAsync("/");
Assert.Equal("Hello World", response);
}

public class RandomConfigurationSource : IConfigurationSource
{
public int ProvidersBuilt { get; set; }
Expand Down