Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
I have a .NET 5 background worker applications with a single background service (MyInternalBackgroundService).
Now I am working on a modular plug in architecture where plug ins are put in a plug in directory, from there assemblies are loaded. Each assembliy can contain multiple class definitons which inherit from BackgroundService. I load the list of types that are inheriting from BackgroundService.
I just can't figure out how to call the AddHostedService method for the loaded types. Every approach seems to cause a different compiler error.
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
services.AddHostedService<MyInternalBackgroundServiceImplementation>();
TypeInfo[] moduleTypes = // class scanning directories for dlls, load the assemblies and find the desired types
foreach(var moduleType in moduleTypes)
// can't find the correct way
// services.AddHostedService<moduleType>();
//services.AddHostedService<moduleType.GetType()>();
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.