100 concurrent users trying to upload files (asp.net C # application)

I am trying to implement a file upload function in an asp.net application. The application will be used by, say, about 200 users at the same time to download various files. It will be hosted on IIS 7. I do not want the application server to crash due to multiple requests appearing simultaneously.

I assume that by calling Context.Response.Flush () in a loop, I clear all the file data that I would have read so far, so the memory usage of the application will be uniform. What other optimizations can be made to the current code, or what other approach should be used in such a scenario?

Requests will be for different files, and file sizes can be from 100 KB to 10 MB.

My current code is as follows:

FileStream inStr = null; byte[] buffer = new byte[1024]; String fileName = @"C:\DwnldTest\test.doc"; long byteCount; inStr = File.OpenRead(fileName); Response.AddHeader("content-disposition", "attachment;filename=test.doc"); while ((byteCount = inStr.Read(buffer, 0, buffer.Length)) > 0) { if (Context.Response.IsClientConnected) { Context.Response.ContentType = "application/msword"; //Context.Response.BufferOutput = true; Context.Response.OutputStream.Write(buffer, 0, buffer.Length); Context.Response.Flush(); } } 
+4
source share
4 answers

You can use Response.TransmitFile to save server memory when sending files.

 Response.ContentType = "application/pdf"; Response.AddHeader("content-disposition", "attachment; filename=testdoc.pdf"); Response.TransmitFile(@"e:\inet\www\docs\testdoc.pdf"); Response.End(); 
+1
source

In your code example, you are not closing / not placing inStr. This may affect performance.

Another easy way to do this is to use the built-in method:

Writefile

It should already be optimized and will take care of opening / closing files for you.

0
source

You might want to use the FileSystemWatcher class to check if a file has been modified and only read it in memory when such a change has been detected. The rest of the time, just return the byte array that is already stored in memory. I donโ€™t know if the HttpResponse.WriteFile method is sensitive for such file modification changes or if it always reads the file with the given path, but this also seems to be a good option to use, since it is served by the framework out of the box.

0
source

Since you are sending an existing file to the client, use HttpResponse.TransmitFile (http://msdn.microsoft.com/en-us/library/12s31dhy.aspx).

Looking at the .NET code, it seems like this redirects the writing of the file in IIS instead of reading / writing in the ASP.NET process. HttpResponse.WriteFile(string, false) and HttpResponse.Write(string) seem to do the same.

To verify that the file transfer is passed to IIS, in the HttpResponse.Output property, it should be of type HttpWriter . Now the HttpWriter._buffers array should contain a new HttpFileResponseElement element).

Of course, you should always examine whether caching is appropriate in your scenario and check to see if it is used.

0
source

Source: https://habr.com/ru/post/1441559/


All Articles