Aysnc方法-该请求在此上下文错误中不可用 [英] Aysnc method - request is not available in this context error

查看:209
本文介绍了Aysnc方法-该请求在此上下文错误中不可用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在我的代码中遇到了一个错误,我正在努力寻找解决方案.

我有一个内部Web应用程序,需要在我们的内部网络上移动一些文件.有问题的文件很多而且相当大,大约1000-3000个文件,每个文件的大小都在30Mb范围内.

由于我不想阻塞UI线程,因此希望异步执行此操作.

我遇到的问题是,在调用begin invoke方法后执行任何文件/文件夹操作时,我收到错误"request is not available in this context".当未由begin invoke调用(即非异步)调用时,该方法效果很好.

因此,例如,我必须检查将文件移入的目录是否确实存在,如果没有,我将创建该目录.获取/创建目录的代码如下:

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;
   }
}




该代码在.net 4中运行,并在IIS7中托管.异步运行此代码会引发我在第一行提到的错误,即




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);.



谁能给我关于我需要做什么的任何想法?

谢谢

Kev



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

Thanks

Kev

推荐答案

有时使用HttpContext.Current.Request代替Page.Request可以帮助解决此问题.
或者,将Request作为参数传递给您的异步方法.
或将您需要的数据从Request复制到某个自定义对象,并将其作为参数传递给异步方法.
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.


这篇关于Aysnc方法-该请求在此上下文错误中不可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆