Search code examples
htmlasp.net-coreraspberry-piautostart

ASP.NET Core web application doesn't use static files stored in wwwroot when autostart on Raspberry


I just deployed my ASP.NET Core web application on my raspberry pi 4b which works just fine when I start it manually via the terminal. As I need it to start right after the start, I thought of Autostart -> so I found some methods to do so (I already tried lxde, crontab and rc.local), every time I start my raspberry, the application starts just fine but my images, which are stored in the wwwroot folder are missing and I get an error 404. I also tried adding sudo.

When I tried to run the application via bash (so I could see the logs), I encountered following message:

User profile is available. Using '/home/user/.aspnet/DataProtection-Keys' as key repository; keys will not be encrypted at rest.

My Program.cs file looks like this:

using GaragenSteuerung.Services;

var builder = WebApplication.CreateBuilder(args);

builder.Logging.ClearProviders();
builder.Logging.AddConsole();

builder.Services.AddSingleton<IRelayService, RelayService>();
builder.Services.AddRazorPages();

var app = builder.Build();

app.UseDefaultFiles();
app.UseStaticFiles();

app.MapRazorPages();
app.Run();

I tried those several autostart methods as well as different publishing methods (one file, folder), and bootstrap did also not work when I stored it locally


Solution

  • You can create a .service file where ExecStart points to the dotnet command and then your .dll.

    [Unit] 
    Description=Example for .NET ConsoleApp with systemd
    
    [Service]
    ExecStart=/home/pi/.dotnet/dotnet Simple-Dotnet-Linux-Service.dll
    WorkingDirectory=/home/pi/simpleDotnetService
    User=pi
    Group=pi
    
    [Install]
    WantedBy=multi-user.target
    

    Copy the .service file to /etc/systemd/system/ then run:

    sudo systemctl enable <filename>.service #enables autostart
    sudo systemctl start <filename>.service #runs now
    sudo systemctl status <filename>.service #checks status
    

    Then the .dll will now be run on system boot. Enjoy! Or, to see a worked through example, keep reading on.

    For more details, you can check this blog: Creating an Autostart .NET 6 Service on a Raspberry Pi