Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
2.33/5 (3 votes)
Hi All,

I'm experiencing an error in my code that i'm struggling to find a solution to.

I have an internal web application that needs to move some files around on our internal network. The files in question are numerous and fairly large, approx 1000 - 3000 files that are all in the range of 30Mb each.

As I do not want to block the UI thread I wish to perform this operation asynchronously.

The problem I am having is that I receive the error "request is not available in this context" when performing any file/folder operations after calling the begin invoke method. The method works perfectly well when not being called by begin invoke, i.e. not asynchronously.

So, for example, I have to check that the directory I am moving the files into is actually there and if not I will create it. The code to get / create the directory is as follows:

DirectoryInfo importDirectory = new DirectoryInfo(required folder path);

if (!backupLocation.Exists)
{
   try
   {
        ....try to create directory
   }
   catch (Exception Ex)
   {
        return Ex error details;
   }
}



The code is running in .net 4 and hosted in IIS7. Running this code asynchronously throws the error I mention on the first line, i.e.
DirectoryInfo importDirectory = new DirectoryInfo(required folder path);.


Can anyone give me any ideas of what I need to do?

Thanks

Kev
Posted
Updated 24-Feb-11 5:31am
v2
Comments
sjelen 24-Feb-11 11:57am    
The exception suggests you are trying to use Request object, but it doesn't show in your code sample.
Provide some more details.

1 solution

Sometimes using HttpContext.Current.Request instead of Page.Request can help solve this issue.
Or, pass Request to your async method as parameter.
Or copy the data you need from Request to some custom object and pass that as param to your async method.
 
Share this answer
 
Comments
Kevinio 25-Feb-11 6:22am    
Hi thanks for the response. You were totally correct. I had mistakenly, and repeatedly missed (i hate rush coding), that the line of code building the folder / file paths was getting data using request.querystring. I moved the code so that the querystring parameter is passed in to the async method and now all is well.

Thanks again
Mahmoud_Farahat 22-Sep-14 10:11am    
@sjelen Thanks very much.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900