Last active
September 11, 2020 03:50
-
-
Save ErikSchierboom/ac1862f3f39fa053c21e9513b2bf4b62 to your computer and use it in GitHub Desktop.
Inject dependencies into hosted service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Threading; | |
using System.Threading.Tasks; | |
using Microsoft.AspNetCore.Hosting; | |
using Microsoft.Extensions.DependencyInjection; | |
using Microsoft.Extensions.DependencyInjection.Extensions; | |
using Microsoft.Extensions.Hosting; | |
using WebApiPlayground.Services; | |
namespace WebApiPlayground | |
{ | |
public class FixedServiceProviderFactory : IServiceProviderFactory<IServiceCollection> | |
{ | |
private readonly ServiceCollection _services; | |
private readonly ServiceProviderOptions _options; | |
public FixedServiceProviderFactory(ServiceCollection services) : this(services, new ServiceProviderOptions()) | |
{ | |
} | |
public FixedServiceProviderFactory(ServiceCollection services, ServiceProviderOptions options) | |
{ | |
_services = services ?? throw new ArgumentNullException(nameof(services)); | |
_options = options ?? throw new ArgumentNullException(nameof(options)); | |
} | |
public IServiceCollection CreateBuilder(IServiceCollection services) | |
{ | |
foreach (var service in services) | |
{ | |
if (service.ImplementationFactory != null) | |
_services.Add(new ServiceDescriptor(service.ServiceType, service.ImplementationFactory, service.Lifetime)); | |
else if (service.ImplementationInstance != null) | |
_services.Add(new ServiceDescriptor(service.ServiceType, service.ImplementationInstance)); | |
else | |
_services.Add(new ServiceDescriptor(service.ServiceType, service.ImplementationType, service.Lifetime)); | |
} | |
return _services; | |
} | |
public IServiceProvider CreateServiceProvider(IServiceCollection containerBuilder) => | |
containerBuilder.BuildServiceProvider(_options); | |
} | |
public class Program | |
{ | |
public static async Task Main() | |
{ | |
var services = new ServiceCollection(); | |
services.AddScoped<IWhisperService, WhisperService>(); | |
var serviceProviderFactory = new FixedServiceProviderFactory(services); | |
await CreateHostBuilder(serviceProviderFactory).RunConsoleAsync(); | |
} | |
private static IHostBuilder CreateHostBuilder(IServiceProviderFactory<IServiceCollection> serviceProviderFactory) => | |
new HostBuilder() | |
.ConfigureServices((context, services) => | |
{ | |
services.AddTransient<IHostedService>(provider => new WebHostedService(serviceProviderFactory)); | |
}); | |
} | |
public class WebHostedService : BackgroundService | |
{ | |
private readonly IServiceProviderFactory<IServiceCollection> _serviceProviderFactory; | |
public WebHostedService(IServiceProviderFactory<IServiceCollection> serviceProviderFactory) => | |
_serviceProviderFactory = serviceProviderFactory; | |
protected override async Task ExecuteAsync(CancellationToken stoppingToken) => | |
await CreateWebHostBuilder().Build().RunAsync(stoppingToken); | |
private IWebHostBuilder CreateWebHostBuilder() => | |
new WebHostBuilder() | |
.UseKestrel() | |
.UseStartup<Startup>() | |
.ConfigureServices((context, services) => | |
{ | |
services.AddSingleton(_serviceProviderFactory); | |
services.AddScoped<IShoutService, ShoutService>(); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment