Search code examples
c#asp.net-corewindows-services

ASP.NET Core hosted in a Windows Service - static files not being served (i.e. contents of /wwwroot)


I have an ASP.NET Core v3.1 application hosted in a Windows Service. When I run the service locally, everything works as expected. However, when I publish it and deploy it out to a server the static files (i.e. the contents of the /wwwroot folder) are not being served, e.g

enter image description here

My service exists on my file system like so, and the wwwroot contains all the necessary files:

enter image description here

As per Microsoft documentation, I understand that the reason for this is because the working directory of the Windows service is the C:\Windows\system32 folder, which isn't a suitable place to drop the wwwroot files. How do I configure the location of the wwwroot folder?


Solution

  • In the Startup.cs class Configure() function, I needed to add the following:

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
            Path.Combine(workingDirectoryPath, "wwwroot"))
    });
    

    where 'workingDirectoryPath' was an absolute path to the directory where my service executable is, e.g. "C:\MyService"