I'm invoking a webjob from my WebApp hosted in an App Service Environment. The code I used to invoke is below:
string userName = "$xxxx";
string userPassword = "xxxxx";
string webJobName = "xxxx";
var unEncodedString = String.Format($"{ userName}:{ userPassword}");
var encodedString = Convert.ToBase64String(System.Text.Encoding.ASCII.GetBytes(unEncodedString));
string URL = string.Format("https://xxxx.scm.azurewebsites.net/api/triggeredwebjobs/" + webJobName + "/run?arguments={0}", jobId.ToString());
System.Net.WebRequest request = System.Net.WebRequest.Create(URL);
request.Method = "POST";
request.ContentLength = 0;
request.Headers["Authorization"] = "Basic " +encodedString;
response = request.GetResponse();
I am invoking a webjob which is a console application. How I handle the argument in the console application is as:
static void Main(string[] args)
{
//
var id = 0;
if (args.Length > 0)
{
if (!int.TryParse(args[0].ToString(), out id))
id = 0;
}
if (id > 0)
{
EmailJobExecution emailServ = new EmailJobExecution();
EmailJobDBContext jobDB = new EmailJobDBContext();
var jobs = jobDB.getActiveJob(id);
List<string> logs = new List<string>();
emailServ.ExecuteJob(jobs, out logs);
foreach (var item in logs)
{
Console.WriteLine(item);
}
}
}
The problem is I can see in the Webjob dashboard the webjob is been invoked but the arguments aren't passed. The Log displays as:
[05/30/2017 11:18:51 > 738495: SYS INFO] Status changed to Initializing
[05/30/2017 11:18:51 > 738495: SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[05/30/2017 11:18:51 > 738495: SYS INFO] Status changed to Running
[05/30/2017 11:18:51 > 738495: INFO]
[05/30/2017 11:18:51 > 738495: INFO] D:\local\Temp\jobs\triggered\xxxxx\rwjgkw42.ann>xxxx.EXE
[05/30/2017 11:18:51 > 738495: SYS INFO] Status changed to Success
If I went to App Service Editor and amend the run.cmd like this: xxxx.EXE 1
(1 is argument) then I the console takes argument and log like this:
[05/30/2017 10:17:26 > 738495: SYS INFO] Status changed to Initializing
[05/30/2017 10:17:27 > 738495: SYS INFO] Run script 'run.cmd' with script host - 'WindowsScriptHost'
[05/30/2017 10:17:27 > 738495: SYS INFO] Status changed to Running
[05/30/2017 10:17:27 > 738495: INFO]
[05/30/2017 10:17:27 > 738495: INFO] D:\local\Temp\jobs\triggered\xxxx\bvck11x1.f3v>xxx.EXE 1
[05/30/2017 10:17:32 > 738495: INFO] 5/30/2017 10:17:27 AM: Job No. 1 Started
[05/30/2017 10:17:32 > 738495: INFO] Message Count: 1
[05/30/2017 10:17:32 > 738495: INFO] 1:OK
[05/30/2017 10:17:32 > 738495: INFO] Status Count: 1
[05/30/2017 10:17:32 > 738495: INFO] 5/30/2017 10:17:32 AM: Job No. 1 Completed
[05/30/2017 10:17:32 > 738495: SYS INFO] Status changed to Success
The Argument does not pass even if I run the URL in Postman.
Please help me resolve this.
Update 31st May 2017:
The Zip Package for Webjobs had the following:
- run.cmd
- xxxx.EXE
and as @SuwatCh guided me, I removed the
run.cmd
and leaving only the*.exe
in Zip Package and then created the WebJob and finally it worked!
run.cmd
leaving only the*.exe
and it worked! Glad you gave me something to think :) – hiFI