DEV Community

Irina Scurtu
Irina Scurtu

Posted on • Originally published at irina.codes on

.NET API as a Linux Service

.net api as linux service

.NET API as a Linux Service is just one of the many options when it comes to hosting.

One thing I love about .NET Core is that it runs everywhere. Running .NET APIs as Linux services, allows different organizations to reduce some costs by maybe reusing infrastructure. Before .NET Core, .NET was almost synonymous with Windows. The only server for .NET was IIS.

Now you have a ton of options when it comes to hosting your .NET applications.

You want to keep running it on Windows? You can host and install it as a Windows Service with NSSM. Do you want Kestrel instead of IIS Express? No worries, you can do that. Nowadays, sky is the limit when it comes to hosting a .NET API.

One thing you will need to do is to install .NET on the server. You can read herehow to install .NET on different Linux distributions. (If you never did this or you are not an experienced Linux user).

What you need

– Putty or a way to connect to your Linux server

– sudo rights or execution rights on the server

Running an API as a Linux Service

If you have your API in good standing, versioned,and with the right status codes and you chose Kestrel, the only thing you need to do is to create a .service file and to put it on your Linux server. In my case, the API was hosted on a RedHat distribution.

The service content will be a file with the .service extension (let’s call the file myapi.service), placed under /etc/systemd/system path.

The .service file will contain info related to what it will need to run and from where, and a few flags.

[Unit]
Description=Awesome API
[Service]
WorkingDirectory=/var/opt/myapps/myapi
ExecStart=/usr/bin/dotnet /var/opt/myapps/myapi/MyApi.dll
Restart=always
RestartSec=10
SyslogIdentifier=myapi
User=root
Environment=ASPNETCORE_ENVIRONMENT=Development
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
[Install]
WantedBy=multi-user.target
Enter fullscreen mode Exit fullscreen mode

Assuming you have sudo rights, you will need to start the service by running systemctl start myapi.service

Useful linux commands

systemctl stop myapi.service stops your service

systemctl start myapi.service starts your service

systemctl status myapi.service returns the status of your service. Is useful when you want to make sure the service is running or for debugging purposes. The response will contain some aditional info, depending on what you have configure, but it will look pretty much like this: status response

Once you have your NET API as a Linux Service up and running, you might need to make it run on HTTPS.

You’ll see in another article how you can configure NGINX Reverse proxy to have Https.

The post .NET API as a Linux Service appeared first on Irina Scurtu.

Top comments (0)