运行 从代码中触发了 Azure WebJob

Run triggered Azure WebJob from Code

我创建了一个控制台应用程序上传作为 Azure 触发器 Webjob。当我从 Azure 门户 运行 它时,它工作正常。我想从我的 C# 代码中 运行 这个。我不想使用队列或服务总线。我只想在用户在我的网络应用程序中执行特定操作时触发它。

经过搜索,我得到了一个解决方案,可以从预定的时间触发作业 http://blog.davidebbo.com/2015/05/scheduled-webjob.html

知道如何从代码中 运行 吗?

您可以通过 WebJob API 触发 WebJob。以下post包含的C#代码:

http://chriskirby.net/blog/running-your-azure-webjobs-with-the-kudu-api

HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://mysiteslot.scm.azurewebsites.net/api/");
// the creds from my .publishsettings file
var byteArray = Encoding.ASCII.GetBytes("username:password");
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(byteArray));
// POST to the run action for my job
var response = await client.PostAsync("triggeredwebjobs/moJobName/run", null)

正如贾斯汀所说,我们可以使用 WebJob API 来实现这个需求。我们可以在 https://github.com/projectkudu/kudu/wiki/WebJobs-API 找到这个 KUDU API。下面是我测试过的代码:

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://<web appname>.scm.azurewebsites.net/api/triggeredwebjobs/<web job name>/run");
request.Method = "POST";
var byteArray = Encoding.ASCII.GetBytes("user:password"); //we could find user name and password in Azure web app publish profile 
request.Headers.Add("Authorization", "Basic "+ Convert.ToBase64String(byteArray));            
request.ContentLength = 0;
try
{
    var response = (HttpWebResponse)request.GetResponse();
}
catch (Exception e) {

}

它对我有效。希望对你有帮助。