What is the purpose of IApplicationBuilder.New ()

In the new ASP.NET 5.0 (vNext) startup code depends on the IApplicationBuilder interface . The Use method is used to add a handler to the constructor, and Build used to build the final delegate. But I can’t understand what the purpose of New . I have been digging on GitHub but cannot find the place where it was used.

Does anyone understand what the purpose of this method is?

+5
source share
2 answers

It seems that there is a branch [clone] of the original instance (as shown in src/Microsoft.AspNet.Http/Extensions/MapExtensions.cs ). There was also the previous MapWhenExtensions.cs , but it seems to have been removed from the dev branch.)

I suspect this is an artifact of the previous design, which will provide the ability to link middleware based on circumstances without affecting the root configuration. The fact that it was since then until IBuilder was reorganized into IApplicationBuilder and that most of the dependencies were in files that have since been removed from the dev branch, I would venture to assume that this is old news.

Of course, it is difficult to say that neither the interface nor the base implementation are commented on.

+3
source

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

You can find the implementation here: ApplicationBuilder.cs .

In some cases, it is also useful at a higher level.

For example, the [MiddlewareFilter] attribute in MVC Core uses New() internally to execute part of the core ASP.NET middleware inside the MVC environment (that is, as a filter). MVC Core creates a small pipeline around the middleware, builds it in RequestDelegate, and then runs the HttpContext through it. Like ASP.NET Core, your main pipeline is built in Startup.cs .

With this feature, we can reuse part of the general purpose ASP.NET Core middleware from within MVC.

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

+2
source

All Articles