I encountered an issue while configuring an ASP.NET Core 7.0 web application to run as a Windows Service (Windows 11), following the official Microsoft guide: Host ASP.NET Core in a Windows Service
After setting up the service with sc.exe and specifying the binpath to my application, the service failed to start, timing out without logging any events. Further investigation revealed that the service did not execute any code from Program.cs, as indicated by the absence of startup logs.
Narrowing down the problem involved removing and incrementally adding project dependencies. I discovered the issue was related to the presence of an auxiliary .exe file in the application's source folder. This utility executable's name is a prefix of the main application file, and Windows Services appears to execute this file instead of the intended main application. Upon changing the name of the utility executable to something that is not a prefix of the main application, the service correctly executes the main application:
Example 1:
binpath = "<SOURCE FOLDER PATH>\application name.exe"
utility .exe path = "<SOURCE FOLDER PATH>\app.exe" <--- THIS IS RUN
main .exe path = "<SOURCE FOLDER PATH>\application name.exe"
Example 2:
binpath = "<SOURCE FOLDER PATH>\application name.exe"
utility .exe path = "<SOURCE FOLDER PATH>\test.exe"
main .exe path = "<SOURCE FOLDER PATH>\application name.exe" <--- THIS IS RUN
Question: How does Windows Services determine which executable to run from the specified binpath? Is there a naming convention or selection process that could cause it to choose an auxiliary executable over the main application, to which the binpath directly references?
This problem had me debugging for quite some time, so if everyone runs into an similar issue, I hope this helps!