Examining application Startup in ASP. 5

Source: Internet
Author: User

by Steve Smith June

ASP. 5 differs from previous versions of ASP. Many ways. Gone is the default ASP. NET event life cycle, and along with it, the Global.asax file (which itself were an evolved version of Global.asa from the ASP days). Also Gone is the xml-based Web. config file, in most cases, though your may still find it making appearances in your Wwwroot folder in some cases.

Note: the official ASP. NET 5 Documentation is still under development. It ' s being developed using github and open source, And falafel Software is One of the official contrib Utors. Some of the links in this article be still under construction, but you should see them fill out by the time ASP 5 is Officially released. A good place-to-get started with this topic are the application startup article that was just published.

ASP. 5 's startup system is heavily influenced by prior work in The owin space, Including project Kata Na. In this new paradigm, the web host environment (not necessarily IIS) would search the application for a starting point, and Typically this is a class called startup located in the root of the application. At a minimum, this class needs to has a method called configure (), which the hosting environment would call to Config Ure the application ' s request pipeline. If you ' re familiar with ASP. NET ' s HTTP handlers and modules, you can think of the request delegates that is specified within theconfigure ()  meth OD as being similar to a combination of these and concepts. Collectively, such request delegates is referred to As middleware.

have complete and granular access to the HTTP request delegate pipeline allows applications to being constructed with Ju St The features and components they require, and nothing more. It ' s extremely lean, composable, and more secure and higher performance by default because only those features that is re Quired is included. This reduces the overall application surface area exposed to attackers, and reduces the work that must is done on each req Uest to the bare minimum.

The request delegate pipeline is constructed as a series of delegates, which can be written simply as Lambda expressions O R encapsulated within their own classes. Each delegate can perform some work, call the next delegate, and then does some more work once that call completes. Thus, it's possible to wrap later delegates within earlier ones, which are important for scenarios like authentication and Error handling. At any given point within the pipeline, a delegate can choose isn't to call the next delegate, even if one was defined, allow ing the pipeline to be short-circuited. Thus, the order in which delegates was wired up and the pipeline is very important.

Consider this sample method from the Default Web site template (in VS2015 RC):

 Public voidConfigure (Iapplicationbuilder app, Ihostingenvironment env, iloggerfactory loggerfactory) {if(Env. Isenvironment ("Development") ) {app.        Usebrowserlink (); App.        Useerrorpage (Errorpageoptions.showall); App.    Usedatabaseerrorpage (Databaseerrorpageoptions.showall); }    Else    {        //Add Error handling middleware which catches all application specific errors and//sends the request to the following path or controller action.App. Useerrorhandler ("/home/error"); }    //Add static files to the request pipeline.app.    Usestaticfiles (); //ADD cookie-based Authentication to the request pipeline.app.    Useidentity (); //ADD authentication middleware to the request pipeline.    You can configure options such as Id and Secret in the Configureservices method. //For more information seehttp://go.microsoft.com/fwlink/?LinkID=532715    //app. Usefacebookauthentication ();app. USEMVC (Routes={routes. MapRoute (Name:"default", Template:"{controller}/{action}/{id?}", defaults:New{controller ="Home", action ="Index" }); });}

In this example, the pipeline are configured slightly differently in a development environment vs. in production. In development, the application wires up browserlink (for use with Visual Studio) as well as helpful error pages that Shou LD not being deployed to production. In production, a simple error handler page is configured. Next, the application is configured-to-support static files, and then to use the ASP. NET Identity for authentication. Note that since authentication are configured after static files, it would not protect static files (nor would static files I Ncur overhead from checking authentication). Finally, ASP. NET MVC is configured, along with a default route. In each case, a simple useWhatever() method is used-to-wire-up the pipeline methods, but under the covers these methods was adding request delegates to the Application ' s pipeline. Although the Configure () method is called when the application starts up and the request delegates that was wired up here is Not–they was called on every individual HTTP request that was made to the application.

In addition to Configure, can optionally specify a method called Configureservices, which would Configure the Defaul T Services container (IoC container) for ASP. This allows for dependency injection, which helps ensure your application remains loosely coupled from its underlying implementation. Your controllers, services, and other application classes should try-to-follow the solid principles, as well as THE&N Bsp Explicit Dependencies Principle, which'll help to ensure you don ' t end up with a brittle, tightly coupled implementation . These principles has been followed by ASP. Note that your Startup class doesn ' t depend on any particular base class, nor does it refer to any particular Implementati On a Web server. Even the parameters it can have injected into it is all interfaces, not concrete types, Allowing multiple Web server S to be supported, even across multiple platforms.

can see how ASP. NET 5 ' s hosting implementation loads an individual application by examining the microsoft.aspn Et. Hosting Package–it ' s open source on GitHub. Specifically, you can see how a application is created and their services registered by examining The hostingengine CL which the Configure class inside Its buildapplication method. The application ' s Startup class has its methods mapped to a Startupmethods class By startuploader, which allows Hosti Ngengine to call these methods without have any knowledge of your application ' s actual Startup class ' s type.

Take a look at how these-classes, Startuploader and Hostingengine, work to start and configure your application. This was fundamental to understanding how ASP. NET 5 applications work at the very basic level. With ASP. 5 being developed completely as open source on GitHub, we had much greater ability to examine and understand The underlying design decisions and plumbing that underpins the applications we build on this foundation. Use the-your advantage and make sure you can follow what the the the classes above was doing when your application was Laun Ched. Do not expect that your understanding of ASP. From prior versions would serve you well–this version was a huge change f Rom ASP. V1-v4. It's not quite as the big a change as from the classic ASP to Asp+/asp.net, but it's close.

Examining application Startup in ASP. 5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.