Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run .exe executable file in Azure Function

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

like image 944
Ves Avatar asked Jul 27 '17 10:07

Ves


2 Answers

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

like image 108
Fei Han Avatar answered Sep 22 '22 15:09

Fei Han


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}");
}
like image 20
Ves Avatar answered Sep 20 '22 15:09

Ves