在Azure函数中运行.exe可执行文件

fv2wmkja  于 2023-11-21  发布在  其他
关注(0)|答案(4)|浏览(151)

我有可执行文件abcd.exe(它包含/合并了许多.dll)。是否可以为abcd.exe创建Azure Function并在Azure Cloud Functions中运行它?
abcd.exe应用程序:

  1. System.Diagnostics.Process process = new System.Diagnostics.Process();
  2. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
  3. startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
  4. startInfo.FileName = "cmd.exe";
  5. **startInfo.Arguments = "/C abcd.exe";**
  6. process.StartInfo = startInfo;
  7. process.Start();

字符串
abcd.exe应用程序没有UI(GUI),但它是数学和科学应用程序,它依赖于合并在abcd.exe内的许多.dll。
谢谢你

a1o7rhls

a1o7rhls1#

是否可以为abcd.exe创建Azure Function并在Azure Cloud Functions中运行它?
是的,我们可以上传.exe文件并在Azure Function应用程序中运行它。我创建了一个TimerTrigger Function应用程序,并在此Function应用程序中运行将记录插入数据库的.exe,这在我这边工作得很好。

function.json

  1. {
  2. "bindings": [
  3. {
  4. "name": "myTimer",
  5. "type": "timerTrigger",
  6. "direction": "in",
  7. "schedule": "0 */1 * * * *"
  8. }
  9. ],
  10. "disabled": false
  11. }

字符串

运行.csx

  1. using System;
  2. public static void Run(TimerInfo myTimer, TraceWriter log)
  3. {
  4. System.Diagnostics.Process process = new System.Diagnostics.Process();
  5. process.StartInfo.FileName = @"D:\home\site\wwwroot\TimerTriggerCSharp1\testfunc.exe";
  6. process.StartInfo.Arguments = "";
  7. process.StartInfo.UseShellExecute = false;
  8. process.StartInfo.RedirectStandardOutput = true;
  9. process.StartInfo.RedirectStandardError = true;
  10. process.Start();
  11. string output = process.StandardOutput.ReadToEnd();
  12. string err = process.StandardError.ReadToEnd();
  13. process.WaitForExit();
  14. log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
  15. }

上传.exe文件到Function app文件夹


的数据

我的.exe程序中的主代码

  1. 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;");
  2. cn.Open();
  3. SqlCommand cmd = new SqlCommand();
  4. cmd.Connection = cn;
  5. cmd.CommandText = "insert into [dbo].[debug]([Name]) values('test')";
  6. cmd.ExecuteNonQuery();
  7. cn.Close();

查询数据库,可以发现测试记录是从我的.exe文件中插入的。

展开查看全部
k3fezbri

k3fezbri2#

非常感谢您的帮助。现在,经过微小的修改,应用程序正在编译和运行。
对应用程序的一些小修改:

  1. using System;
  2. using System.Diagnostics;
  3. using System.Threading;
  4. public static void Run(TimerInfo myTimer, TraceWriter log)
  5. {
  6. System.Diagnostics.Process process = new System.Diagnostics.Process();
  7. string WorkingDirectoryInfo =@"D:\home\site\wwwroot\TimerTriggerCSharp1";
  8. string ExeLocation = @"D:\home\site\wwwroot\TimerTriggerCSharp1\MyApplication.exe";
  9. Process proc = new Process();
  10. ProcessStartInfo info = new ProcessStartInfo();
  11. try
  12. {
  13. info.WorkingDirectory = WorkingDirectoryInfo;
  14. info.FileName = ExeLocation;
  15. info.Arguments = "";
  16. info.WindowStyle = ProcessWindowStyle.Minimized;
  17. info.UseShellExecute = false;
  18. info.CreateNoWindow = true;
  19. proc.StartInfo = info;
  20. proc.Refresh();
  21. proc.Start();
  22. proc.WaitForInputIdle();
  23. proc.WaitForExit();
  24. }
  25. catch
  26. {
  27. }
  28. log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
  29. }

字符串

展开查看全部
vwkv1x7d

vwkv1x7d3#

如果你只是在本地机器上运行它没有问题,但在Azure上运行,如果会失败,不幸的是。我只是告诉大家真相,这是不可能的Azure函数中运行.exe可执行文件,所以不要浪费时间试图弄清楚这个主题,我花了3天时间才意识到,Azure函数是一种纳米服务器,所有的东西都在沙箱中,没有其他东西可以从内部执行

tkclm6bt

tkclm6bt4#

可以在Azure函数应用程序中运行exe文件。
确保

  • 它与应用程序环境兼容.(即为Windows运行时,Windows兼容exe内置)
  • ProcessStartInfo类通常比较棘手,我建议使用Medallion Shell或Cli.Wrap库,你可以将输入和输出通过管道传输到流中。
  • 我在一个孤立的进程中尝试,它工作
  1. var cmd = Cli.Wrap("a.exe")
  2. .WithArguments(args)
  3. .WithStandardInputPipe(PipeSource.FromStream(_src))
  4. .WithStandardOutputPipe(PipeTarget.ToStream(outputStream))
  5. .WithValidation(CommandResultValidation.None);

字符串

相关问题