Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
C# creating stream object by using statement

I have one method to download s3 file

Method

Public ActionResult download(string filename, string credentials)
{
  ....
  Using(stream res = response from s3)
  { 
       return file(res, type, filename);
   }
}

But exception is throwing on executing the above method.

Exception message - The request was aborted: The connection was closed unexpectedly

I have to release the stream 'res' object after download.

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

In the case of the File method for returning an ActionResult, you are transferring responsibility for closing the stream to the action-result, so indeed you do not want to call Dispose or use using. This is so that the ActionResult does not need to buffer the data. So: just take out the using:

public ActionResult Download(string filename, string credentials)
{
  ....
  var res = /* response from s3 */
  return File(res, type, filename);
}

If you have non-trivial code, you can make it more complex:

public ActionResult Download(string filename, string credentials)
{
  ....
  Stream disposeMe = null;
  try {
      // ...
      disposeMe = /* response from s3 */
      // ...
      var result = File(disposeMe, type, filename);    
      disposeMe = null; // successfully created File result which
                        // now owns the stream, so *leave stream open*
      return result;
  } finally {
      disposeMe?.Dispose(); // if not handed over, dispose
  }
}
about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda