Azure Function中使用外部exe文件

問題背景

在項目中需要用到pandoc.exe的工具,但是nuget到的包也不是dll,是一個exe(看了stackoverflow大概兩者區別就是exe是許多dll的集成)。在本地調用時,可以直接指向某個絕對路徑找到可執行文件,但是到了Azure portal上怎麼定位呢?

解決

solution1

爲了在function中使用這個exe,通過nuget貌似只能添加dll的引用,所有的包都會在build的時候加載到一個PackageFolder裏面:

var settings = Settings.LoadDefaultSettings(null);
Console.WriteLine(SettingsUtility.GetGlobalPackagesFolder(settings));

通過這個語句可以查到。但是嘗試之後,並不能自動導exe.失敗

solution 2

其實publish的就是你的項目下面的bin/release文件夾(或者是debug,默認是release可以修改)。

以我的目錄爲例:FunctionAppTest\bin\Release\netcoreapp2.1下面有各個function的子文件夾,可以在function中通過下面的語句定位到:

executionContext.FunctionDirectory

所以想把exe放到項目根目錄再通過相對路徑直到根目錄。但是發現這樣也不會加載exe文件!!!失敗!

Solution 3

https://stackoverflow.com/questions/45348498/run-exe-executable-file-in-azure-function

終於stackoverflow真厲害!!!


Is it possible to create Azure Function for abcd.exe and run it in Azure Cloud Functions?

Yes, we can upload .exe file and run it in Azure Function app. I create a TimerTrigger Function app, and run a .exe that insert record into database in this Function app, which works fine on my side.

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

Upload .exe file to Function app folder

enter image description here


但是我在實踐這個的過程中遇到一個問題:我的文件夾在portal上是read-only的!!後來我刪了整個工程重新publish,記得不要勾publish as  a package! 然後到portal Azure function界面裏在functionappsetting中將文件宣稱read and write模式!!!!終於可以upload file啦!!!!!!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章