The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

22

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);
}
c#
asp.net
iis-7
shared-hosting
httpexception
asked on Stack Overflow Oct 12, 2011 by Salman Roy • edited Dec 23, 2013 by p.campbell

6 Answers

14

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" 
answered on Stack Overflow Dec 18, 2012 by asim • edited Dec 18, 2012 by Lin-Art
13

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()


}
answered on Stack Overflow Nov 19, 2013 by Ricky • edited Nov 19, 2013 by (unknown user)
3

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.

answered on Stack Overflow Aug 17, 2015 by Andrew Rose
2

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.

answered on Stack Overflow Oct 12, 2011 by joelmdev • edited Oct 13, 2011 by joelmdev
2

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");
answered on Stack Overflow Mar 26, 2014 by Akira Yamamoto • edited Apr 7, 2017 by Akira Yamamoto
0

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!

answered on Stack Overflow Apr 6, 2018 by t_plusplus

User contributions licensed under CC BY-SA 3.0