ASP.NET Core in Visual Studio for Mac

So you all heard the news in #Build 2017, Visual Studio on Mac has been fully released and is General Available. To celebrate this release I will write few posts on building some different .net apps on my Mac.

As you already expect from the title, this article will be on ASP.NET Core I will make it short and simple.

The dummy app I will be building will be on Movies. Here is how it looks when you create a new project and pick ASP.NET Core Empty.

Screenshot 2017-05-13 00.53.28.png

After choosing a name and creating the project, Visual Studio just gives you a plain web project with some nugets being restored, on the first run just as expected you get an old pretty Hello World in your default browser.

Screenshot 2017-05-13 01.01.10.png

Now let’s explore the template and understand what we get for free with this Empty template and start building up our application.

As shown in the previous image, we got two files generated for us Program.cs and Startup.cs and the Movies.csproj itself which contains relevant information to tell Visual Studio what to do with this applicaiton.

Screenshot 2017-05-13 01.07.51.png

Now let’s examine the Program.cs file Screenshot 2017-05-13 01.15.35.png

This file actually has a Main method, something we didn’t see for a while in Web projects, ASP.NET Core apps need a host to run. This host implements IWebHost interface, that exposes the features and services needed to start a host.

The lines above first create an instance of the WebHostBuilder, a class helper that builds and configures a host, first configuration stated is UseKestrel is actually telling the host to use Kestrel server, the next configuration UseContentRoot is the telling the host where it should find content files to serve as you can see it takes the current directory as a parameter. UseIISIntegration on the other hand is added to support integration in IIS/IIS Express and is used to specify a reverse proxy to the internal server done by Kestrel. UseStartup tells the host how to configure middleware and services, and as you can see this is the link between the two files, it tells the host to use an instance of the Startup class in the other file. And Finally it builds this host and returns an instance configured for this role.

The next line host.Run() is the one that actually runs the web application and blocks the calling thread until host shutdown. There is also an alternative non-blocking method host.Start().

The next file Startup.cs is the actual ASP.NET configuration that will be used, and we will revisit this file a lot in the subsequent posts and topics.Screenshot 2017-05-13 01.37.20

There is not a lot to describe in this file, it is pretty simple (so far). Two methods are declared one of them for adding services. And the other for intial configuration.

As you can see dependency injection is built into .NET Core and natively supported, if you noticed the using statements, you will the Microsoft.Extensions.DependencyInjection to handle that. So all the parameters values will be located and binded in the runtime behind the scenes, all you have to do is ask for an object implementing an IApplicationBuilder, or IHostingEnvironment or even an ILoggerFactory and you will get a concrete implementation for you to use; there are a lot more like these to come when we explore more and get deeper into ASP.NET Core.

The first line in the method adds a Console logger, on Windows if you run using Kestrel, you would always get a Command window for you and you can see the log traces everytime you do something on the web page. Here on the Mac when using Visual Studio this Logger will be embeded inside the Application Output window.

Screenshot 2017-05-13 01.47.43.png

One intersting thing about ASP.NET Core, that it is really just a Core any service you need to be added or configured, you have to ask for it!! Even the old exception page (the yellow screen of death) which is now a cute light blue screen of death telling you what happend wrong, also not that we only tell the Application Builder to add this DeveloperExceptionPage only in Developement environment, this is a good practice because you don’t want to expose your internal code exceptions in production, you’d probably want to build a custom 500 Error page. Adding this developer exception page is actually adding a “Middleware” to the pipeline, a topic which we will talk about a lot soon.

And the last line is a terminating middleware to the pipeline command, that listens to the requests coming up to this server. It takes a delegate (handler) parameter and as you can see this is the place we are getting “Hello World” from, where we just write a plain text to the Repsonse of the current context.

That was all for this post, very basic no code was actually written, but I believe this post seves as an intrduction to ASP.NET Core and a beginner to the upcoming posts.

Leave a Reply

Your email address will not be published. Required fields are marked *