如何在运行时从appsettings.json读取CRON表达式?

问题描述 投票:1回答:1

我开发了具有多个定时器触发功能的web作业项目。每个功能都在一天中的不同时间执行。其中一些是每分钟执行一次,一些每五分钟执行一次,其中一些是一天一次。

如果我在函数参数属性本身中编写CRON表达式就像 ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo),它按预期工作,但当我尝试从appsettings.json读取它失败。

有人能建议在functions.cs中读取appsettings.json的正确方法吗?

以下是我正在尝试的代码。

Program.cs中

internal class Program
   {
        // Please set the following connection strings in app.config for this WebJob to run:
        // AzureWebJobsDashboard and AzureWebJobsStorage
        private static void Main()
        {
            ServiceCollection services = new ServiceCollection();
            ConfigureServices(services);

            var config = new JobHostConfiguration();
            config.JobActivator = new JobActivator(services.BuildServiceProvider());
            config.UseTimers();

            if (config.IsDevelopment)
            {
                config.UseDevelopmentSettings();
            }

            var host = new JobHost(config);
            // The following code ensures that the WebJob will be running continuously
            host.RunAndBlock();
        }

        private static IConfiguration Configuration { get; set; }

        private static void ConfigureServices(IServiceCollection services)
        {
            var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

            Configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
                .AddJsonFile($"appsettings.{environment}.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

            services.AddSingleton(Configuration);
            services.AddTransient<Functions, Functions>();
            services.AddLogging(builder => builder.AddConsole());
        }
    }

Functions.cs

public class Functions
    {
        private readonly ILogger<Functions> logger;

        private readonly IConfiguration configuration;

        public Functions(ILogger<Functions> logger, IConfiguration configuration)
        {
            this.configuration = configuration;
            this.logger = logger;
        }


        [FunctionName("TimerTriggerEveryMinute")]
        public void ProcessTrigger1([TimerTrigger("%TimerTriggerEveryMinute:Schedule%")]TimerInfo timerInfo)
        {
            var ab = this.configuration;
            Console.WriteLine(string.Format("{0} Proc 1",DateTime.Now));
        }

        [FunctionName("TimerTriggerEveryThirdMinute")]
        public void ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo)
        {
            Console.WriteLine(string.Format("{0} Proc 2",DateTime.Now));
        }

        [FunctionName("TimerTriggerEveryFiveMinute")]
        public void ProcessTrigger3([TimerTrigger("%EveryFiveMinuteSchedule%",RunOnStartup =false)]TimerInfo timer)
        {
            Console.WriteLine(string.Format("{0} Proc 5", DateTime.Now));
        }
}

JobActivator.cs

 public class JobActivator : IJobActivator
    {
        private readonly IServiceProvider services;

        public JobActivator(IServiceProvider services)
        {
            this.services = services;
        }

        public T CreateInstance<T>()
        {
            return services.GetService<T>();
        }
    }

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "ConnectionStrings": {
    "AzureWebJobsDashboard": "My Azure Dashboard key here...",
    "AzureWebJobsStorage": "My Azure Job Storage key here...",
  },
  "TimerTriggerEveryMinute": {
    "Schedule": "0 * * * * *"
  },
  "TimerTriggerEveryFiveMinute": {
    "Schedule": "0 */5 * * * *"
  }
}

在上面的代码中,在Functions.cs中,如果我在中写入定时器触发器 ProcessTrigger2([TimerTrigger("0 */3 * * * *", RunOnStartup = false)]TimerInfo timerInfo)这种方式适用于所有方法,工作按预期工作,但如果我用其他两种方式写,它给了我例外。请建议从appsettings.json读取时间表的书面方式。

azure azure-webjobs timer-trigger
1个回答
1
投票

是。而不是硬编码CRON表达式,将表达式放在应用程序设置中,并使用%符号引用它。例如,如果您有一个名为CRON_EXPRESSION的设置:

public static void Run([TimerTrigger(“%CRON_EXPRESSION%”)] TimerInfo myTimer,TraceWriter log)

请参阅 - https://github.com/Azure/azure-functions-host/issues/1934

© www.soinside.com 2019 - 2024. All rights reserved.