Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for IHostedLifecycleService in WebHost #60176

Open
1 task done
jraufeisen opened this issue Feb 3, 2025 · 0 comments
Open
1 task done

Add support for IHostedLifecycleService in WebHost #60176

jraufeisen opened this issue Feb 3, 2025 · 0 comments
Labels
area-hosting Includes Hosting

Comments

@jraufeisen
Copy link

jraufeisen commented Feb 3, 2025

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?

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-hosting Includes Hosting label Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-hosting Includes Hosting
Projects
None yet
Development

No branches or pull requests

1 participant