skip to Main Content

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);
}

enter image description here

2

Answers


  1. Chosen as BEST ANSWER

    The issue was that I was returning Results.ok() when I should have returned Results.File()

    using (var memoryStream = new MemoryStream())
            {
                await blobClient.DownloadToAsync(memoryStream);
                //memoryStream.Seek(0, SeekOrigin.Begin);
    
                return Results.File(memoryStream.ToArray(), "application/octet-stream", blobName);
            }
    

  2. 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

                return Results.Ok(new FileStreamResult(newMemoryStream, "application/octet-stream") { FileDownloadName = blobName });
    

    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 :

                return new FileStreamResult(newMemoryStream, "application/octet-stream") { FileDownloadName = blobName };
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search