Created
April 17, 2018 19:29
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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