ASP.NET core is different from ASP.NET in the past.ASP.NET core is just bunch of libraries; it doesn’t have any host process that comes with it. ASP.NET core is completely decoupled from System.web dll.
This post outlines the following ASP.NET Core 1 features in detail
· ASP.NET Core hosting
· Request handling
· MVC Application Parts
You provide the host process for example when you write .net core application the dotnet.exe is the host process which boots up the .net clr and start running managed code and starts the asp.net hosting logic. You can host asp.net core on a console application. You have flexibility in hosting the asp.net, you can host on console app or windows service. ASP.NET core still supports hosting on IIS and IIS express along with self-hosting using Kestrel and WebListener HTTP servers.
Setting up the ASP.NET Core hosting
Configure the hosting properties – what server you want to use? Microsoft provides http webservers Kestrel and WebListener. You can write your own server if you want to! Specify the server URL and startup logic. There is new concept on paths like Content root and Web root. Content root is where your application files located like your MVC views, Web root is typically all your static files. Web root is relative to the content root.
There is an environment notation for defining development, staging and production etc there is also Application name notation for specifying friendly name to your application.
You can use visual studio or command prompt for setting this environment
You can start new .netcore application by just typing dotnet new; it just creates two files program.cs and project.json file.You can use Visual Studio code for opening these files, to open Visual Studio code type code at command prompt use dotnet run command to run the application
You can build and run the host in program.cs file using the following code
Startup class has methods like configuration building, configuration services that get called at runtime
You can write your own hosting server by implementing IServer interface and hook it up in main method using a useserver property.
You don’t have to write your server, if you want to run your application everything in memory there is a nuget package named Microsoft.AspNetCore.TestHost
Microsoft recommended running Kestrel behind mature webservers like IIS, Nginx, Apache etc. Kestrel is not for internet facing traffic.
.UseIISIntegration property takes the user request and forwards it to Kestrel. In this way you can leverage all the features that IIS giving like windows authentication, logging etc. In High-level ASP.NET Core Module (ACM) will does the work for you
· IIS receives a request
· ACM starts your application in a separate process , this is no longer as w3wp.exe but as dotnet.exe or myapp.exe
· ACM forwards the request to the application
· The app processes the request and sends the response back to ACM
· IIS forwards the response to the client
The ASP.NET Core Module (ACM) configurations file looks as following
The Environment variable set by ACM for the host process
· ASPNETCORE_PORT – Port to forward the request to
· ASPNETCORE_APPL_PATH – Application path to forward the request to
· ASPNETCORE_TOKEN – Pairing token to ensure only local requests from IIS get received
Request handling pipeline gets defined in startup configure method and it uses middleware to handle all requests. Your entire app will built into a single request delegate to encapsulate all middleware that you wanted to use. ASP.NET core comes with built-in middleware like Routing, authentication, static files, diagnostics, error handling, session, CORS, localization etc
Request Delegate looks like following
Middleware takes the Request Delegate and Request Delegate has another delegate pointer to the middleware and returns request delegate with whatever that middleware wants to do
Constructor of the class takes the Request Delegate as parameter and Request Delegate that returned by middleware method get implemented by invoke method. When you write your middleware you will use the extension method UseMiddleware to invoke your middleware.
Routing takes IRouter with context object, when the route logic is done if context object contains a RequestDelegate then it executes that delegate, if it doesn’t find a request delegate then it creates for handled requests, Routing middleware also has RoutingCollection which iterates through a list of IRouters and stops when one handles the request.
Route implements route templates and calls a target IRouter when the request matches.
MVC Application Parts is for extensibility
ApplicationPartManager is the one-stop shop for discovery logic in MVC. Application parts are resources from which to discover features (eg: assemblies). The discover logic includes Controllers, View Components, Tag Helpers, Razor compilation references.
Application Model is a representation of your MVC app that you can read and manipulate controller names, action names, action parameters, attribute routes and filters.
More information on asp.net core can found on following links http://dot.net https://docs.asp.net Samples https://github.com/aspnet
This post is based on Daniel Roth Visual Studio Live Redmond talk