从代码运行触发的Azure WebJob

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

搜索后我得到了一个解决方案来触发预定的http://blog.davidebbo.com/2015/05/scheduled-webjob.html

知道如何从代码运行吗?

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

HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create("https://.scm.azurewebsites.net/api/triggeredwebjobs//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) { } 

它适用于我。 希望能帮助到你。

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

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)