Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- IHostBuilder builder = new HostBuilder()
- .ConfigureServices((hostBuilderContext, services) => {
- services.AddHostedService<MyService>();
- });
- public class MyService : IHostedService {
- public async Task StartAsync(CancellationToken cancellationToken) { }
- public async Task StopAsync(CancellationToken cancellationToken) { }
- }
- public class MyCustomProvider : IServiceProvider {
- SomeContainer _someContainer;
- public MyCustomProvider(SomeContainer someContainer) {
- _container = container;
- }
- public object GetService(Type serviceType) {
- return _container.Get(serviceType);
- // This is only called once, and serviceType is typeof(IHost)
- }
- }
- IHostBuilder builder = new HostBuilder()
- .UseServiceProviderFactory(new MyContainerFactory())
- .ConfigureServices((hostBuilderContext, services) => {
- services.AddHostedService<MyService>();
- });
- class MyContainerFactory : IServiceProviderFactory<SomeContainer> {
- public SomeContainer CreateBuilder(IServiceCollection services) {
- return SomeContainer.SetupStuff().Create();
- }
- public IServiceProvider CreateServiceProvider(SomeContainer container) {
- return new MyCustomProvider(container);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement