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

Graham
  • 7,431
  • 18
  • 59
  • 84
Ves
  • 347
  • 1
  • 3
  • 8

3 Answers3

34

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

Fei Han
  • 26,415
  • 1
  • 30
  • 41
  • 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) – Ves Aug 01 '17 at 14:53
  • 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? – Void Mar 30 '18 at 23:45
  • 3
    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? – Ryan Sep 09 '18 at 21:48
  • How do you know what drive and folder the .exe will be deployed to? This seems very fragile and unreliable. – Naikrovek Mar 20 '19 at 14:47
  • 2
    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? – Jamshaid K. Jul 19 '19 at 10:09
  • @JamshaidKamran DId you found any solution? I also want to add exe file using visual studio 2019. – Yogen Darji Apr 16 '20 at 08:01
  • @Ryan 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 ? – Yogen Darji Apr 16 '20 at 08:03
  • 1
    @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. – Ryan Apr 17 '20 at 01:25
  • 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\ folder – Yossi Dahan Jun 08 '20 at 06:42
  • how to send a file as input parameter to the exe ? – roney Oct 01 '20 at 16:42
  • @YossiDahan can you describe your steps in an answer – Justin Feb 12 '21 at 15:48
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}");
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Ves
  • 347
  • 1
  • 3
  • 8
  • 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. – Fei Han Aug 02 '17 at 01:20
  • 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. – Ves Aug 02 '17 at 08:13
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

Izumi kana
  • 11
  • 1
  • 3
  • This does not really answer the question. If you have a different question, you can ask it by clicking [Ask Question](https://stackoverflow.com/questions/ask). To get notified when this question gets new answers, you can [follow this question](https://meta.stackexchange.com/q/345661). Once you have enough [reputation](https://stackoverflow.com/help/whats-reputation), you can also [add a bounty](https://stackoverflow.com/help/privileges/set-bounties) to draw more attention to this question. - [From Review](/review/late-answers/32106905) – Ravikumar B Jul 01 '22 at 08:42
  • 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 – Izumi kana Jul 02 '22 at 21:26