【问题标题】:Run .exe executable file in Azure Function在 Azure Function 中运行 .exe 可执行文件
【发布时间】:2017-07-27 10:43:47
【问题描述】:

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

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

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

谢谢

【问题讨论】:

    标签: c# azure azure-functions


    【解决方案1】:

    是否可以为 abcd.exe 创建 Azure Function 并在 Azure Cloud Functions 中运行它?

    是的,我们可以上传 .exe 文件并在 Azure Function 应用程序中运行它。我创建了一个 TimerTrigger Function 应用程序,并运行了一个 .exe,将记录插入到此 Function 应用程序中的数据库中,这对我来说运行良好。

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

    将 .exe 文件上传到函数应用文件夹

    我的 .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();
    

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

    【讨论】:

    • 它正在编译函数没有任何问题,但它没有运行(在这种情况下我没有任何结果) 2017-08-01T14:50:00.009 函数启动(Id=~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~) 2017-08-01T14:50:00.208 C# 定时器触发函数执行于: 2017 年 8 月 1 日下午 2:50:00 2017-08-01T14:50:00.208 功能完成(成功,ID=####################,持续时间= 200 毫秒)
    • 如果您在控制台应用程序中安装了一些 nuget 包会怎样。现在我在函数应用程序根级别创建了 project.json,但我仍然收到错误消息“System.IO.FileNotFoundException:无法加载文件或程序集”。知道如何解决这个问题吗?
    • 有没有办法像在这个问题中一样包含一个 exe,作为您的 Visual Studio 项目的一部分,以便它与函数代码一起发布到天蓝色?
    • 我不允许上传文件,因为我使用 Visual Studio 发布了函数。还有其他方法可以推送 exe 文件以便运行吗?
    • @yogendarji 不作为解决方案/发布的一部分。不过,我相信你可以那样做。我手动将我们的依赖项放在函数主机上。
    【解决方案2】:

    Fred 非常感谢您的帮助。现在,稍作修改,应用程序正在编译和运行。

    对应用程序的一些小修改:

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

    【讨论】:

    • 您好@Ves,如果我的回复有帮助,您可以标记为答案(或投票给它),这将有助于其他人快速找到该线程并解决类似问题。
    • 嗨@Fred,您的回复很有帮助,我确实投了赞成票。我是新用户,显示此消息:感谢您的反馈!声望低于 15 人的投票将被记录,但不会更改公开显示的帖子得分。
    猜你喜欢
    • 2019-02-22
    • 2013-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多