Reputation: 575
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);
}
Upvotes: 24
Views: 76730
Reputation: 890
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 .
Upvotes: 1
Reputation: 4209
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!
Upvotes: 0
Reputation: 4935
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");
Upvotes: 2
Reputation: 31
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.
Upvotes: 3
Reputation: 131
I found a fix from link below:
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()
}
Upvotes: 13
Reputation: 149
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"
Upvotes: 14
Reputation: 11773
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.
Upvotes: 2