12
votes

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.

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?

2

2 Answers

16
votes

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.

11
votes

You could trigger the WebJob via the WebJob API. C# code included in the following 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)