Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics GP (Archived)

Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

Posted on by Microsoft Employee

Hi,

We are having problems with GP Webservice call that looks like this:

invItem = wsDynamicsGP.GetInventoriedItemByKey(orderedItem, context);

It works for a while, then we start getting the following eeror in the GP Exception Log:

System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE)).


Restarting the Microsoft Dynamics GP Service Host service fixes this problem. It seems to be ocuring once or twice a day. Does anyone know how to resolve this?  We are running GP2013R2.

Thank you, Bob

*This post is locked for comments

  • DavidMO Profile Picture
    DavidMO 1,030 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    Sure. These are my first pair of PowerShell scripts, a batch file, plus a Windows Scheduler task that runs every 5 minutes. It does it's job and has kept us running.

    ReadDynamicsEventLog2.ps1

    # David Morinello 04/19/2016

    $Event = get-eventlog -logname Dynamics -EntryType Error -After (Get-Date).AddMinutes(-5) | where-object  {$_.Message -like "*Exception from HRESULT*"}

    If ($Event.length -gt 0) {

       #Write-Output "Event Exists"

       cmd.exe /c '\Automations\RestartGPWebServices.bat'

       }

       #Else {"No Event"}

    Exit

    --------------------------------------------

    RestartGPWebServices.bat

    Net Stop DynGPWebService

    Timeout /t 10 /nobreak

    REM Alternate killer of the GP Web Service (DPM)

    Echo Off

    FOR /F "tokens=3" %%A IN ('sc queryex DynGPWebService ^| findstr PID') DO (SET pid=%%A)

    IF "!pid!" NEQ "0" (

     taskkill /F /PID %pid%

    )

    Timeout /t 10 /nobreak

    Net Start DynGPWebService

    Timeout /t 10 /nobreak

    Powershell.exe -executionpolicy remotesigned -File C:\Automations\RestartGPWebService_Email.ps1

    ----------------------------------------

    RestartGPWebService_Email.ps1

    # David Morinello 04/19/2016

    send-mailmessage -to "David.Morinello <David.Morinello@eMAILSERVER.COM.com>" -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"

    send-mailmessage -to "David.Morinello <DMorinelloii@yahoo.com>"             -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"

    #send-mailmessage -to "John.Steyer <John.Steyer@eMAILSERVER.COM.com>"         -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"

    send-mailmessage -to "CorpAppsAlerts@eMAILSERVER.COM.opsgenie.net"           -from "noreply@eMAILSERVER.COM.com" -subject "GP Web Service on ASC-STG-GPWB03 has restarted" -Body "The GP Web Service on ASC-STG-GPWB03 has restarted due to a detected Error Event" -SmtpServer "mail.eMAILSERVER.COM.com"

    ----------------------

    Windows Task Scheduler Task

    Calls a PowerShell script ReadDynamicsEventLog2.ps1 to check the Dynamics Event log for a "Handle is invalid" error. PS Script calls RestartGPWebServices.bat batch if true. Emails are handled by RestartGPWebService_Email.ps1

  • Rob Klaproth Profile Picture
    Rob Klaproth 1,730 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    Hi David,

    Would you mind sharing the script that you created to restart?

  • Mitch.M Profile Picture
    Mitch.M 530 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    It's just a little frustrating. For me the biggest problem is you can't even be proactive with the issue; you can only catch it after the fact. And even if we do catch it and restart immediately you have to deal with those original transactions that were sent and failed.

    For one client we know they will receive transactions starting at 11 PM. So we proactively restart the services right before. But it still breaks randomly.

  • DavidMO Profile Picture
    DavidMO 1,030 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    A good question. Microsoft has taken this as a suggestion ( I did get some votes, but not as many as I would have liked), but since the issue has been known and reported for years, I would not hold any deep breaths.

    Once the error starts, the GP Web Service is neither completely up or stopped. It takes a Kill commend to get rid of it. I ended up writing some PowerShell script to detect this specific error and running the kill command, then restart the Service. It works well enough, so we are not down for more than 5 minutes or so when it occurs.

  • Mitch.M Profile Picture
    Mitch.M 530 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    How is this not fixed yet?

  • Suggested answer
    DavidMO Profile Picture
    DavidMO 1,030 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    Here is some feedback I received from Microsoft via our GP partner

    Summary of issue:

    • The connection that is established between the Dynamics Security Service for Web Services security store is getting “dropped” sporadically which causes any subsequent calls to the security store fail. This exception is getting logged in the Web Service exception console.

     

    • Somewhere within the infrastructure of that deployment, there is an issue either between the Servers and/or Virtual Machine and it’s Host or the Host and the SQL Server. The way the code is written today, we are expecting that connection to be there and constant. We DO NOT have any additional code that can detect if the connection is gone OR handle the exception when we make a call and the connection is no longer there. The best scenario would be for the code to handle the exception and then spin up a new connection to the SQL Server. This way, even though there are problems with the infrastructure, the software handles the problem and continues to work. (Enhancement request, anyone?)

     

    In dealing with these types of issues, you typically need to work with the windows platforms team. What they will do is gather all of the information from the physical host and servers/virtual machines/etc. involved and verify that the BIOS and drivers for both systems are at their latest revisions. They would also have you make sure that the latest public release of service packs for the SQL and SQL Servers have also been applied.

     

    The only other thing I can offer is something that we looked at previously with Web Services but never really verified the success of it. If you get a corresponding error or log in Event Viewer when this issue happens, you could potentially use the ‘attach task to this event’ functionality where each time the exception is thrown, it would run a batch file, for example, to stop and restart the Dynamics GP Service Host service, which is the resolution you’re currently using. We have gotten this to work in a controlled environment, but haven’t tested extensively with customer or partner issues. The worst case scenario with this, would be that the Dynamics GP Service Host service is restarted all the time, depending on what is listed in the Event Viewer log.

  • Midmichael Profile Picture
    Midmichael 59 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    I have seen this with a couple of our clients - but we have not determined the exact cause yet. I have the service restarting periodically which helps, but does not completely eliminate the issue. I have seen it with GP2010 and GP2013R2. I'm hoping that keeping this topic current will eventually effect a solution.

  • DavidMO Profile Picture
    DavidMO 1,030 on at
    RE: Error in Webservice - System.Runtime.InteropServices.COMException : The handle is invalid. (Exception from HRESULT: 0x80070006 (E_HANDLE))

    We are seeing this every few weeks I production. I saw another thread with the 2010 version of Web Services, saying it was a known issue with Microsoft. Anyone have a solution?

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans