Is it possible to add an IHostedService to an IHost after calling HostApplicationBuilder.Build() and IHost.StartAsync()?
The application needs to be able to load plug-ins during use and have services provided in those plug-ins become hosted by the already-started IHost. Is this possible?
AFAIK - no, at least in non-brittle way. The
IHostedServiceinfrastructure is build on top of the DI and build-in DI container is immutable after it has been build. Even if you substitute the container with some which allows runtime management of dependencies it still will not work because theIHostedService's are resolved and started upon the application start (docs, internalHostimplementation starting services):So working over default infrastructure to "just add" the hosted service is not possible.
But this requirement is totally satisfiable - just create a hosted service which will somehow (depended on your needs) determine/watch new plugins available and manage them (start and stop). Those plugins can be build over standard
IHostedServiceinterface or any one you deem appropriate. Something to get you started: