Reputation: 1961
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!
Upvotes: 0
Views: 318
Reputation: 1961
The reason for such did not work accordingly, is that the Zip package did have my own run.cmd.
Prior to the resolution I had in my Zip Package are:
- run.cmd
- *.dlls
- xxxx.EXE
To resolve I had to remove the run.cmd
in my Zip package and it worked! :)
Upvotes: 0
Reputation: 43193
Passing argument that way should work fine. Please test with a minimal app, since everything you're doing related to EmailJobExecution has no bearing on the question and should be omitted. Also, your client code is not relevant to the question if you also repro with postman, so please leave that out.
e.g. I just tried with a trivial batch file containing just:
echo Hello Azure WebJob!
echo command line arg %1
echo env variable %WEBJOBS_COMMAND_ARGUMENTS%
And both %1
and %WEBJOBS_COMMAND_ARGUMENTS%
correctly have the passed in value.
Upvotes: 2