The following class takes a primitive on the constructor, so I need to tell AutoFac how to generate said value using a delegate:
public class BackgroundTaskScheduler : IStartable
{
private readonly IJobRunner _jobRunner;
private int _triggerMilliseconds;
public BackgroundTaskScheduler(IJobRunner jobRunner, int triggerMilliseconds)
{
_jobRunner = jobRunner;
_triggerTimespan = triggerMilliseconds;
}
}
public static class AutoFac
{
public static void Configure()
{
var builder = new ContainerBuilder();
var triggerInterval =
int.Parse(
ConfigurationManager.AppSettings["TaskScheduleTriggerMilliseconds"]);
builder.Register(
c => new BackgroundTaskScheduler(c.Resolve<IJobRunner>(), triggerInterval)).AsImplementedInterfaces().SingleInstance();
builder.RegisterAssemblyTypes(typeof (RegistrationController).Assembly)
.AsImplementedInterfaces()
.AsSelf();
IContainer container = builder.Build();
}
}
However, Autofac appears to ignore my registration as evidenced by the exception it throws when resolving an instance of BackgroundTaskScheduler:
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'Delen.Server.Tasks.BackgroundTaskScheduler' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Int32 triggerMilliseconds' of constructor 'Void .ctor(Delen.Server.Tasks.IJobRunner, System.Int32)'.
I've successfully used the IContainerBuilder.Register method before in exactly such a scenario but I've no clue why it's not working this go around.
Update 1
Changing the order of the registrations did not make a difference.
Update 2
Here's an alternative way to achieve the same result as the accepted Answer:
builder.RegisterAssemblyTypes(typeof(IJobRunner).Assembly)
.Except<BackgroundTaskScheduler>()
.AsImplementedInterfaces()
.AsSelf()