Description
Description
.NET 6 published application sometimes crashes on service stop operation. I created a web application from the template and only added the Microsoft.Extensions.Hosting.WindowsServices
(version 6.0.0) package and called UseWindowsService()
to make sure the application listens for service controller requests.
Here is the code:
using Microsoft.Extensions.Hosting.WindowsServices;
var options = new WebApplicationOptions
{
Args = args,
ContentRootPath = WindowsServiceHelpers.IsWindowsService() ? AppContext.BaseDirectory : default
};
var builder = WebApplication.CreateBuilder(options);
// Add services to the container.
builder.Services.AddRazorPages();
builder.Host.UseWindowsService();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.Run();
Reproduction Steps
Repository with code, script and repro steps: https://github.com/zryska/net6-repro
- Create a new Web application using a template
- Make the changes to add UseWindowsService() call as shown above
- Publish the application as SFD or FDD for win-x64 runtime
- Create a new service using
sc create
command - Start the service and verify it's running
- Stop the service
- Start the service
- Repeat steps 4 and 5 until the issue is reproduced
It can be done through the services UI or using a script. On my system it usually happens 1 time out of 10 graceful shutdowns - but it isn't deterministic.
When done through services UI the following will be displayed:
Even log => Windows Logs -> System
will contain the following message (when done through script or UI):
The .NET6 test service service terminated unexpectedly. It has done this 26 time(s).
Expected behavior
Service stops gracefully every time.
Actual behavior
Non-graceful shutdowns are happening.
Regression?
No response
Known Workarounds
Explicitly assigning value to ServiceName
fixes the problem.
builder.Host.UseWindowsService(o => o.ServiceName = "AnyServiceName");
Configuration
.NET version: .NET 6.0.100
OS version: Windows 10 pro build 19044.1348
Architecture: x64
Other information
No response