我在 .NET MAUI 中有一个应用程序,它通过 sftp 连接到服务器并下载文件。调试时,一切都按预期工作。但是,当我发布 Android 应用程序(目标 V.13,最低 V.9)时,我在连接到服务器时会得到以下堆栈跟踪:
Could not create HashAlgorithm from `SHA512`. Arg_ParamName_Name, hashAlgorithmName at Renci.SshNet.Security.Cryptography.RsaDigitalSignature..ctor(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName) at Renci.SshNet.PrivateKeyFile.Open(Stream privateKey, String passPhrase) at Renci.SshNet.PrivateKeyFile..ctor(Stream privateKey, String passPhrase)
这是我用来连接服务器的代码:
public void Connect()
{
MemoryStream keyStream = new(Encoding.ASCII.GetBytes(privateKey));
keyFile = new PrivateKeyFile(keyStream, privateKeyPassword);
PrivateKeyConnectionInfo keyConnectionInfo = new(host, port, username, keyFile);
client = new SftpClient(keyConnectionInfo);
client.Connect();
}
正如 Jason 所建议的,这是一个链接器问题。为了解决这个问题,我在发布时禁用了修剪和 aot。
项目属性 -> Android -> 选项 -> 启用 AOT 编译并启用修剪