c# – Azure WebJob最佳方法
作者:互联网
这是我第一次使用WebJob类型的应用程序.我创建了一个webjob项目,在解决方案中它附带了Program.cs和Function.cs.
我已经删除了Function.cs,因为在这个项目中没有队列,我将从中获取数据.
现在在Program.cs中已有Main方法如下:
class Program
{
// Please set the following connection strings in app.config for this WebJob to run:
// AzureWebJobsDashboard and AzureWebJobsStorage
static void Main()
{
var host = new JobHost();
// The following code ensures that the WebJob will be running continuously
host.RunAndBlock();
}
}
据我所知,RunAndBlock是连续运行webjob,但我希望这份工作只运行一次.我想按计划从外部控制执行.我想知道如何让我的代码只运行一次?
如下所示,我有一个具有RunOnePoolProvisioingCycle的SupportService类,我只想调用此方法一次.
这是正确的方法吗?
static void Main()
{
SupportService _supportService = new SupportService();
_supportService.Initialize();
_supportService.SetPoolProvisioningConfigurations();
_supportService.RunOnePoolProvisioningCycle();
}
还是这个?
static void Main()
{
var host = new JobHost();
SupportService _supportService = new SupportService();
_supportService.Initialize();
_supportService.SetPoolProvisioningConfigurations();
host.Call(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}
还是这个?
static void Main()
{
var host = new JobHost();
SupportService _supportService = new SupportService();
_supportService.Initialize();
_supportService.SetPoolProvisioningConfigurations();
host.CallAsync(typeof(SupportService).GetMethod("SetPoolProvisioningConfigurations"));
}
或者我应该使用:
host.Start()
要么
host.StartAsync()?
解决方法:
你看到的是SDK的一部分,它是可选的.
webjob可以像您按原样Zip,上传和运行的控制台应用程序一样简单.
因此,此代码似乎是您的最佳选择:
static void Main()
{
SupportService _supportService = new SupportService();
_supportService.Initialize();
_supportService.SetPoolProvisioningConfigurations();
_supportService.RunOnePoolProvisioningCycle();
}
标签:c,azure,azure-webjobs,azure-webjobssdk 来源: https://codeday.me/bug/20190519/1136062.html