dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.27k stars 4.73k forks source link

[API Proposal]: Allow UseWindowsService and UseSystemd to indicate whether they've activated #90105

Open jez9999 opened 1 year ago

jez9999 commented 1 year ago

Background and motivation

The .UseWindowsService() and .UseSystemd() extensions allow a host builder to be configured to run as a Windows or systemd service if it's detected that they're currently running in one of those contexts, but it'd be useful for the program to actually know whether that activation happened. They should therefore be able to report back as to whether they detected that the current program was running as a Windows or systemd service, and therefore activated.

API Proposal

namespace Microsoft.Extensions.Hosting;

public static class WindowsServiceLifetimeHostBuilderExtensions
{
    public static (bool? WasActivated, IHostBuilder Builder) UseWindowsService(this IHostBuilder hostBuilder, Action<WindowsServiceLifetimeOptions> configure, bool indicateWhetherActivated = false)
}
namespace Microsoft.Extensions.Hosting;

public static class SystemdHostBuilderExtensions
{
    public static (bool? WasActivated, IHostBuilder Builder) UseSystemd(this IHostBuilder hostBuilder, bool indicateWhetherActivated = false)
}

API Usage

var builder = Host.CreateDefaultBuilder(args);
(var isWinService, _) = builder.UseWindowsService(x => x.ServiceName = "MyService", true);
(var isSystemdService, _) = builder.UseSystemd(true);

Alternative Designs

No response

Risks

No response

ghost commented 1 year ago

Tagging subscribers to this area: @dotnet/area-extensions-hosting See info in area-owners.md if you want to be subscribed.

Issue Details
### Background and motivation The `.UseWindowsService()` and `.UseSystemd()` extensions allow a host builder to be configured to run as a Windows or systemd service if it's detected that they're currently running in one of those contexts, but it'd be useful for the program to actually know whether that activation happened. They should therefore be able to report back as to whether they detected that the current program was running as a Windows or systemd service, and therefore activated. ### API Proposal ```csharp namespace Microsoft.Extensions.Hosting; public static class WindowsServiceLifetimeHostBuilderExtensions { public static (bool? WasActivated, IHostBuilder Builder) UseWindowsService(this IHostBuilder hostBuilder, Action configure, bool indicateWhetherActivated = false) } ``` ```csharp namespace Microsoft.Extensions.Hosting; public static class SystemdHostBuilderExtensions { public static (bool? WasActivated, IHostBuilder Builder) UseSystemd(this IHostBuilder hostBuilder, bool indicateWhetherActivated = false) } ``` ### API Usage ```csharp var builder = Host.CreateDefaultBuilder(args); var isWinService = builder.UseWindowsService(x => x.ServiceName = "MyService", true); var isSystemdService = builder.UseSystemd(true); ``` ### Alternative Designs _No response_ ### Risks _No response_
Author: jez9999
Assignees: -
Labels: `api-suggestion`, `untriaged`, `area-Extensions-Hosting`
Milestone: -
gfoidl commented 1 year ago

You could use something like

using Microsoft.Extensions.Hosting.Systemd;
using Microsoft.Extensions.Hosting.WindowsServices;

public static bool IsRunAsService { get; } = IsRunAsServiceCore();

private static bool IsRunAsServiceCore()
{
    return WindowsServiceHelpers.IsWindowsService()
        || SystemdHelpers       .IsSystemdService();
}

Does this fit your needs?