在 C# 中停用和激活 Azure WebJobs

Deactivate and activate Azure WebJobs in C#

我有一个问题,我想在部署新版本时停用应用服务的网络作业,并在再次部署后激活它们。

在 c# 中有可能做到这一点吗?

谢谢大家

要从外部客户端停止 WebJob,您只需进行 REST 调用:https://github.com/projectkudu/kudu/wiki/WebJobs-API#stop-a-continuous-job

 POST https://{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/stop

这将添加 disabled.job

的文件

再次启动 WebJob

   POST https://{sitename}.scm.azurewebsites.net/api/continuouswebjobs/{job name}/start

这将删除禁用的作业文件,webjob 将再次 运行

据我所知,您无法直接停止触发的 WebJob,您需要利用进程资源管理器通过 Web 应用程序的 KUDU. For the continuous WebJobs, you could leverage WebJobs API to start/stop the WebJobs, you need to invoke the specificed Rest API with basic auth using Deployment credentials 将其终止。这是停止 WebJob 的 C# 代码片段:

string username = "{username}";
string password = "{password}";
string jobname = "{your-webjob-name}";
string authorization = Convert.ToBase64String(System.Text.UTF8Encoding.UTF8.GetBytes($"{username}:{password}"));
using (var client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", authorization);
    var res = await client.PostAsync($"https://{your-webapp-name}.scm.azurewebsites.net/api/continuouswebjobs/{jobname}/stop", null);
    Console.WriteLine($"StatusCode:{res.StatusCode}");
}

注意:此时,将在您的 WebJob 中添加一个名为 disable.job 的文件,如下所示:

要启动 WebJob,只需调用 /api/continuouswebjobs/{job name}/start,然后 disable.job 文件将被删除,您的 WebJob 将再次 运行。