New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
OpenRead(path) disposed Stream immediately #1303
Comments
also if i have this code: var x = stream.CanSeek; return stream; i get exception if i try to read the length. |
If you want a seekable stream you could use |
But what if i want to pass that stream to a different method and use StreamReader on it? I always get that stream is not seekable, i dont want to persist it in memory or download it, only to read and process it. |
I don't think StreamReader does any seeking, so there must be something else going on. You would need to show more code. |
in the next method i do this: StreamReader streamReader = new(request.Stream); //request.Stream is the SftpFileStream reader.Read(); //this throws the " Seek operation failed" and that file handle is closed |
Please post the full code and the full exception with stack trace. It's difficult to deduce anything from small snippets of code |
namespace MyNamespace.Handlers.Interfaces; using Microsoft.Extensions.Logging; public interface ISftpStreamConnector public class SftpStreamConnector : ISftpStreamConnector
} //// namespace MyNamespace.Handlers; using MyNamespace.Domain; public interface IJsonToNdJsonHandler public class JsonToNdJsonHandler : IJsonToNdJsonHandler
} ////errors |
Surely there are more lines in this stack trace? |
Yes, but they are all trace of my outer classes up until this invocation, nothing relevant. Please, give it a try and you'll see. |
I'm sorry, I'm happy to try to help you diagnose the problem but I'm not going to spend my free time diagnosing it for you. I don't see anything obviously wrong with your code. I suggest you start by tracking down what is calling If you find anything interesting, feel free to report back and we can see what to do from there. |
I've run into a very similar issue so I'll start off by using this issue rather than open yet another one. app.MapGet("/{*path}", async (CancellationToken token, string path) =>
{
using var client = new SftpClient(connectionInfo);
await client.ConnectAsync(token);
if (!client.Exists(path))
{
return Results.NotFound("File not found");
}
var attr = client.GetAttributes(path);
if (attr.IsRegularFile)
{
using (var stream = await client.OpenAsync(path, FileMode.Open, FileAccess.Read, token))
{
return Results.Stream(stream, "application/octet-stream", path.Split("/").Last());
}
}
return Results.NotFound("File not found");
}); Trying to actually access the endpoint though, I get the following dumped:
If you need any more info, feel free to @ me, though in this case there really isn't much to this program other than this. |
@Soulusions your stream is being disposed immediately upon return but it is only read from after your request handler delegate is executed. See the code for https://github.com/dotnet/aspnetcore/blob/32b772a470e3c7dc147d3d124c32199bb3e7b6bc/src/Http/Http.Results/src/Results.cs#L403 You probably need to pull the file to a local |
OpenRead or OpenAsync lose their purpose if we cant do anything with the stream outside of the method. Stream purpose is to not inflate the memory but processing on the fly for example. If i pull it into MemoryStream i am overloading my machine RAM with couple of gigabytes potentially. So yeah, not the greatest thing. |
It is the same with |
i have this piece of code:
try
{
_sftpClient = new SftpClient(host, port, username, password);
_sftpClient.Connect();
return _sftpClient.OpenRead(path);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occured while connecting to sftp server.");
throw;
}
i checked the parameters, i open the File with FileZilla, but the SftpFileStream is unreadable after i exit this method. Even when i tried to forward the Stream to a different method it throws me " Seek operation failed"
The text was updated successfully, but these errors were encountered: