从代码运行触发的Azure WebJob
我创建了一个作为Azure触发器Webjob上传的控制台应用程序.当我从Azure Portal运行它时,它运行良好.我想从我的C#代码中运行它.我不想使用队列或服务总线.我只想在用户在我的Web应用程序中执行特定操作时触发它.
I created a console application upload as Azure trigger Webjob. It is working fine when I run it from Azure Portal. I want to run this from my C# code. I don't want to use Queue or service bus. I just want to trigger it when user perform a specific action in my web app.
搜索后,我得到了一个解决方案,可以按计划触发作业 http://blog.davidebbo.com/2015/05/scheduled-webjob.html
After searching I got a solution to trigger job from a scheduled http://blog.davidebbo.com/2015/05/scheduled-webjob.html
有人知道如何从代码运行吗?
Any idea how to run from code?
正如Justin所说,我们可以使用WebJob API来达到此要求.我们可以在以下位置找到此KUDU API: https://github.com/projectkudu/kudu/wiki/WebJobs-API .下面是我经过测试的代码:
As Justin said, we can use WebJob API to achieve this requirement. We could find this KUDU API at: https://github.com/projectkudu/kudu/wiki/WebJobs-API. Below is my tested code:
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) {
}
它对我有效.希望对您有所帮助.
It works on my side. Hope it helps.