Add a AddHostedService extension #1402

This commit is contained in:
Chris Ross (ASP.NET) 2018-05-01 11:06:25 -07:00
parent 1717b97444
commit 2c1376c95f
8 changed files with 32 additions and 12 deletions

View File

@ -25,8 +25,8 @@ namespace GenericHostSample
})
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<IHostedService, MyServiceA>();
services.AddScoped<IHostedService, MyServiceB>();
services.AddHostedService<MyServiceA>();
services.AddHostedService<MyServiceB>();
})
.Build();
}

View File

@ -23,8 +23,8 @@ namespace GenericHostSample
})
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<IHostedService, MyServiceA>();
services.AddScoped<IHostedService, MyServiceB>();
services.AddHostedService<MyServiceA>();
services.AddHostedService<MyServiceB>();
})
.Build();

View File

@ -11,8 +11,8 @@ namespace GenericHostSample
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<IHostedService, MyServiceA>();
services.AddScoped<IHostedService, MyServiceB>();
services.AddHostedService<MyServiceA>();
services.AddHostedService<MyServiceB>();
});
await builder.RunConsoleAsync();

View File

@ -12,8 +12,8 @@ namespace GenericHostSample
var builder = new HostBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddScoped<IHostedService, MyServiceA>();
services.AddScoped<IHostedService, MyServiceB>();
services.AddHostedService<MyServiceA>();
services.AddHostedService<MyServiceB>();
});
await builder.RunAsServiceAsync();

View File

@ -22,7 +22,7 @@ namespace GenericWebHost
{
options.ConfigureApp = configureApp;
});
services.AddSingleton<IHostedService, WebHostService>();
services.AddHostedService<WebHostService>();
var listener = new DiagnosticListener("Microsoft.AspNetCore");
services.AddSingleton<DiagnosticListener>(listener);

View File

@ -0,0 +1,20 @@
// 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 Microsoft.Extensions.Hosting;
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionHostedServiceExtensions
{
/// <summary>
/// Add an <see cref="IHostedService"/> registration for the given type.
/// </summary>
/// <typeparam name="THostedService">An <see cref="IHostedService"/> to register.</typeparam>
/// <param name="services">The <see cref="IServiceCollection"/> to register with.</param>
/// <returns>The original <see cref="IServiceCollection"/>.</returns>
public static IServiceCollection AddHostedService<THostedService>(this IServiceCollection services)
where THostedService : class, IHostedService
=> services.AddTransient<IHostedService, THostedService>();
}
}

View File

@ -548,7 +548,7 @@ namespace Microsoft.AspNetCore.Hosting
.UseFakeServer()
.ConfigureServices(services =>
{
services.AddSingleton<IHostedService, TestHostedService>();
services.AddHostedService<TestHostedService>();
})
.Build())
{

View File

@ -110,7 +110,7 @@ namespace Microsoft.Extensions.Hosting
using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IHostedService, TestHostedService>();
services.AddHostedService<TestHostedService>();
})
.Build())
{
@ -133,7 +133,7 @@ namespace Microsoft.Extensions.Hosting
using (var host = CreateBuilder()
.ConfigureServices((hostContext, services) =>
{
services.AddSingleton<IHostedService, TestHostedService>();
services.AddHostedService<TestHostedService>();
})
.Build())
{