c# - 在 Azure Function 中运行 .exe 可执行文件

标签 c# azure azure-functions

我有可执行文件 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。

谢谢

最佳答案

Is it possible to create Azure Function for abcd.exe and run it in 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 文件上传到 Function 应用文件夹

enter image description here

.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文件中插入的。 enter image description here

关于c# - 在 Azure Function 中运行 .exe 可执行文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45348498/

相关文章:

Azure 应用服务 - 无法通过指纹找到证书

c# - Azure Function 中的复杂对象应用设置

azure - 在azure函数中摄取SSE(服务器发送的事件)?

c# - 在 Azure Function 中处理从查询到 Azure IOT Hub 的 JSON 响应

c# - Response.Write 后 TextBoxWatermarkExtender 丢失文本

c# - 在非不可变类型中覆盖 == 运算符

c# - 检查IP地址和端口是否响应

c# - 为什么 LINQ Where 搜索查询在 List<> 上比 ConcurrentBag<> 更快

azure - Azure SQL 数据库中的最少日志记录

c# - azure 函数queueTrigger 错误 - Microsoft Azure WebJobs SDK '[Hidden Credential]' 连接字符串丢失或为空