Skip to content

Commit

Permalink
App startup topic updates (dotnet#8422)
Browse files Browse the repository at this point in the history
  • Loading branch information
guardrex authored Sep 6, 2018
1 parent 448b927 commit 89071cf
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
6 changes: 3 additions & 3 deletions aspnetcore/fundamentals/startup.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ For more information on how to use `IApplicationBuilder` and the order of middle

[!code-csharp[](startup/snapshot_sample/Program.cs?highlight=18,22)]

## Startup filters
## Extend Startup with startup filters

Use [IStartupFilter](/dotnet/api/microsoft.aspnetcore.hosting.istartupfilter) to configure middleware at the beginning or end of an app's [Configure](#the-configure-method) middleware pipeline. `IStartupFilter` is useful to ensure that a middleware runs before or after middleware added by libraries at the start or end of the app's request processing pipeline.

Expand All @@ -96,9 +96,9 @@ The `RequestSetOptionsMiddleware` is configured in the `RequestSetOptionsStartup

[!code-csharp[](startup/sample/RequestSetOptionsStartupFilter.cs?name=snippet1&highlight=7)]

The `IStartupFilter` is registered in the service container in `ConfigureServices`:
The `IStartupFilter` is registered in the service container in [IWebHostBuilder.ConfigureServices](xref:Microsoft.AspNetCore.Hosting.IWebHostBuilder.ConfigureServices*) to demonstrate how the startup filter augments `Startup` from outside of the `Startup` class:

[!code-csharp[](startup/sample/Startup.cs?name=snippet1&highlight=3)]
[!code-csharp[](startup/sample/Program.cs?name=snippet1&highlight=4-5)]

When a query string parameter for `option` is provided, the middleware processes the value assignment before the MVC middleware renders the response:

Expand Down
8 changes: 8 additions & 0 deletions aspnetcore/fundamentals/startup/sample/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;

namespace StartupFilterSample
{
Expand All @@ -11,8 +12,15 @@ public static void Main(string[] args)
}

public static IWebHost BuildWebHost(string[] args) =>
#region snippet1
WebHost.CreateDefaultBuilder(args)
.ConfigureServices(services =>
{
services.AddTransient<IStartupFilter,
RequestSetOptionsStartupFilter>();
})
.UseStartup<Startup>()
.Build();
#endregion
}
}
3 changes: 0 additions & 3 deletions aspnetcore/fundamentals/startup/sample/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,10 @@ namespace StartupFilterSample
{
public class Startup
{
#region snippet1
public void ConfigureServices(IServiceCollection services)
{
services.AddTransient<IStartupFilter, RequestSetOptionsStartupFilter>();
services.AddMvc();
}
#endregion

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
Expand Down

0 comments on commit 89071cf

Please sign in to comment.