SCENARIO I'm using C# Webforms with .NET framework 2.0* (the project is 3.5, but the IIS has only 2.0). I have a simple form that gets some user's information and files (PDF, gif, etc) and send them to another application. Pretty simple. The problem is that eventually there's A LOT of people (over 1000) trying to access it at the very same time and the memory usage of the IIS process explodes, causing the application to crash. We're trying to improve the infra (more memory, webfarm, etc), but I'd like to know:
QUESTIONS
Do I really NEED to manually call FileUpload.FileContent.Close() and FileUpload.FileContent.Dispose() like in the finally clause below? Can it help solving the aforementioned problem? ==> ANSWERED: YES
If I do so, can it mess up the cleanup process of the temporary files used by the framework? ==> ANSWERED: NO
What else can I do to avoid this problem? For now, I'll try changing the Webconfig following these articles: http://support.microsoft.com/kb/821268/en-us AND http://www.codeproject.com/Articles/23306/ASP-NET-Performance-and-Scalability-Secrets
CODE Here's the code fragment where I'm using the FileUpload.SaveAs(). It basically gets the generated and already validated info (filename, extension, etc) and uses it to save the fileContent of the fileupload.
private bool GravarArquivo(FileUpload fileUpload, eXML.Anexo anexo)
{
bool sucesso = false;
if (AnexoValido(fileUpload, anexo))
{
string path = Server.MapPath(bllProcesso.GetUrlArquivo(anexo.NomeArquivoFisico));
try
{
//GRAVA ARQUIVO
fileUpload.SaveAs(path);
sucesso = true;
}
catch (Exception ex)
{
string mensagem = String.Format("Erro ao tentar salvar o arquivo '{0}'. Exception: '{1}'", anexo.NomeArquivoFisico, ex.Message);
MostrarAlerta(this, mensagem);
//throw new Exception(mensagem, ex);
}
//TODO: ESTUDAR O USO DESTE FINALLY, PARA VER SE DIMINUI O PROBLEMA DE MEMÓRIA DO SERVIDOR
//finally
//{
// fileUpload.FileContent.Close();
// fileUpload.FileContent.Dispose();
//}
}
return sucesso;
}