Run .exe executable file in Azure Function
Asked Answered
G

4

23

I have executable abcd.exe (it contains/merged with many .dll). Is it possible to create Azure Function for abcd.exe and run it in Azure Cloud Functions?

The abcd.exe application :

System.Diagnostics.Process process = new System.Diagnostics.Process();

System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();

startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;

startInfo.FileName = "cmd.exe";

**startInfo.Arguments = "/C abcd.exe";**

process.StartInfo = startInfo;

process.Start();

The abcd.exe application does not have UI (GUI), but it is math and scientific Application and it depend from many .dll which are merged inside abcd.exe.

Thank you

Glen answered 27/7, 2017 at 10:43 Comment(0)
J
35

Is it possible to create Azure Function for abcd.exe and run it in Azure Cloud Functions?

Yes, we can upload .exe file and run it in Azure Function app. I create a TimerTrigger Function app, and run a .exe that insert record into database in this Function app, which works fine on my side.

function.json

{
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */1 * * * *"
    }
  ],
  "disabled": false
}

run.csx

using System;

public static void Run(TimerInfo myTimer, TraceWriter log)
{
    System.Diagnostics.Process process = new System.Diagnostics.Process();
    process.StartInfo.FileName = @"D:\home\site\wwwroot\TimerTriggerCSharp1\testfunc.exe";
    process.StartInfo.Arguments = "";
    process.StartInfo.UseShellExecute = false;
    process.StartInfo.RedirectStandardOutput = true;
    process.StartInfo.RedirectStandardError = true;
    process.Start();
    string output = process.StandardOutput.ReadToEnd();
    string err = process.StandardError.ReadToEnd();
    process.WaitForExit();

    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}

Upload .exe file to Function app folder

enter image description here

Main code in my .exe program

SqlConnection cn = new SqlConnection("Server=tcp:{dbserver}.database.windows.net,1433;Initial Catalog={dbname};Persist Security Info=False;User ID={user_id};Password={pwd};MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;");
cn.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = cn;

cmd.CommandText = "insert into [dbo].[debug]([Name]) values('test')";

cmd.ExecuteNonQuery();
cn.Close();

Query the database, I can find the test record is inserted from my .exe file. enter image description here

Jesselyn answered 28/7, 2017 at 7:13 Comment(11)
It is compiling function without any problem, but it is not running (in this case I don't have any result) 2017-08-01T14:50:00.009 Function started (Id=~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~) 2017-08-01T14:50:00.208 C# Timer trigger function executed at: 8/1/2017 2:50:00 PM 2017-08-01T14:50:00.208 Function completed (Success, Id=####################, Duration=200ms)Glen
What if you installed some nuget packages in your console application. Now I created project.json in Function app root level, but I still get error message "System.IO.FileNotFoundException: Could not load file or assembly". Any idea how to fix this?Celtuce
Is there a way to include an exe, like in this question, as part of your visual studio project so that it publishes to azure along with the Function code?Mckenna
How do you know what drive and folder the .exe will be deployed to? This seems very fragile and unreliable.Expectoration
I am not allowed to upload the file because I published the function using Visual Studio. Any other way to push the exe files so it can be run?Cavorilievo
@JamshaidKamran DId you found any solution? I also want to add exe file using visual studio 2019.Listen
@Mckenna did you find any way to include exe file as a part of solution in visual studio so after publish function app I can use exe ?Listen
@yogendarji no not as part of the solution / publish. you can do it that way, I believe, though. I manually placed our dependencies on to the function host.Mckenna
to push the exe to the functions from visual studio, add the csproj file to ensure it gets copied to the output directory - I've added one, copying the host.json entry and modifying the filename; the file ended up in the D:\home\site\wwwroot\ folderWaggon
how to send a file as input parameter to the exe ?Ascetic
@YossiDahan can you describe your steps in an answerDissident
G
7

Fred Thank you very much for your help. Now, with minor modification the application is compiling and running.

Some minor modifications to the application:

using System;

using System.Diagnostics;

using System.Threading;


public static void Run(TimerInfo myTimer, TraceWriter log)
{

    System.Diagnostics.Process process = new System.Diagnostics.Process();
    string WorkingDirectoryInfo =@"D:\home\site\wwwroot\TimerTriggerCSharp1";
    string ExeLocation = @"D:\home\site\wwwroot\TimerTriggerCSharp1\MyApplication.exe";
    Process proc = new Process();
    ProcessStartInfo info = new ProcessStartInfo();

    try
    {
    info.WorkingDirectory = WorkingDirectoryInfo;
    info.FileName = ExeLocation;
    info.Arguments = "";
    info.WindowStyle = ProcessWindowStyle.Minimized;
    info.UseShellExecute = false;
    info.CreateNoWindow = true;
    proc.StartInfo = info;
    proc.Refresh();
    proc.Start();
    proc.WaitForInputIdle();
    proc.WaitForExit();
    }
    catch
    {
    }
    log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
}
Glen answered 1/8, 2017 at 16:28 Comment(2)
Hi @Ves, if my reply is helpful, you can mark it as answer (or vote for it), which will help others quickly find this thread and solve the similar problems.Jesselyn
Hi @Fred, your reply was helpful and I did vote for it. I am new user and this message is shown: Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score.Glen
B
1

no problem if you just run it in local machine, but run in azure, if will failed unfortunately . I'm simply tell everybody the truth, which is impossible to Run .exe executable file in Azure Function, so stop wasting time on trying figure out this topic, it took me 3 days to realized that the azure function is kind of nanoserver which everything in sandbox nothing else can be executed from inside

Banbury answered 26/6, 2022 at 2:58 Comment(2)
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewDurga
I'm simply tell everybody the truth, which is impossible to Run .exe executable file in Azure Function, so stop wasting time on trying figure out this topic, it took me 3 days to realized that the azure function is kind of nanoserver which everything in sandbox nothing else can be executed from insideBanbury
P
0

It is possibile to run an exe file in an azure function app.

Make sure that

  • it's compatible with the app environment. (ie for Windows runtime, Windows compatible exe built)

  • ProcessStartInfo class is ofter tricky, I suggest Medallion Shell or Cli.Wrap library, you can pipe input and outputs to streams.

  • I tried within an isolated process, it works

          var cmd = Cli.Wrap("a.exe")
              .WithArguments(args)
              .WithStandardInputPipe(PipeSource.FromStream(_src))
              .WithStandardOutputPipe(PipeTarget.ToStream(outputStream))
              .WithValidation(CommandResultValidation.None);
    
Preterite answered 1/11, 2023 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.