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.
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.
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.
ยฉ 2025 juniyunapapa@gmail.com.