Skip to content

Add support for IHostedLifecycleService in WebHost #60176

Open
@jraufeisen

Description

@jraufeisen

Is there an existing issue for this?

  • I have searched the existing issues

Describe the solution you'd like

.NET 8 introduced the interface IHostedLifecycleService, which extends IHostedService with some additional callbacks such as StartingAsync() and StartedAsync(). The original API proposal (dotnet/runtime#86511) noted that

This interface is located in the Microsoft.Extensions.Hosting.Abstractions assembly which will be supported by the default host (in the Microsoft.Extensions.Hosting assembly). Other hosts will need to implement IHostedService in order to support this new interface.

As far as I can see, WebHost did not gain support for this new feature yet.

internal sealed partial class WebHost : IWebHost, IAsyncDisposable
{
private const string DeprecatedServerUrlsKey = "server.urls";
private readonly IServiceCollection _applicationServiceCollection;
private IStartup? _startup;
private ApplicationLifetime? _applicationLifetime;
private HostedServiceExecutor? _hostedServiceExecutor;

internal sealed class HostedServiceExecutor
{
private readonly IEnumerable<IHostedService> _services;
public HostedServiceExecutor(IEnumerable<IHostedService> services)
{
_services = services;
}
public async Task StartAsync(CancellationToken token)
{
foreach (var service in _services)
{
await service.StartAsync(token);
}
}
public async Task StopAsync(CancellationToken token)
{
List<Exception>? exceptions = null;
foreach (var service in _services)
{
try
{
await service.StopAsync(token);
}
catch (Exception ex)
{
exceptions ??= [];
exceptions.Add(ex);
}
}
// Throw an aggregate exception if there were any exceptions
if (exceptions != null)
{
throw new AggregateException(exceptions);
}
}

Would it be possible to add support for IHostedLifecycleService to WebHost?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions