I am preparing to host my apps for internal use on a linux server (Ubuntu 24.*) and am currently testing the right configuration. I have hit a annoying snag when trying to run a dotnet app (TestLinuxMySQL.dll) as a service.
I would like to assure my app is started when linux is started. I have created a service, but the service crashes as it cannot find relevant files and folders (specifically apssettings.json and /wwwroot).
When I try to run the app from the CLI ($ dotnet /var/www/testlinux/TestLinuxMySQL.dll
I get the following errormessage:
*Unhandled exception. System.InvalidOperationException: Connection string ‘DefaultConnection’ not found at Program.$(String[] args) in E:…TestLinuxMySQLTestLinuxMySQLProgram.cs:line 7 Aborted (core dumped) *
When I start the app from its own folder, everything works fine.
It appears that dotnet or the app needs a different configuration with explicit reference to the path?
How can I make sure the app starts properly as a service under /etc/systemd/system?
I have tried to set the WebBuilderOptions in the app by adding to program.cs
WebbuilderOptions webBuilderOptions = new WebBuilderOptions
{
ContentRootPath = Directory.GetCurrentDirectory(),
WebRootPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot")
};
This did not solve the problem.
Strongly typing the connectionstring temporarily bypassed the problem, but the /wwwroot
-folder still was not found and is not preferable.
The service code /etc/systemd/system/kestrel-testlinux.service
:
[Unit]
Description=Example .NET Web API App running on Linux
[Service]
WorkingDirectory=/var/www/testlinux
ExecStart=/snap/bin/dotnet /var/www/testlinux/TestLinuxMySQL.dll
Restart=always
# Restart service after 10 seconds if the dotnet service crashes:
RestartSec=10
KillSignal=SIGINT
SyslogIdentifier=dotnet-example
User=www-data
Environment=ASPNETCORE_ENVIRONMENT=Production
Environment=DOTNET_NOLOGO=true
[Install]
WantedBy=multi-user.target