gem version

Using Passenger with DotNet Core

In this example we'll be using the built in webapi template, because it is simple enough to not distract from the tutorial. However you are free to choose your favourite framework, we simply made this choice for our docs.

Step 1: Install DotNet Core

DotNet Core is available from Microsoft via an installation script, or package repository. We recommend using the package repository if possible to receive updates via your package manager.

Debian:

apt install -y libicu57 zlib1g libkrb5-3 libssl1.0.2 libcurl3 liblttng-ust0 wget gpg apt-transport-https
wget -qO- https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.asc.gpg
mv microsoft.asc.gpg /etc/apt/trusted.gpg.d/
wget -q https://packages.microsoft.com/config/debian/9/prod.list
mv prod.list /etc/apt/sources.list.d/microsoft-prod.list
chown root:root /etc/apt/trusted.gpg.d/microsoft.asc.gpg
chown root:root /etc/apt/sources.list.d/microsoft-prod.list
apt update
apt install -y dotnet-sdk-2.2

Ubuntu:

apt install -y libicu60 zlib1g libkrb5-3 liblttng-ust0 libcurl4 libssl1.0.0 wget software-properties-common apt-transport-https
wget -q https://packages.microsoft.com/config/ubuntu/18.04/packages-microsoft-prod.deb
dpkg -i packages-microsoft-prod.deb
add-apt-repository universe
apt update
apt install -y dotnet-sdk-2.2

CentOS:

yum install -y epel-release
yum install -y lttng-ust libcurl openssl-libs krb5-libs libicu zlib
rpm -Uvh https://packages.microsoft.com/config/rhel/7/packages-microsoft-prod.rpm
yum update -y
yum install -y dotnet-sdk-2.2

RHEL:

subscription-manager register --username $USERNAME_EMAIL --password $PASSWORD --auto-attach
subscription-manager repos --enable=rhel-7-server-dotnet-rpms
yum install -y rh-dotnet22 scl-utils
scl enable rh-dotnet22 bash

macOS:

curl https://dot.net/v1/dotnet-install.sh | bash -s -- -Channel Current
PATH=$PATH:~/.dotnet/
dotnet --help

Step 2: Create your project

Run the following commands in a directory where you keep your projects. The dotnet command will create a new project in the hello_world directory, populated with the files you'll use to create your app.

mkdir hello_world
cd hello_world
dotnet new webapi

Step 3: Hello world

Edit the Startup.cs file to disable https redirection in development, as DotNet's support for https in dev is flakey. You are going to move the following line into the else clause of the Configure function's conditional:

app.UseHttpsRedirection();

Next edit the Properties/launchSettings.json file and delete this line:

"applicationUrl": "https://localhost:5001;http://localhost:5000",

Next edit the Controllers/ValuesController.cs file to contain these contents:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace hello_world.Controllers
{
    [Route("")]
    [Route("/Index")]
    [Route("/[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        // GET /values
        [HttpGet]
        public ActionResult<string> Get()
        {
            return "Hello, world!";
        }
    }
}

Step 4: Build and run

Run the following commands to run the web app, which will serve the web app on port 5000, then verify that your app is being served by sending a request to the web app from another terminal.

dotnet run
# in another terminal
curl http://localhost:5000/

Step 5: Add Passenger

You will need Passenger installed, for instructions on how to install Passenger click here.

Next we will want a release build of our web app, which you can make with dotnet build -c Release, then you can move the executable into a more convenient location on your disk, such as ~/Sites/. Once you are happy with where things are, you can start your web app in Passenger by cding to the dir with the executable and running the following command: passenger start --app-start-command 'env ASPNETCORE_URLS=http://localhost:$PORT dotnet ./hello_world.dll'.

Passenger requires that applications listen on a particular port that Passenger has chosen, which it provides to the application by replacing the $PORT string in its startup command with the actual port number. Regardless of how your app accepts port arguments, the startup string must be able to convert from a cli argument to what works for your app. In our example we converted the port argument to an envvar to show how one may need to change how the port is passed to your app.

You can verify that everything is working with curl http://localhost:3000. To stop Passenger simply press Ctrl+C in the terminal where you started it.

The commands to run from this section are collected here:

dotnet build -c Release
passenger start --app-start-command 'env ASPNETCORE_URLS=http://localhost:$PORT dotnet ./hello_world.dll'
# in another terminal
curl http://localhost:3000
light mode dark mode
Passenger 6 Passenger 6

Was this article helpful?

Yes No

Thanks for your feedback!