Skip to content

fix: support windows services#263

Open
wolfgangs0815 wants to merge 1 commit intobagetter:mainfrom
wolfgangs0815:SupportWindowsServices
Open

fix: support windows services#263
wolfgangs0815 wants to merge 1 commit intobagetter:mainfrom
wolfgangs0815:SupportWindowsServices

Conversation

@wolfgangs0815
Copy link

Addresses #262

Comment on lines +19 to +30
if (WindowsServiceHelpers.IsWindowsService())
{
// Make sure appsettings.json can be found when running as windows service
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
}

var builder = CreateHostBuilder(args);

// Support running as a windows service
builder.UseWindowsService();

var host = builder.Build();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think something like this would make more sense:

Suggested change
if (WindowsServiceHelpers.IsWindowsService())
{
// Make sure appsettings.json can be found when running as windows service
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
}
var builder = CreateHostBuilder(args);
// Support running as a windows service
builder.UseWindowsService();
var host = builder.Build();
var builder = CreateHostBuilder(args);
// Support running as a windows service
if (WindowsServiceHelpers.IsWindowsService())
{
// Make sure appsettings.json can be found when running as windows service
Directory.SetCurrentDirectory(AppDomain.CurrentDomain.BaseDirectory);
builder.UseWindowsService();
}
var host = builder.Build();

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @seriouz,

Thank you for commenting and proposing improvements.

Wrapping builder.UseWindowsServices() into an "if( IsWindowsService )" is not necessary.
The extension method already checks this internally.

Also if you want to chain it with UseSystemd() it would not make sense, e.g.

builder.UseWindowsService().UseSystemd()

Regards the order when to apply the SetCurrentDirectory(...) code.
I think it needs to happen before the CreateHostBuilder() call (as it was in my code snippet) because otherwise appsettings.json is not found correctly during CreateHostBuilder(...) and e.g. the kestrel URL config in appsettings.json is not applied correctly

@seriouz seriouz requested a review from Regenhardt December 22, 2025 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants