Почему CronSchedule не запускается при запуске
Я хочу создать веб-задание, которое сначала запускается при запуске, а затем запускается в определенное время ежедневно в соответствии с моими настройками в App.config. В App.config у меня есть свойство
<configuration>
...
<appSettings>
<add key="ScheduleCronExpression" value="0 0 6 * * *"/>
...
</appSettings>
...
</configuration>
Я создал индивидуальный CronSchedule.
public class CustomCronSchedule : CronSchedule
{
public CustomCronSchedule() :
base(GetCronExpression())
{
}
public static string GetCronExpression()
{
var result = (!DataFeedProviderInputValidator.IsEmptyValue(ConfigurationManager.AppSettings["ScheduleCronExpression"]) ? ConfigurationManager.AppSettings["ScheduleCronExpression"] : "0 30 9 * * *");
return result;
}
}
А вот и мой файл Program.cs.
public class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
private static readonly ILog log = LogManager.GetLogger(typeof(Program));
//other private variables
public Program(...)
{
//assign parameters to private variables
}
public static void Main()
{
JobHostConfiguration config = new JobHostConfiguration()
{
JobActivator = new MyActivator(UnityConfig.GetConfiguredContainer())
};
ConfigurationManager.AppSettings["CRMConnectionString"] = ConfigurationManager.ConnectionStrings["CRMConnectionString"].ConnectionString;
config.UseTimers();
var host = new JobHost(config);
host.RunAndBlock();
log.Info("Web job started with success.");
}
public void Run([TimerTrigger(typeof(CustomCronSchedule), RunOnStartup = true, UseMonitor = true)] TimerInfo timer)
{
//do something
}
}
Я обнаружил, что функция "Выполнить" веб-задания не была вызвана. Однако если заменить "typeof (CustomCronSchedule)" на что-то вроде "0 0 6 * * *", это будет работать как шарм. Интересно, можно ли вообще вызывать CronSchedule?