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

system.componentmodel.win32exception: the handle is invalid
vb net the handle is invalid
connection handle is invalid
an error occurred while communicating with the remote host. the error code is 0x80070006

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);
}

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" 

When downloading a file: System.Runtime.InteropServices , COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (​E_HANDLE)) Source Error: An unhandled exception was  Exception Details: System.Runtime.InteropServices.COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)) An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

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


}

How to fix "The handle is invalid. (Exception from HRESULT , COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (​E_HANDLE))RSS. 5 replies. Last post May 17, 2016 05:23 PM by Sergio. An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below. Stack Trace: [COMException (0x80070006): The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))] [HttpException (0x80004005

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.

https://social.msdn.microsoft.com/Forums/vstudio/e, (Exception from HRESULT: 0x80070006 (E_HANDLE)). Description: An unhandled exception occurred during the execution of the current web  (Exception from HRESULT: 0x80070006 (E_HANDLE)) Feb 04, 2015 06:22 AM | Ashim Chatterjee | LINK Make sure correct connection is established before you try to update the fields.Perhaps using a Using keyword would be the best way to do that.

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.

Error in Webservice, System.Runtime.InteropServices.COMException (0x80070006): The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)) When downloading a file: System.Runtime.InteropServices.COMException: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))RSS

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

Could not start executor. The handle is invalid. (Exception from , COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (​E_HANDLE)). Restarting the Microsoft Dynamics GP Service Host service fixes  Re: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)) Nov 09, 2014 11:34 PM | Pengzhen Song - MSFT | LINK

COMException: The handle is invalid after installing patches , The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)) Description: An unhandled exception occurred during the  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. (Exception from HRESULT: 0x80070006 (E_HANDLE

The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)) · Build · orchestrator · sub_setuplicensing · root_advanced. The handle is invalid. (0x80070006) Trying to create a system image of new PC using create a system image in Control Panel. I am running Win 7 Professional 64 bit on

Exception: System.Runtime.InteropServices.COMException. Message: The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)). Exception de HRESULT : 0x80070006 (E_HANDLE)) × Après avoir cliqué sur "Répondre" vous serez invité à vous connecter pour que votre message soit publié. × Attention, ce sujet est très ancien.

Comments
  • An error occurred while communicating with the remote host. The error code is 0x80070006.
  • I am going to guess this is a permission problem because of your shared hosting.
  • Please tell me what permission should be given?
  • webdev.webserver40.exe is the dev webserver built in to Visual Studio isn't it? How is this going to help?
  • 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.
  • I rebooted my web server and that stopped the exception from occurring. I'm still not sure how to prevent this error though.
  • 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.
  • 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!
  • 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 :(