是否可以为 abcd.exe 创建 Azure Function 并在 Azure Cloud Functions 中运行它?
是的,我们可以上传.exe文件并在Azure Function应用程序中运行它。我创建了一个 TimerTrigger Function 应用程序,并运行一个 .exe,将记录插入到该 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 app文件夹
![enter image description here](https://i.stack.imgur.com/s36CX.png)
我的.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();
Query the database, I can find the test record is inserted from my .exe file.
![enter image description here](https://i.stack.imgur.com/nrs55.png)