Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<PackageVersion Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.10" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks" Version="9.0.10" />
<PackageVersion Include="Microsoft.Extensions.Diagnostics.HealthChecks.EntityFrameworkCore" Version="9.0.10" />
<PackageVersion Include="Microsoft.Extensions.Hosting.WindowsServices" Version="9.0.11" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="9.0.10" />
<PackageVersion Include="Microsoft.Extensions.Options.ConfigurationExtensions" Version="9.0.10" />
<PackageVersion Include="NuGet.Frameworks" Version="6.13.2" />
Expand Down Expand Up @@ -47,4 +48,4 @@
<PackageVersion Include="Microsoft.EntityFrameworkCore.Sqlite" Version="9.0.10" />
<PackageVersion Include="Tencent.QCloud.Cos.Sdk" Version="5.4.44" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions src/BaGetter/BaGetter.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" />
</ItemGroup>

<ItemGroup>
Expand Down
15 changes: 14 additions & 1 deletion src/BaGetter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using System.Threading.Tasks;
using BaGetter.Core;
using BaGetter.Web;
Expand All @@ -7,14 +8,26 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Hosting.WindowsServices;

namespace BaGetter;

public class Program
{
public static async Task Main(string[] args)
{
var host = CreateHostBuilder(args).Build();
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();
Comment on lines +19 to +30
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

if (!host.ValidateStartupOptions())
{
return;
Expand Down