句柄无效。 (HRESULT 异常:0x80070006 (E_HANDLE))

发布于 2024-12-09 13:39:31 字数 1039 浏览 0 评论 0 原文

我有一个位于 IIS 7 共享托管环境中的网站。它运行.NET 3.5。我有一个下载按钮可以从服务器下载文件。

当我在本地将此应用程序部署到 IIS 6 时,它运行良好。在IIS 7共享托管服务器上,出现异常。

句柄无效。 (HRESULT 异常:0x80070006 (E_HANDLE)) 描述:执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
System.Runtime.InteropServices.COMException:句柄无效。 (HRESULT 异常:0x80070006 (E_HANDLE))
COMException (0x80070006): 句柄无效。 (HRESULT 异常:0x80070006 (E_HANDLE))] [HttpException (0x80004005):与远程主机通信时发生错误。错误代码为0x80070006。]

如何解决这个问题?

string strRequest = Convert.ToString(Request.QueryString.Get("file"));
System.IO.FileInfo file = new System.IO.FileInfo(strRequest);
if (file.Exists)
{
    Response.Clear();
    Response.ContentType = ReturnExtension(System.IO.Path.GetExtension(file.Name));
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.TransmitFile(strRequest);
    Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    //DownloadFile(file.FullName, file.Name);
}

I have a website in an IIS 7 shared hosting environment. It's running .NET 3.5. I have a download button to download a file from the server.

When I locally deploy this application to IIS 6, it runs fine. On the IIS 7 shared hosting server, the exception occurs.

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
System.Runtime.InteropServices.COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))
COMException (0x80070006): The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))] [HttpException (0x80004005): An error occurred while communicating with the remote host. The error code is 0x80070006.]

How can this be solved?

string strRequest = Convert.ToString(Request.QueryString.Get("file"));
System.IO.FileInfo file = new System.IO.FileInfo(strRequest);
if (file.Exists)
{
    Response.Clear();
    Response.ContentType = ReturnExtension(System.IO.Path.GetExtension(file.Name));
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + file.Name);
    Response.TransmitFile(strRequest);
    Response.End();
    HttpContext.Current.ApplicationInstance.CompleteRequest();
    //DownloadFile(file.FullName, file.Name);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

浅唱々樱花落 2024-12-16 13:39:31

创建一个 .bat 文件,输入以下命令并运行该文件。它将杀死所有现有的网络服务器进程并应该解决问题。我遇到了同样的问题,它对我来说很有效。非常感谢

taskkill  /fi "imagename eq webdev.webserver40.exe" 

Create a .bat file, put the following command and run the file. It will kill all existing webserver processes and should fix the problem. I had the same problem and it worked out for me. Thanks much

taskkill  /fi "imagename eq webdev.webserver40.exe" 
甜是你 2024-12-16 13:39:31

我从下面的链接找到了修复:

http://forums.asp.net/t/1387967.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

if (file.Name == fileName)

{
     Response.ClearContent();
     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
     Response.AddHeader("Content-Length", file.Length.ToString());
     Response.TransmitFile(file.FullName);
     //Response.End(); Will raise that error. this works well locally but not with IIS
     Response.Flush();//Won't get error with Flush() so use this Instead of End()


}

I found a fix from link below:

http://forums.asp.net/t/1387967.aspx?How+to+create+a+flipcart+like+panel+for+showing+products+in+gridview

if (file.Name == fileName)

{
     Response.ClearContent();
     Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
     Response.AddHeader("Content-Length", file.Length.ToString());
     Response.TransmitFile(file.FullName);
     //Response.End(); Will raise that error. this works well locally but not with IIS
     Response.Flush();//Won't get error with Flush() so use this Instead of End()


}
婴鹅 2024-12-16 13:39:31

我刚刚在我们的环境中解决了这个问题。我们打开了模拟并让应用程序池作为 ApplicationPoolIdentity 运行。

问题是由于应用程序池身份没有对源文件的读取访问权限而引起的,即使模拟的用户确实有权访问该文件。解决这个问题的棘手之处在于,如果用户和应用程序池都没有访问权限,则会出现访问权限错误。

I just resolved this issue in our environment. We have impersonation turned on and have the application pool running as ApplicationPoolIdentity.

Problem was caused by the app pool identity not have having read access to the source file even though the impersonated user did have access to the file. What made this tricky to resolve is that if both user and app pool do not have access you get a access permission error.

橙味迷妹 2024-12-16 13:39:31

编辑:错过了最初有关页面加载的部分。我不太确定从查询字符串传入的内容是什么,但是您尝试过使用 Server.MapPath 吗?因此,

System.IO.FileInfo file = new System.IO.FileInfo(strRequest);

您不必

System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(strRequest));

让我知道这是否有帮助。

EDIT: Missed the part about the page loading fine initially. I'm not exactly sure what's being passed in from your querystring, but have you tried using Server.MapPath? So instead of

System.IO.FileInfo file = new System.IO.FileInfo(strRequest);

you have

System.IO.FileInfo file = new System.IO.FileInfo(Server.MapPath(strRequest));

Let me know if that helps.

天暗了我发光 2024-12-16 13:39:31

就我而言,我尝试写入和读取该文件:

var path = System.IO.Path.GetTempFileName();

我使用了下面的代码并且它有效。我认为 IIS 用户缺少写入或读取临时文件的权限。

var path = Server.MapPath(@"~\App_Data\Stats");
Directory.CreateDirectory(path);
path = Path.Combine(path, String.Format("{0}.csv", Guid.NewGuid()));

using (var streamWriter = new StreamWriter(path))
using (var csvWriter = new CsvHelper.CsvWriter(streamWriter))
{
    csvWriter.Configuration.Delimiter = csvWriter.Configuration.CultureInfo.TextInfo.ListSeparator;

    csvWriter.WriteRecords(rounds);
}

return File(path, "text/csv", "Stats.csv");

In my case I was trying to write and read to this file:

var path = System.IO.Path.GetTempFileName();

I used the code below and it worked. I think that IIS user was missing permission to write to or read from the temporary file.

var path = Server.MapPath(@"~\App_Data\Stats");
Directory.CreateDirectory(path);
path = Path.Combine(path, String.Format("{0}.csv", Guid.NewGuid()));

using (var streamWriter = new StreamWriter(path))
using (var csvWriter = new CsvHelper.CsvWriter(streamWriter))
{
    csvWriter.Configuration.Delimiter = csvWriter.Configuration.CultureInfo.TextInfo.ListSeparator;

    csvWriter.WriteRecords(rounds);
}

return File(path, "text/csv", "Stats.csv");
瞳孔里扚悲伤 2024-12-16 13:39:31

重新启动 IIS 解决了我的问题。

Restarting IIS solved the issue for me.

赠意 2024-12-16 13:39:31

如果您的网页中有任何 Console.WriteLine 或 Console.Clear(),则当不在 Visual Studio 中运行时(即当您将其部署到 IIS 时),您可能会收到该错误。
如果您在 IIS 和 VS 中遇到此问题,它工作正常,然后搜索控制台语句并将其删除。

If you have any Console.WriteLine or Console.Clear() in your web pages you might get that error when not running in Visual Studio , means when you deploy it to IIS .
If you get this problem in IIS and in VS it works fine then search for Console statements and remove them .

〆一缕阳光ご 2024-12-16 13:39:31

就我而言,这种情况仅发生在特定用户登录时。其他所有用户都可以正常工作。

问题是用户的 LoginEmail 中存在额外的空格。

这发生在 MVC 应用程序中,该应用程序使用 Asp.net Identity 和 Impersonation 从托管服务器上的目录下载 excel 文件。

奇怪的东西!

In my case this happened for a specific user login only. Every other user had it working.

The issue was an extra white space in the user's LoginEmail.

This happened in an MVC application, which was using Asp.net Identity, and Impersonation to download an excel file from a directory that lives on the hosting server.

Weird stuff!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文