C#.NET 8我的Windows Service可以从哪里访问SC.EXE的参数?

问题描述 投票:0回答:1
IN.NET框架4.6.2,您将这种类型的代码用于Windows服务:

public partial class ServiceMain : ServiceBase { public ServiceMain() { InitializeComponent(); } protected override void OnStart( string[] args ) { // I can do something with the args.
IN.NET 8,我看到的每个Windows服务示例(以及我写过的)是在您的主入口点(通常为程序.cs)中声明一个工人

builder.Services.AddHostedService<Worker>();
那个工人只是一个背景服务:

public class Worker : BackgroundService
通过sc.exe启动时,来自main的args是空的。还有其他方法可以抓住这些论点吗?
我期望在主要args中看到sc.exe呼叫的args的值。我看不到.NET 8等于有服务参数的在线示例。

这个似乎是一个答案,但我已经确认args总是空的。

我自己创建了几种ASP核心服务,我认为您是错误的 - 但是您是正确的,没有可访问的Args。

,我检查了来源:https://github.com/dotnet/runtime/tree/main/main/src/libraries/microsoft.extensions.hosting.windowsservices/src

c# windows-services .net-8.0 servicecontroller service-control-manager
1个回答
0
投票
protected override void OnStart(string[] args)

我在任何地方都找不到公共ARGS成员,所以我认为唯一的途径是覆盖此类并添加自己的IHOSTLIFETIME的实现

我从WindowsserviceLifetime -HostBuilderextensions.cs复制以下内容 i仅进行了一个小的更改,以使您自己的WindowsServicelifetime基于类别。

namespace Microsoft.Extensions.Hosting { public static class MyWindowsServiceLifetimeHostBuilderExtensions { public static void AddMyWindowsServiceLifetime<TImplementation>(this IServiceCollection services , Action<WindowsServiceLifetimeOptions> configure) where TImplementation : class, IHostLifetime { if (WindowsServiceHelpers.IsWindowsService()) { #if !NETFRAMEWORK Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); #endif services.AddLogging(logging => { #if !NETFRAMEWORK Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); #endif logging.AddEventLog(); }); services.AddSingleton<IHostLifetime, TImplementation>(); services.AddSingleton<IConfigureOptions<EventLogSettings>, EventLogSettingsSetup>(); services.Configure(configure); } } private sealed class EventLogSettingsSetup : IConfigureOptions<EventLogSettings> { private readonly string _applicationName; public EventLogSettingsSetup(IHostEnvironment environment) { _applicationName = environment.ApplicationName; } public void Configure(EventLogSettings settings) { #if !NETFRAMEWORK Debug.Assert(RuntimeInformation.IsOSPlatform(OSPlatform.Windows)); #endif if (string.IsNullOrEmpty(settings.SourceName)) { settings.SourceName = _applicationName; } } } } }

我自己的WindowsServiceLifetime基于类似的类是:
[SupportedOSPlatform("windows")]
public class MyWindowsServiceLifetime : WindowsServiceLifetime
{
    public static string[] args { get; set; }
    public MyWindowsServiceLifetime(IHostEnvironment environment
        , IHostApplicationLifetime applicationLifetime
        , ILoggerFactory loggerFactory, IOptions<HostOptions> optionsAccessor)
        : base(environment, applicationLifetime, loggerFactory, optionsAccessor)
    {
    }
    protected override void OnStart(string[] args)
    {
        MyWindowsServiceLifetime.args = args;
        base.OnStart(args);
    }
}

在我的启动代码中,我称之为新的扩展方法:

builder.Services.AddMyWindowsServiceLifetime<MyWindowsServiceLifetime>(c => { });

现在您可以从自己的班级中访问ARG,或者在这种情况下,我使它们公开静态。
var _args = MyWindowsServiceLifetime.args;
if (_args != null)
{
    foreach (var arg in _args)
    {
        logger.LogInformation("arg= {arg}", arg);
    }
}
cons.cs启动代码中的args不可用,但是您可以从其他任何地方访问它们。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.