Program.cs中:
public static void Main(string[] args)
{
var builder = new HostBuilder()
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
})
.UseConsoleLifetime();
var host = builder.Build();
using (host)
{
host.Run();
}
}
Functions.cs:
public class Functions
{
private readonly ISampleServiceA _sampleServiceA;
private readonly ISampleServiceB _sampleServiceB;
public Functions(ISampleServiceA sampleServiceA, ISampleServiceB sampleServiceB)
{
_sampleServiceA = sampleServiceA;
_sampleServiceB = sampleServiceB;
}
public static void RunSomething([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log) // every one minute
{
if (myTimer.IsPastDue)
{
log.LogInformation("Timer is running late!");
}
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
当我跑,我得到:
dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
Hosting starting
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
Hosting started
我已经通过几个例子看和我的印象是,通过创建一个新的控制台应用程序,引用所需的程序集,和包括上述情况,应该WebJob“只是工作”下。我是否忽略了一些重要的配置?
版本:
你错过AddTimers()的配置,因为它是一个时间触发功能。
对您项目,请安装包Microsoft.Azure.WebJobs.Extensions
,然后在您的Program.cs - > Main方法:添加AddTimers到webjob配置,如下图所示:
var builder = new HostBuilder()
.ConfigureWebJobs(
b =>
{
b.AddTimers();
//other configure
})
.ConfigureLogging((context, b) =>
{
b.SetMinimumLevel(LogLevel.Debug);
b.AddConsole();
})
// your other configure
.UseConsoleLifetime();
您也可以参考我previous answer为webjob 3.x的更多配置