WinSCP .NET 程序集在 C# Azure 函数中的 Session.Open 中出现 "The winscp.exe executable was not found" 失败

WinSCP .NET assembly fails with "The winscp.exe executable was not found" in Session.Open in C# Azure function

首先我生成了一个简单的控制台应用程序来查看由 WinSCP 生成的是否工作正常 - 确实如此

现在我尝试使用 WinSCP 编写一个 Azure 函数 - 以相同的方式 - 相同的参数 - 我在 Session.Open (SessionLocalException) 上遇到异常:

[Error] Executed 'WriteFile' (Failed, Id=68e2ef30-c2fb-4f5e-8444-93634a2728bb, Duration=256ms) The winscp.exe executable was not found at location of the assembly WinSCPnet (D:\home\site\wwwroot\bin), nor the entry assembly Microsoft.Azure.WebJobs.Script.WebHost (D:\Program Files (x86)\SiteExtensions\Functions.2.0bit), nor in an installation path. You may use Session.ExecutablePath property to explicitly set path to winscp.exe.

winscp.exe 文件不在 D:\home\site\wwwroot\bin 中,而是在 D:\home\site\wwwroot 中。由于我是通过 Visual studio 发布的,所以它是只读的,所以我不能只将文件复制到正确的位置。

如何更新我的 zip 部署以使文件位于正确的位置?

我的代码:

public static async Task<IActionResult> WriteFile(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = null)] HttpRequest req)
{
    SessionOptions sessionOptions = new SessionOptions
    {
        Protocol = Protocol.Sftp,
        HostName = "xxx",
        UserName = "xxx",
        Password = "xxx",
        SshHostKeyFingerprint = "xxx",
    };
    using (Session session = new Session())
    {
        try
        {
            session.Open(sessionOptions);
        }
        catch (SessionLocalException)
        {
            content = JsonConvert.SerializeObject(new { success = false, exception = "SessionLocalException", condition = "Error communicating with winscp.com. See the exception documentation for details." });
            return (ActionResult)new OkObjectResult($"{content}");
        }

有关详细信息的异常文档没有多大帮助 有人可以帮我吗?


我第一次直接在 Azure 门户中编写一些函数来处理 SFTP 相关活动 - 我是 Azure Functions 的新手,但我找到了处理它的方法

现在我尝试在 Visual Studio 2019 中编写这些函数以发布到 Azure 函数

问题是在 C# 代码中,当我尝试打开会话时它失败了。

这个确切的问题记录在 WinSCP guide to transfers in Microsoft Azure Function:

The only problem you will face is, that for some reason, when publishing the project to an Azure storage, winscp.exe dependency is stored to the root folder of the function, instead of the bin subfolder along with other binaries (particularly the WinSCPnet.dll).

To overcome that, you need to use Session.ExecutablePath to point to the actual location of winscp.exe. You can use ExecutionContext.FunctionAppDirectory to refer to the root folder of the function. To get an instance of the ExecutionContext, add new parameter to the signature of your entry method (usually Run).

[FunctionName("Function1")]
public static void Run(
    [TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
    // ...
    session.ExecutablePath = Path.Combine(context.FunctionAppDirectory, "winscp.exe");
}

(有关完整代码,请参阅指南)


毕竟,异常消息暗示相同:

You may use Session.ExecutablePath property to explicitly set path to winscp.exe.