Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run triggered Azure WebJob from Code

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?

like image 450
Muhammad zubair Avatar asked Jul 13 '16 08:07

Muhammad zubair


1 Answers

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.

like image 55
Jambor - MSFT Avatar answered Oct 04 '22 13:10

Jambor - MSFT