Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of IApplicationBuilder.New()

In the new ASP.NET 5.0 (vNext), the startup code relies on the IApplicationBuilder interface. The Use method is used to add a handler to the builder, while Build is used to construct the final delegate. But I can't figure out what is the purpose of New. I've been digging in GitHub, but can't find any place where that's used.

Anyone understand what is the purpose of that method?

like image 354
Flavien Avatar asked Jul 06 '15 20:07

Flavien


1 Answers

New() creates a second ApplicationBuilder, sharing all the ApplicationServices and ServerFeatures of the first one, but none of the middleware. It is used internally by the branching extensions (Map, MapWhen, UseWhen) to create the new 'branch'.

You can find the implementation here: ApplicationBuilder.cs.

In some cases, it is also useful in higher-level frameworks.

For exemple, the [MiddlewareFilter] attribute in MVC Core uses New() internally to execute a piece of ASP.NET Core middleware inside the MVC framework (i.e. as a filter). MVC Core creates a small pipeline around the middleware, builds it into a RequestDelegate, then runs the HttpContext through it. Just like ASP.NET Core does with your 'main' pipeline built in Startup.cs.

Thanks to this feature, we can reuse a piece of general-purpose ASP.NET Core middleware, from inside MVC.

For more information, see MiddlewareFilterBuilder.cs in ASP.NET MVC Core.

like image 180
Mathieu Renda Avatar answered Oct 11 '22 23:10

Mathieu Renda