[ASP.Net] Fixing ASP.NET Core Windows Service Startup Error in .NET 6
Table of Contents
Error Occurrence
If you encounter an error after building an ASP.NET Core project in .NET 6, registering it as a Windows Service, and starting the service, follow these steps to diagnose and fix the issue.
Checking the Event Log
First, open the Event Log Viewer and check the exception message.

Navigate to Windows Logs → Application and look for a .NET Runtime Error. This log typically reveals the exact location in the code where the exception occurred.
Fixing the Issue
If the error is caused by code accessing configuration settings from appsettings.json, you can resolve it by specifying the ContentRootPath when creating the application builder.
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default,
});
For unknown reasons, when running the application as a Windows Service, the ContentRootPath may be set incorrectly. By explicitly setting it as shown above, the application should function properly.