-
Couldn't load subscription status.
- Fork 10
Description
Autofac has this
/// <summary>
/// When implemented by a component, an instance of the component will be resolved
/// and started as soon as the container is built. Autofac will not call the Start()
/// method when subsequent instances are resolved. If this behavior is required, use
/// an <c>OnActivated()</c> event handler instead.
/// </summary>
/// <remarks>
/// For equivalent "Stop" functionality, implement <see cref="IDisposable"/>. Autofac
/// will always dispose a component before any of its dependencies (except in the presence
/// of circular dependencies, in which case the components in the cycle are disposed in
/// reverse-construction order.)
/// </remarks>
public interface IStartable
{
/// <summary>
/// Perform once-off startup processing.
/// </summary>
void Start();
}anything that inherits from it will init on startup.
It's a cool idea some something we use in Supply extranets at Agoda. There this IHostedService now as well for background workers too
Idea would be to do something like this if we follow the autofac approach
[RegisterSingleton]
public class MyBackgroundWorker : IMyBackgroundWorker, IStartupable
{
// ..
} And then call a GetService after app init, this would also require use to add a new extension method though something like "UseAgodaIoC", or more specific "StartupAgodaIoCSingletons".
Alternatively we could simply leverage the IHostedService and register them with a new attribute
[RegisterSingletonStartup]
public class MyBackgroundWorker : IHostedService
{
// ..
}
// in registartion
services.AddHostedService<MyBackgroundWorker>();
//..The problem here though is that AddHostedService has no support for registering an interface.
In most of our use cases the startup method is prewarming cache in singletons that are later used for data fetchnig (i.e. Repositories, etc). So i think this wont work for us as we need interfaced use in teh regestration to properly mock for unit testing, so we need something simple like the autofac example imo.
Thoughts?