Skip to content

Instantly share code, notes, and snippets.

@rahulbagal
Created April 17, 2018 19:29
Show Gist options
  • Save rahulbagal/65c30b6e2d95d503da5c0e7eefa44701 to your computer and use it in GitHub Desktop.
Save rahulbagal/65c30b6e2d95d503da5c0e7eefa44701 to your computer and use it in GitHub Desktop.
Azure Triggered Webjob - When you programatically want to start an azure webjob , you can sent a call to WEB HOOK . Below function call webhook URL with HTTP Basic authentication.
static void WakeupWebjob()
{
var jobUrl = ConfigurationManager.AppSettings["sms-webjob-trigger-url"];
var jobUser = ConfigurationManager.AppSettings["sms-webjob-trigger-user"];
var jobPwd = ConfigurationManager.AppSettings["sms-webjob-trigger-pwd"];
Uri jobUri = new Uri(jobUrl);
var request = (HttpWebRequest)WebRequest.Create(jobUrl);
var postData = "";
var data = Encoding.ASCII.GetBytes(postData);
request.Method = "POST";
request.ContentType = "application/json";
request.ContentLength = data.Length;
NetworkCredential myNetworkCredential = new NetworkCredential(jobUser, jobPwd);
CredentialCache myCredentialCache = new CredentialCache();
myCredentialCache.Add(jobUri, "Basic", myNetworkCredential);
request.PreAuthenticate = true;
request.Credentials = myCredentialCache;
using (var stream = request.GetRequestStream())
{
stream.Write(data, 0, data.Length);
}
try
{
var response = (HttpWebResponse)request.GetResponse();
var responseString = new StreamReader(response.GetResponseStream()).ReadToEnd();
if (response.StatusCode.ToString().ToUpper() == "Accepted".ToUpper())
{
Console.WriteLine(responseString);
Console.Write("Job Started");
}
}
catch (Exception Ex)
{
if (Ex.Message.IndexOf("409") > 0)
{
Console.Write("Job was already running.");
}
else
{
Console.Write(Ex.Message.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment