14

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?

Muhammad zubair
  • 291
  • 2
  • 6
  • 20

2 Answers2

19

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.

UNeverNo
  • 549
  • 3
  • 8
  • 29
Jambor - MSFT
  • 3,175
  • 1
  • 13
  • 16
11

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)
Justin Patten
  • 1,059
  • 6
  • 16
  • Thank you, this works. Is there a way to check when the web job finished because I have to notify the user about that? – Dacili Jun 25 '20 at 16:20