22
votes

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);
}
6
An error occurred while communicating with the remote host. The error code is 0x80070006.David Heffernan
I am going to guess this is a permission problem because of your shared hosting.Security Hound
Please tell me what permission should be given?Salman Roy

6 Answers

14
votes

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" 
13
votes

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


}
3
votes

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.

2
votes

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.

2
votes

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");
0
votes

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!