I currently have an endpoint in my .net 7 API that when hit, should download a specific file from my blob storage by using the file name. When I test out the endpoint I get a 500 error stating
"Time outs are not supported in this stream". I have attached the error below for more information.
Endpoint.cs
public IEndpointRouteBuilder MapEndpoints(IEndpointRouteBuilder endpoints)
{
endpoints.MapGet("getBlob/{blobname}", async (string blobName, IApplicationFormService applicationFormService) => await applicationFormService.GetBlob(blobName))
.Produces<FileStreamResult>();
return endpoints;
}
Service.cs
public async Task<IResult> GetBlob(string blobName)
{
BlobClient blobClient = _blobServiceClient
.GetBlobContainerClient("root")
.GetBlobClient(blobName);
try
{
using (var memoryStream = new MemoryStream())
{
await blobClient.DownloadToAsync(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
var newMemoryStream = new MemoryStream(memoryStream.ToArray());
return Results.Ok(new FileStreamResult(newMemoryStream, "application/octet-stream") { FileDownloadName = blobName });
}
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
return Results.Ok(blobName);
}
2
Answers
The issue was that I was returning Results.ok() when I should have returned Results.File()
Based on the stacktrace of the error, it doesn’t seems to come from the downloading of the blob, but from your endpoint when it tries to serialize the response to json.
I think the mistake is on the line
FileStreamResult is already an IActionResult that represents an HTTP 200 response so there’s no need to call the Ok() method.
So the line should just be :