I am getting the below error on registering a concrete class using Autofac
This is the class
[AutomaticRetry(Attempts = 0)]
public class NoRetryJob<T> where T : IRecurringJob
{
private T Job { get; set; }
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public NoRetryJob(T job)
{
Job = job;
Job.Error += LogError;
Job.Info += LogInfo;
}
}
This is how I am doing the registration with autofac
builder.RegisterGeneric(typeof(NoRetryJob<>)).InstancePerDependency();
builder.RegisterType<CachingService>().As<ICachingService>();
Calling the above class as
JobRegistration.Add<NoRetryJob<PageCachingJob>>(nameof(PageCachingJob), j => j.Run(), Cron.Daily(1));
PageCachingJob class -
public class PageCachingJob : BaseJob, IRecurringJob
{
private readonly ICachingService _cachingService;
public DateTime? LastSuccessfulExecutionOn { get; set; }
public PageCachingJob(ICachingService cachingService) : base()
{
_cachingService = cachingService;
}
protected override void RunJob()
{
_cachingService.ClearRedisCache();
}
}
public class JobRegistration
{
public static void Add<T>(string jobName, Expression<Action<T>> methodCall, string cron)
{
RecurringJob.AddOrUpdate(jobName, methodCall, cron, TimeZoneInfo.Local);
}
}
BaseJob Class
public abstract class BaseJob
{
private static readonly Logger logger = LogManager.GetCurrentClassLogger();
public event EventHandler<string> Info;
public event EventHandler<string> Error;
protected BaseJob()
{
}
protected abstract void RunJob();
public virtual JobResponse Run()
{
var stopwatch = new Stopwatch();
var response = new JobResponse();
try
{
logger.Info($"Job {this.GetType().Name} - started.");
stopwatch.Start();
RunJob();
stopwatch.Stop();
logger.Info($"Job {this.GetType().Name} - successful.");
}
catch (Exception ex)
{
response.Success = false;
response.LogMessages.Add("Exception" + ex.Message);
response.LogMessages.Add("Stacktrace : " + ex.StackTrace);
logger.Error($"Job {this.GetType().Name} - failure." + ex);
LogError(response.LogMessages, $"Job {this.GetType().Name} - failure.", ex);
}
logger.Info($"Job {this.GetType().Name} - duration: {stopwatch.Elapsed:g}.");
return response;
}
private void LogError(IList<string> messages, string message, Exception ex)
{
messages.Add(DateTime.Now + ": ERROR " + message);
Error?.Invoke(this, message + " " + ex.Message);
}
}
Interface -
public interface IRecurringJob
{
event EventHandler<string> Info;
event EventHandler<string> Error;
DateTime? LastSuccessfulExecutionOn { get; set; }
JobResponse Run();
}
ICachingService interface
public interface ICachingService
{
bool ClearRedisCache();
}
CachingService class
public class CachingService : ICachingService
{
public CachingService()
{
}
public bool ClearRedisCache()
{
return true;
}
}
Getting the below exception
None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'PacDig.Atlantic.Schedule.Jobs.NoRetryJob`1[PacDig.Atlantic.Schedule.Jobs.PageCachingJob]' can be invoked with the available services and parameters: Cannot resolve parameter 'PacDig.Atlantic.Schedule.Jobs.PageCachingJob job' of constructor 'Void .ctor(PacDig.Atlantic.Schedule.Jobs.PageCachingJob)'.
Thanks in advance