How do I property register an class which contains both the IHostedService
and a custom interface like IMyInterface
?
As in
class BackgroundTaskScheduler: BackgroundService, ITaskScheduler {...}
If it is configure like:
services.AddHostedService<BackgroundTaskScheduler>();
And then try to have it inject into a client, like so:
public class Foo
{
Foo(ITaskScheduler taskScheduler) {...}
}
An error is generated stating that ASP.net can't resolve BackgroundTaskScheduler
, why?
After reading lots of ideas, including:
HostedServicewrapping
class which forward the start and stop calls to the real hosted service. But how to get it to work without requiring a wrapper class? I combined the ideas discussed above into the following two extension methods.
If you like using interface DI, as in Bar.Bar(IFoo foo)
then use this one:
/// <summary>
/// Used to register <see cref="IHostedService"/> class which defines an referenced <typeparamref name="TInterface"/> interface.
/// </summary>
/// <typeparam name="TInterface">The interface other components will use</typeparam>
/// <typeparam name="TService">The actual <see cref="IHostedService"/> service.</typeparam>
/// <param name="services"></param>
public static void AddHostedApiService<TInterface, TService>(this IServiceCollection services)
where TInterface : class
where TService : class, IHostedService, TInterface
{
services.AddSingleton<TInterface, TService>();
services.AddSingleton<IHostedService>(p => (TService) p.GetService<TInterface>());
}
Usage:
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedApiService<ITaskScheduler, BackgroundTaskScheduler>();
}
If you like to use concrete class injection, as in Bar.Bar(Foo foo)
then use:
/// <summary>
/// Used to register <see cref="IHostedService"/> class which defines an interface but will reference the <typeparamref name="TService"/> directly.
/// </summary>
/// <typeparam name="TService">The actual <see cref="IHostedService"/> service.</typeparam>
public static void AddHostedApiService<TService>(this IServiceCollection services)
where TService : class, IHostedService
{
services.AddSingleton<TService>();
services.AddSingleton<IHostedService>(p => p.GetService<TService>());
}
Usage:
public void ConfigureServices(IServiceCollection services)
{
services.AddHostedApiService<BackgroundTaskScheduler>();
}
Enjoy!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.