Today, one of the websites that I developed started throwing an error message saying:

Server Error in ‘/’ Application.

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.

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

Source Error:

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))] System.Transactions.Transaction.JitSafeGetContextTransaction(ContextData contextData) +0 System.Transactions.Transaction.FastGetTransaction(TransactionScope currentScope, ContextData contextData, Transaction& contextTransaction) +169 System.Transactions.Transaction.get_Current() +121 System.Data.ProviderBase.DbConnectionPool.GetFromTransactedPool(Transaction& transaction) +15 System.Data.ProviderBase.DbConnectionPool.GetConnection(DbConnection owningObject) +147 System.Data.ProviderBase.DbConnectionFactory.GetConnection(DbConnection owningConnection) +108 System.Data.ProviderBase.DbConnectionClosed.OpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory) +126 System.Data.SqlClient.SqlConnection.Open() +125 System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf(Boolean openCondition, DbConnection storeConnectionToOpen, DbConnection originalConnection, String exceptionCode, String attemptedOperation, Boolean& closeStoreConnectionOnFailure) +52

Solution:

This kind of error usually comes when the COM object your InterOp is accessing is throwing an exception.  It is quite hard to give a straight forward answer without knowing that which COM your InterOp is calling.

So, there are 2 possible solutions that will fix this problem:

1st Solution) Under you IIS > Virtual Directory Properties > Execution Permissions might be marked as “Scripts and Executables” the dll is not being loaded so you are  getting “System.Runtime.InteropServices.COMException”

Try changing the Execution Permissions to “Scripts Only” and applying it – It should work now!

I am not sure about the exact different between the two so all I know is:

Scripts only  -> Run only scripts,such as ASP applications  and
Scripts and Executables -> Run both scripts, such as ASP applications, and executables

2nd Solution) On your server where you’re website sits, open the command prompt and run the following command:

C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_regiis.exe -i – It should work now!

I hope it helps someone…