1

I have web page index.aspx and corresponding server side code index.aspx.cs. This C# code has a method which cannot executed in parallel if multiple clients connect to my website. How can I restrict this?

Here is what the method does. It creates a folder, zip it and make it available for the user to download. My requirement is that when one user is executing this method, some other user should not do this because it will create the same folder again which leads to corruption of data.

I tried using Session objects. But I came to know that session objects are stored per client basis.

Can anyone suggest me some solution?

Ravi Chandra
  • 677
  • 12
  • 24
  • 3
    You need to rethink your problem. Do all users want same zipped file? If not then, you should create (random folders maybe) folders and keep a reference of folder name in your calling code. Instead of session, use Cache to store limiting variable if you don't want to change how zipped files are created. – Yahya May 09 '13 at 10:28
  • All users don't want same zipped file. Based on the user selections on the UI, the package differs. – Ravi Chandra May 09 '13 at 10:38
  • @RaviChandra why not defer creation of the zip until they try to download it, and just do it in-memory? no contention then, and no wasted effort – Marc Gravell May 09 '13 at 10:39
  • WHy not give each file a unique name? Simplest is GUID. – Captain Kenpachi May 09 '13 at 10:47

3 Answers3

1

The Application context or a static class is application wide. So you can store a flag which indicates that the process is already started. After the procees ended, you can delete the flag.

http://msdn.microsoft.com/en-us/library/94xkskdf(v=vs.100).aspx

And always use Application.Lock when you write to the application state and lock(mutex) when you use a static class.

In your case a static class would be a better solution, because it seems that the application context exist only for compatible purposes to classic asp: Using static variables instead of Application state in ASP.NET

static object mutex= new object();

lock(mutex)
{
   //Do the work
}
Community
  • 1
  • 1
Kai
  • 1,953
  • 2
  • 13
  • 18
  • 1
    The application context is nothing more than static variables. http://stackoverflow.com/questions/10960695/using-static-variables-instead-of-application-state-in-asp-net/10964038#10964038 – Aristos May 09 '13 at 10:32
  • Kai, Thanks for your answer, this will help solving my problem – Ravi Chandra May 09 '13 at 10:47
1

My immediate advice would be: create a random folder name per request, which would allow you to run them concurrently. However, if that isn't an option then you will need to synchronize using something like lock or Mutex. However, this would only work well if you are returning the result from the current request, rather than zipping it in one request, and letting them download it the next.

Frankly, though, I think that you should do the zip in the request for the zip. Indeed, unless the file will be huge you don't even need to touch the file-system - you can create a zip in-memory using MemoryStream and any of the zip encoders (System.IO.Packaging.ZipPackage for example) - then just hand the client the data from the MemoryStream.

If you are using MVC, this is just return File(contents, contentType). With vanilla ASP.NET you need a few more steps.

Community
  • 1
  • 1
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

If you use the classic asp.net session you do not need to do anything because session all ready lock the run of the pages from multiple users.

If you not, then you can follow what Marc suggest, use Mutex.

About the session lock:
Web app blocked while processing another web app on sharing same session
jQuery Ajax calls to web service seem to be synchronous
ASP.NET Server does not process pages asynchronously
Replacing ASP.Net's session entirely

Community
  • 1
  • 1
Aristos
  • 66,005
  • 16
  • 114
  • 150
  • 1
    session does *not* block multiple runs of the page at the same time (certainly not for different users, but usually not for the same user either); asp.net is inherently happy to run multiple copies of the same page – Marc Gravell May 09 '13 at 10:37
  • @MarcGravell Maybe I did not write it correct ? Is lock them and is not going to run the code in parallel, if the code is from inside call of the page. – Aristos May 09 '13 at 10:39