【发布时间】:2021-10-24 13:29:42
【问题描述】:
我编写了以下返回的函数也返回 SftpFile
public (DataTable, string, SftpFile) ReadFileFromSftp(List<string> headers ,InboundFileConfiguration configuration, ILogger logger)
{
try
{
if (!sftpClient.IsConnected)
sftpClient.Connect();
var fileToBeRead = sftpClient.ListDirectory(configuration.SftpFileUploadPath)
.Where(x => x.Name.Contains(configuration.InterfaceNamePattern))
.OrderByDescending(x => x.LastWriteTimeUtc).FirstOrDefault();
if (fileToBeRead == null)
logger.LogError(ErrorMessages.NoFilePresentOnTheAzureStorageToRead);
fileToBeRead.Delete();
using (var remoteFileStream = sftpClient.OpenRead(fileToBeRead.FullName))
{
using (var memoryStream = new MemoryStream())
{
remoteFileStream.CopyTo(memoryStream);
RemoveArchivedFilesAfterRetentionPeriod(configuration.SftpFileArchivePath, configuration.InterfaceNamePattern, configuration.RetentionPeriod, logger);
memoryStream.Position = 0;
return (ReadMemoryStreamContents(memoryStream, configuration.Delimeter, headers, logger), fileToBeRead.Name, fileToBeRead);
}
}
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
}
finally
{
if (sftpClient.IsConnected)
sftpClient.Disconnect();
}
return (null, null, null);
}
当我在上面的函数中编写以下代码时:
fileToBeRead.Delete();
它可以工作,但是当我在此函数之外删除文件时,它会引发以下异常:
Cannot access a disposed object. Object name: 'Renci.SshNet.Sftp.SftpSession'.
请帮帮我
【问题讨论】:
-
你贴的代码中没有
.Delete()1,fileToBeRead是一个局部变量,不能在函数外使用 -
@PanagiotisKanavos 我没有发布,因为我不想删除函数内的文件。
fileToBeRead不能在我很清楚的功能之外访问。我只想删除函数返回的 SftpFile 以及函数之外的那个。
标签: c# asp.net azure asp.net-core azure-functions