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

8

24

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);
}
Kilar answered 12/10, 2011 at 11:43 Comment(3)
An error occurred while communicating with the remote host. The error code is 0x80070006.Fowl
I am going to guess this is a permission problem because of your shared hosting.Oraliaoralie
Please tell me what permission should be given?Kilar
E
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" 
Epigraph answered 18/12, 2012 at 15:13 Comment(3)
webdev.webserver40.exe is the dev webserver built in to Visual Studio isn't it? How is this going to help?Thracophrygian
Yes finalizing the service helped me too.I did it manually. The real cause is "cross thread operation is not allowed" for me. Visual Studio's development server fails for this state.I think more then one service runs and cross thread issue occurs.Rarebit
I rebooted my web server and that stopped the exception from occurring. I'm still not sure how to prevent this error though.Amblyopia
U
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()


}
Universalism answered 19/11, 2013 at 19:48 Comment(2)
Note that Response.End() is called automatically when page execution is complete, so this answer may apply even if you don't explicitly call Response.End(). Also note that using Response.TransmitFile() without Response.Flush() will work in Visual Studio, but it won't work when deployed on IIS.Palmitin
Indeed the above code works when almost nothing else worked. I was about to go thinker with the IIS users on a production server with many active users that was working perfectly fine (who knows what messing with the users might break) Wish I could give you more UpVotes!Sam
X
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.

Xantha answered 17/8, 2015 at 13:21 Comment(0)
S
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.

Sundsvall answered 12/10, 2011 at 18:24 Comment(1)
no its not a version probelm i think if it is a version probelm whole site will give same error but it is not the case, every thing is working fine except this , i more investigate the problem and i found that this error produced when i use Page.Redirect("download.aspx"); i try another mehtod i open a download.aspx in javascript popup window it works fine.i still cant understand the poblem,this method only work fine in firefox in internet explorer only popup open and download dialog not appear :(Kilar
B
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");
Burl answered 26/3, 2014 at 14:46 Comment(0)
F
1

Restarting IIS solved the issue for me.

Flintshire answered 31/7, 2022 at 17:33 Comment(1)
To be more precise, I just had to recycle my application pool.Preshrunk
A
1

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 .

Aberrant answered 24/10, 2022 at 14:27 Comment(0)
H
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!

Humfrey answered 6/4, 2018 at 14:57 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.