Hosting is how you get you ASP.NET application up and running. And ASP.NET Core is just bunch of libraries host in you own process. It’s not provide a hosting – user have to take care of providing hosting process.
That gives us lot of possibilities and flexibility. Such application can be hosted in console application, windows service or everywhere you want – all you need to do is to build a host, get it started and start listening for requests. Bellow I will try to show you couple of examples of that.
Build the host
In few words all we need to do is to:
- Setup host services – definition of the features which you server will provide
- Configure a server – what server do we want to use
- Identify startup logic – setup of all core hosting services (for example logger, hosting environment)
- Build the app RequestDelagate – host will take the request pipeline logic defined in the startup logic and turn it into the Request Delegate. Request Delegate is the core abstraction of ASP.NET Core – it take the HTTP request and does something asynchronously with it.
Run the host
That is also quite simple, just:
- Start the server from hosted app – simply start listening for requests
- Handle requests using app RequestDelegate
Configuring hosting properties
There is couple of thing which have to be set up before we will be able to start our server:
- Server – we have to decide if will use Kestrel, Web Listener or you will write your own server
- Server URLs – address we want our server to listen on
- Startup – here will be our startup logic
- Web root – this is the place where we want static files middleware to look to expose static content
- Environment – development, integration or production – that’s just a string
- Application name – the name of the application
How it looks like in code – for that you can create an empty ASP.NET Core project in Visual Studio and prepare something like that:
All you need to do is just to start the application and start listening for requests!
What will be done with the HTTP requests depends on the Startup class. Bellow is the example of very simple Startup class which is writing “Hello world!” message into the output.