使用WinSCP .NET找不到异常的方法(EventWaitHandle..ctor)

问题描述 投票:2回答:1

我正在尝试使用PowerShell连接到SFTP服务器,并使用WinSCP .NET程序集。

代码无法打开会话($session.Open($sessionOptions))。

在我发现的日志中,

Exception: System.MissingMethodException: Method not found: 'Void System.Threading.EventWaitHandle..ctor(Boolean, System.Threading.EventResetMode, System.String, Boolean ByRef, System.Security.AccessControl.EventWaitHandleSecurity)'. at WinSCP.ExeSessionProcess.TryCreateEvent(String name, EventWaitHandle& ev) at WinSCP.ExeSessionProcess.InitializeConsole() at WinSCP.ExeSessionProcess.Start() at WinSCP.Session.Open(SessionOptions sessionOptions) [2019-03-14 10:59:02.835Z] [0013] Session.Cleanup entering [2019-03-14 10:59:02.835Z] [0013] Terminating process

基本上,它从$session.Open($sessionOptions)跳到$session.Dispose()并跳过代码的其他部分。

我试图在WinSCP论坛上找到解决方案,但它没有帮助: https://winscp.net/forum/viewtopic.php?t=26140

try {
    Add-Type -Path "M:\AMA\ztemp_powershell\WinSCPnet.dll" 

    $sessionOptions = New-Object WinSCP.SessionOptions -Property @{
        Protocol = [WinSCP.Protocol]::Sftp
        HostName = "abc.xyz.ca"
        UserName = "abc_abc"
        Password = "*********"
        SshHostKeyFingerprint = "ssh-rsa 2048 **********************"
        PortNumber = 22
    }
    $session = New-Object WinSCP.Session   
    $session.ExecutablePath = "H:\FromLocal\Powershell\WinSCP.exe" 
    $filelist = Get-ChildItem M:\AMA\ztemp_powershell\sample_files

    try {
        # $session.DebugLogPath = "M:\AMA\ztemp_powershell\sftp1.log"
        $session.Open($sessionOptions)

        $transferOptions = New-Object WinSCP.TransferOptions
        $transferOptions.TransferMode = [WinSCP.TransferMode]::Binary

        foreach ($file in $filelist) {
            $transferResult =  $session.PutFiles("M:\AMA\ztemp_powershell\sample_files\$file" , "/outbox/", $false, $transferOptions)

            foreach ($transfer in $transferResult.Transfers) {
                Write-Host "Upload of $($transfer.FileName) succeeded"
            }
        }
    }
    finally {
        $session.Dispose()
    }
    exit 0
}
catch {
    Write-Host "Error: $_"
    exit 1
}
.net powershell sftp winscp winscp-net
1个回答
1
投票

在论坛中,您写道您已使用WinSCP .NET程序集5.13.8。

  1. 您已将查询发布到有关.NET Core 2.0和的线程
  2. 缺少的方法是使用EventWaitHandleSecurity,它不是.NET Standard的一部分

根据以上事实,我假设您的项目以.NET Standard为目标,WinSCP 5.13不支持。我已经在WinSCP论坛的回复中建议你,你应该升级到支持.NET Standard 2.0的WinSCP 5.14 RC。

https://winscp.net/eng/docs/library

© www.soinside.com 2019 - 2024. All rights reserved.