.net core self contained linux code example
Example 1: .net core check if linux
if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
{
Console.WriteLine("We're on macOS!");
}
Example 2: asp.net core linux systemd
[Unit]
Description=ASP.NET Core web template
[Service]
Type=notify
# will set the Current Working Directory (CWD)
WorkingDirectory=/srv/AspNetSite
# systemd will run this executable to start the service
ExecStart=/srv/AspNetSite/AspNetSite
# to query logs using journalctl, set a logical name here
SyslogIdentifier=AspNetSite
# Use your username to keep things simple, for production scenario's I recommend a dedicated user/group.
# If you pick a different user, make sure dotnet and all permissions are set correctly to run the app.
# To update permissions, use 'chown yourusername -R /srv/AspNetSite' to take ownership of the folder and files,
# Use 'chmod +x /srv/AspNetSite/AspNetSite' to allow execution of the executable file.
User=yourusername
# ensure the service restarts after crashing
Restart=always
# amount of time to wait before restarting the service
RestartSec=5
# copied from dotnet documentation at
# https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1#code-try-7
KillSignal=SIGINT
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_PRINT_TELEMETRY_MESSAGE=false
# This environment variable is necessary when dotnet isn't loaded for the specified user.
# To figure out this value, run 'env | grep DOTNET_ROOT' when dotnet has been loaded into your shell.
Environment=DOTNET_ROOT=/opt/rh/rh-dotnet31/root/usr/lib64/dotnet
[Install]
WantedBy=multi-user.target