在 TopShelf 中使用异步 WhenStarted 和 WhenStopped 方法

Using async WhenStarted & WhenStopped methods within TopShelf

我们像这样使用 TopShelf 来启动我们的服务。不过,我们在服务启动和停止方面看到了一些奇怪的问题,想知道这是否是由于我们的异步启动/停止方法造成的。从未提及使用异步查看文档。 one mentionon their github pages 声明您不应以这种方式使用异步。

但是,话虽如此,它编译和运行(大部分)没问题。那么这是正确的还是我应该使用 .Wait() 来代替?

var host = HostFactory.New(hostConfig =>
{
    hostConfig.Service<StreamClient>(serviceConfig =>
    {
        serviceConfig.ConstructUsing(name => new StreamClient());
        serviceConfig.WhenStarted(async tc => await tc.Start());
        serviceConfig.WhenStopped(async tc => await tc.Stop());
    });

    hostConfig.RunAsLocalSystem();

    hostConfig.SetDescription("Stream Client Service");
    hostConfig.SetDisplayName("Stream Client Service");
    hostConfig.SetServiceName("StreamClientService");
});

host.Run();

@Nkosi 问方法签名是什么样的,它们是异步的,启动内部客户端和进程。

public async Task Start()
{
    // Dont start again if we are already running, or if we are already in the starting state
    if (this.Running || this.Starting)
    {
        return;
    }

    await this.slackService.SendSlackServiceEvent(ServiceEventType.Starting, serviceName, applicationVersion);

    this.Starting = true;
    this.Stopping = false;

    var configurationBuilder = new ClientConfigurationBuilder();

    ClientConfiguration clientConfiguration;
    if (Constants.UseLocalConnection)
    {
        await this.OnClientDebugMessage($"Using Local Connection");
        clientConfiguration = configurationBuilder.CreateLocalConfiguration();
    }
    else
    {
        await this.OnClientDebugMessage($"Using SQL Connection");
        clientConfiguration = configurationBuilder.CreateSqlConfiguration();
    }

    this.ClusterGrainClient = await this.StartClient(clientConfiguration);

    if (this.ClusterGrainClient == null)
    {
        using (ConsoleColours.TextColour(ConsoleColor.Red))
        {
            await this.OnClientDebugMessage($"Cluster client null, aborting!");
        }

        return;
    }

    this.Running = true;

    await this.OnClientStarted();
    await this.slackService.SendSlackServiceEvent(ServiceEventType.Started, serviceName, applicationVersion);
    this.Starting = false;

}

无论是否是 Topshelf,Windows 服务主机将启动服务,而不是 运行 它。

我自己从未尝试过,但您可以尝试这样的操作:

public void Start() => this.StartAsync().GetAwaiter().GetResult();
public void Stop() => this.Stop().GetAwaiter().GetResult();

public async Task StartAsync()
{
    // ...
}

public async Task StopAsync()
{
    // ...
}

您实际上是在对那些即发即忘的代表进行 async void

引用Async/Await - Best Practices in Asynchronous Programming

事件处理程序是该规则允许的唯一例外

将您的启动和停止方法转换为引发可在内部等待的异步事件的同步方法。

public void Start() {
    Started += OnStarted; //subscribe to event
    Started(this, EventArgs.Empty); //raise event
}

private event EventHandler Started = delegate { };

private async void OnStart(object sender, EventArgs args) {
    Started -= OnStarted;        
    await StartAsync();
}

public async Task StartAsync() {
    // ...
}

然后正常调用开始

serviceConfig.WhenStarted(_ => _.Start());

这将按预期引发事件和流程。