Make IHostedServiceAsync
This commit is contained in:
parent
75ba58bac2
commit
3b488c470a
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using Microsoft.Extensions.Hosting;
|
using Microsoft.Extensions.Hosting;
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
|
@ -19,11 +21,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
_services = services;
|
_services = services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start()
|
public async Task StartAsync(CancellationToken token)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Execute(service => service.Start());
|
await ExecuteAsync(service => service.StartAsync(token));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -31,11 +33,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public async Task StopAsync(CancellationToken token)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
Execute(service => service.Stop());
|
await ExecuteAsync(service => service.StopAsync(token));
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
@ -43,7 +45,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void Execute(Action<IHostedService> callback)
|
private async Task ExecuteAsync(Func<IHostedService, Task> callback)
|
||||||
{
|
{
|
||||||
List<Exception> exceptions = null;
|
List<Exception> exceptions = null;
|
||||||
|
|
||||||
|
|
@ -51,7 +53,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
callback(service);
|
await callback(service);
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
_applicationLifetime?.NotifyStarted();
|
_applicationLifetime?.NotifyStarted();
|
||||||
|
|
||||||
// Fire IHostedService.Start
|
// Fire IHostedService.Start
|
||||||
_hostedServiceExecutor.Start();
|
await _hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
|
||||||
_logger.Started();
|
_logger.Started();
|
||||||
|
|
||||||
|
|
@ -296,7 +296,10 @@ namespace Microsoft.AspNetCore.Hosting.Internal
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fire the IHostedService.Stop
|
// Fire the IHostedService.Stop
|
||||||
_hostedServiceExecutor?.Stop();
|
if (_hostedServiceExecutor != null)
|
||||||
|
{
|
||||||
|
await _hostedServiceExecutor.StopAsync(cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
// Fire IApplicationLifetime.Stopped
|
// Fire IApplicationLifetime.Stopped
|
||||||
_applicationLifetime?.NotifyStopped();
|
_applicationLifetime?.NotifyStopped();
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,9 @@
|
||||||
// Copyright (c) .NET Foundation. All rights reserved.
|
// Copyright (c) .NET Foundation. All rights reserved.
|
||||||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
|
||||||
|
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace Microsoft.Extensions.Hosting
|
namespace Microsoft.Extensions.Hosting
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
@ -11,11 +14,11 @@ namespace Microsoft.Extensions.Hosting
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Triggered when the application host is ready to start the service.
|
/// Triggered when the application host is ready to start the service.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Start();
|
Task StartAsync(CancellationToken cancellationToken);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Triggered when the application host is performing a graceful shutdown.
|
/// Triggered when the application host is performing a graceful shutdown.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
void Stop();
|
Task StopAsync(CancellationToken cancellationToken);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -999,14 +999,16 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
public bool StopCalled { get; set; }
|
public bool StopCalled { get; set; }
|
||||||
public bool DisposeCalled { get; set; }
|
public bool DisposeCalled { get; set; }
|
||||||
|
|
||||||
public void Start()
|
public Task StartAsync(CancellationToken token)
|
||||||
{
|
{
|
||||||
StartCalled = true;
|
StartCalled = true;
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Stop()
|
public Task StopAsync(CancellationToken token)
|
||||||
{
|
{
|
||||||
StopCalled = true;
|
StopCalled = true;
|
||||||
|
return Task.CompletedTask;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
|
|
@ -1028,9 +1030,16 @@ namespace Microsoft.AspNetCore.Hosting
|
||||||
_disposing = disposing;
|
_disposing = disposing;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Start() => _started();
|
public Task StartAsync(CancellationToken token)
|
||||||
|
{
|
||||||
public void Stop() => _stopping();
|
_started();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
public Task StopAsync(CancellationToken token)
|
||||||
|
{
|
||||||
|
_stopping();
|
||||||
|
return Task.CompletedTask;
|
||||||
|
}
|
||||||
|
|
||||||
public void Dispose() => _disposing();
|
public void Dispose() => _disposing();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue