web-dev-qa-db-fra.com

Exécutez le fichier exécutable .exe dans la fonction Azure

J'ai exécutable abcd.exe (il contient/a fusionné avec de nombreux .dll). Est-il possible de créer Azure Function pour abcd.exe et de l'exécuter dans Azure Cloud Functions?

L'application abcd.exe:

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();

L'application abcd.exe n'a pas d'interface utilisateur (GUI), mais c'est une application mathématique et scientifique et elle dépend de nombreux fichiers .dll qui sont fusionnés dans abcd.exe.

Je vous remercie

15
Ves

Est-il possible de créer Azure Function pour abcd.exe et de l'exécuter dans Azure Cloud Functions?

Oui, nous pouvons télécharger un fichier .exe et l'exécuter dans l'application Azure Function. Je crée une application TimerTrigger Function et exécute un .exe qui insère un enregistrement dans la base de données de cette application Function, ce qui fonctionne bien de mon côté.

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}");
}

Téléchargez le fichier .exe dans le dossier de l'application Function

enter image description here

Code principal de mon programme .exe

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();

Recherchez la base de données, je peux trouver que l'enregistrement de test est inséré à partir de mon fichier .exe. enter image description here

25
Fei Han

Fred Merci beaucoup pour votre aide. Maintenant, avec une modification mineure, l'application se compile et s'exécute.

Quelques modifications mineures à l'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}");
}
4
Ves