Make IHostedServiceAsync

This commit is contained in:
John Luo 2017-05-25 18:10:36 -07:00
parent 75ba58bac2
commit 3b488c470a
4 changed files with 32 additions and 15 deletions

View File

@ -3,6 +3,8 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
@ -19,11 +21,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
_services = services;
}
public void Start()
public async Task StartAsync(CancellationToken token)
{
try
{
Execute(service => service.Start());
await ExecuteAsync(service => service.StartAsync(token));
}
catch (Exception ex)
{
@ -31,11 +33,11 @@ namespace Microsoft.AspNetCore.Hosting.Internal
}
}
public void Stop()
public async Task StopAsync(CancellationToken token)
{
try
{
Execute(service => service.Stop());
await ExecuteAsync(service => service.StopAsync(token));
}
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;
@ -51,7 +53,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
{
try
{
callback(service);
await callback(service);
}
catch (Exception ex)
{

View File

@ -123,7 +123,7 @@ namespace Microsoft.AspNetCore.Hosting.Internal
_applicationLifetime?.NotifyStarted();
// Fire IHostedService.Start
_hostedServiceExecutor.Start();
await _hostedServiceExecutor.StartAsync(cancellationToken).ConfigureAwait(false);
_logger.Started();
@ -296,7 +296,10 @@ namespace Microsoft.AspNetCore.Hosting.Internal
}
// Fire the IHostedService.Stop
_hostedServiceExecutor?.Stop();
if (_hostedServiceExecutor != null)
{
await _hostedServiceExecutor.StopAsync(cancellationToken).ConfigureAwait(false);
}
// Fire IApplicationLifetime.Stopped
_applicationLifetime?.NotifyStopped();

View File

@ -1,6 +1,9 @@
// 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.
using System.Threading;
using System.Threading.Tasks;
namespace Microsoft.Extensions.Hosting
{
/// <summary>
@ -11,11 +14,11 @@ namespace Microsoft.Extensions.Hosting
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
void Start();
Task StartAsync(CancellationToken cancellationToken);
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
void Stop();
Task StopAsync(CancellationToken cancellationToken);
}
}

View File

@ -999,14 +999,16 @@ namespace Microsoft.AspNetCore.Hosting
public bool StopCalled { get; set; }
public bool DisposeCalled { get; set; }
public void Start()
public Task StartAsync(CancellationToken token)
{
StartCalled = true;
return Task.CompletedTask;
}
public void Stop()
public Task StopAsync(CancellationToken token)
{
StopCalled = true;
return Task.CompletedTask;
}
public void Dispose()
@ -1028,9 +1030,16 @@ namespace Microsoft.AspNetCore.Hosting
_disposing = disposing;
}
public void Start() => _started();
public void Stop() => _stopping();
public Task StartAsync(CancellationToken token)
{
_started();
return Task.CompletedTask;
}
public Task StopAsync(CancellationToken token)
{
_stopping();
return Task.CompletedTask;
}
public void Dispose() => _disposing();
}