Search code examples
azureazure-webjobsazure-worker-roles

How do I "shell out" to a binary in an Azure webjob?


I need to create an offline job processor which will call a console executable with a varying set of command line flags, based on a queue. I've created a new project with the "Azure WebJobs SDK: Queues" template in Visual Studio. I know that I can deploy this to Azure, and it will create a virtual machine and set it all up to work.

I already have a web "worker role" doing essentially the same sort of thing, but, in that case, I wrote the code that it calls, and have the source to a library my code needs. This time, I need to call a "canned" binary that someone else wrote, and to which I do not have access to the source.

I can create a subfolder in the project, and dump the executable (and all its folders and libraries) in this folder, but I wouldn't know how to call it from my webjob. What would be the path structure to the binary once deployed to the cloud service in Azure, if it's just sitting in a subfolder in the project?

Also, this binary really expects to live in C:\Software\<Directory>. Is is possible to specify this particular, external directory to be created on the virtual machine when this application is deployed to a cloud service in Azure? If I could do that, it would work better, and I'd know precisely how to call the executable.


Solution

  • As part of the worker role, you can create a subfolder, and put your binary in it. (You can't drag-and-drop a folder in Visual Studio when doing this, so, in my case, I had to create several subfolders, and drop files into them, in order to make my application work.)

    enter image description here

    When deployed, the worker role will then copy this folder of arbitrary files to the worker server in Azure, where it can then be called. When deployed, this arbitrary folder will be located in ...TASACDWS\TASACDWS\TASACDWS\csx\Debug\roles\TASACDWS_Worker\approot

    The "root" or "base" folder of the worker role itself is the current directory of the process. So, if my application lives in a folder called "Arbitrary_Binary" under the worker role (in Visual Studio), I can reference it with a Process object like so:

    Process p = new Process();
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.WorkingDirectory = "Arbitrary_Binary";
    p.StartInfo.FileName = "Arbitrary_Binary\\Arbitrary_Binary.exe";
    p.Start();
    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();