gpt4 book ai didi

c# - Azure C# Webjob TimeTrigger 未触发

转载 作者:行者123 更新时间:2023-12-03 03:06:28 26 4
gpt4 key购买 nike

我正在尝试创建一个 Azure WebJob,它将在设定的计时器上执行 Functions.Methods。我在 VS2017 中设置了一个简单的 WebJob 项目,并添加了 Microsoft.Azure.Webjobs.Extensions 以允许我引用计时器。

作为一个学习过程,我正在尝试创建一个 WebJob,它将调用函数方法,以便在 TimerTrigger 触发时向我的地址发送电子邮件(例如,每 60 秒发送一封电子邮件)

我的主要空白看起来像这样:

public static void Main()
{
var config = new JobHostConfiguration();

config.Tracing.ConsoleLevel = System.Diagnostics.TraceLevel.Verbose;


config.UseTimers();

var host = new JobHost();
host.RunAndBlock();
}

我想调用的 Function 方法如下所示:

    [NoAutomaticTrigger]    //See below note on [NoAutomaticTrigger]
public static void SendMail([TimerTrigger("00:00:10", RunOnStartup = true)] TextWriter log)
{
log.WriteLine("[" + DateTime.Now.ToString() + "] Preparing to send Mail");
MailMessage Message = new MailMessage();
SmtpClient MailClient = new SmtpClient();

String FromAddress = "****";
String FromName = "****";
String Password = "****;
String ToAddress = "****";

MailClient.UseDefaultCredentials = false;
MailClient.Credentials = new System.Net.NetworkCredential(FromAddress, Password);
MailClient.Port = 587;
MailClient.Host = "****";
MailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
MailClient.EnableSsl = true;



Message.To.Add(ToAddress);
Message.From = new MailAddress(FromAddress, FromName);
Message.Subject = "WebJob | Test Mail";
Message.Body = "TimerTrigger Function Sendmail triggered at: " + DateTime.Now.ToString();
MailClient.Send(Message);
log.WriteLine("[" + DateTime.Now.ToString() + "] Mail Successfully Sent");
}

现在,如果我在本地调试上述内容,我会看到 this

SendMail 方法永远不会触发。

现在,如果我将其作为 WebJob 上传到 azure,然后导航到 WebJob 仪表板/函数/函数调用日志和“运行函数”,我很快就会从 My SendMail 方法收到一封电子邮件,其中包含以下输出:

[5/20/2017 11:47:31 AM] Preparing to send Mail

[5/20/2017 11:47:32 AM] Mail Successfully Sent

回复:[无自动触发]

我的一个建议是从 SendMail 方法中删除 [NoAutomaticTrigger] 属性,因为这会告诉作业主机忽略计时器,但如果我这样做,我会得到:

No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

所以问题是,当在 Azure 中作为 WebJob 运行时,如何让我的 sendmail 方法在指定的 [TimerTrigger] 上触发

提前致谢

最佳答案

So the question is, how can I make my sendmail method fire on the specified [TimerTrigger] when running in Azure as a WebJob

根据您的代码,我发现您错过了将配置参数传递给JobHost。

var host = new JobHost(config);

此外,确实需要删除 NoAutomaticTrigger 属性,并且应将 TimerTrigger 属性应用于类型为 TimerInfo 的参数。以下代码供您引用。

public static void SendMail([TimerTrigger("00:00:10")] TimerInfo timer, TextWriter log)

之后,TimerTrigger 将起作用。

enter image description here

关于c# - Azure C# Webjob TimeTrigger 未触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44085586/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com