Mike Tolland

Kestrel Web Server

The first step for developing this Electron App is developing the web server to host the backend code. This would be rest calls, websockets, and static file serving. To do this we must first install dot net core. To do so go to http://dot.net and follow the instructions to install on your machine.

Once installed create a new project using

dotnet new

Setup your project.json like the following :

One important thing to note about the project.json is the removal of

type:"platform"

This exists under the Microsoft.NETCore.App dependency normally but in order to create an executable you need to remove this line. Next you want to add

"runtimes": {
    "win7-x64": {},
    "ubuntu.16.04-x64": {}
}

This will be important for building an executable that works for windows 7 and ubuntu. You will want to add extra runtimes or Runtime Identifiers for each platform. You can find the RIDs here. After we have the project json setup you will want to run the command

dotnet restore

This will restore the dependencies in the project.json allowing you to build the project.

Next we have to setup the csharp files. I made up two files shown below:

Program.cs

Server.cs

Startup.cs

I like to separate out functionality from the entry class. Which in this instance is Program.cs as you can see I moved most of the code to the Server.cs. Server.cs builds the kestrel server setting the url to localhost and port. The other intresting aspect is the setting of webroot which allows us to determine where the staticfiles/webcontent will go. Startup.cs just sets up the staticfile serving and MVC to allow for rest calls. Websockets will be added in the future part of the series.

Now with the code completed you can do a quick command to build the project

dotnet build

This wil produce a Debug/netcoreapp1.0 in the bin folder of your project. Looking in the win7-x64 you will see an executable that can be used to run the webserver. This is not enough though as this executable will only work on machines that have dotnet installed. To get a standalone version of dotnet you will need to run the following command.

dotnet publish

Now you will have a publish folder in Debug/netcoreapp1.0/win7-x64 folder which will have a lot more dlls and an executable. This folder is the standalone version of this application which means you only need this folder to run the executable. You won’t need dotnet core installed or even .net framework. This is ideal for distribution especially in a electron app.

With this we are now done with our first step. We have a functioning web server that can server up our web app. Next we will make an electron app that will launch this executable.

This article is Part 2 in a 2-Part Series.


Share this: