MSDTC issue with transactions in ADO.NET Entity Fr

2019-06-20 06:18发布

in our current project we are using ADO.NET Entity Framework as data layer for the application. There are some tasks which require to run in a transaction because there's a lot of work to do in the database. I am using a TransactionScope to surround those tasks.

using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.RequiresNew))
{
    // Do something...
    transactionScope.Complete();
}

The problem is as soon as i am using an TransactionScope an exception occurs:

System.Data.EntityException: The underlying provider failed on Open. ---> System.Transactions.TransactionManagerCommunicationException: Communication with the underlying transaction manager has failed. ---> System.Runtime.InteropServices.COMException (0x80004005): Error HRESULT E_FAIL has been returned from a call to a COM component.

It seems that this error has to do something with the MSDTC (Microsoft Distributed Transaction Coordinator). When I change the security configuration of MSDTC another exception is thrown:

System.Data.EntityException: The underlying provider failed on Open. ---> System.Transactions.TransactionManagerCommunicationException: Network access for Distributed Transaction Manager (MSDTC) has been disabled. Please enable DTC for network access in the security configuration for MSDTC using the Component Services Administrative tool.

However MSDTC is configured, the TransactionScope will cause an error. Does somebody know whats going wrong here?

8条回答
对你真心纯属浪费
2楼-- · 2019-06-20 06:38

This is the article we used in resolving our own, similar issue:

Troubleshooting Problems with MSDTC

This is basically an addendum to Nikolay R's answer. He already covered some of the suggestions listed in the article.

Note: The article is part of the Biztalk documentation, but it can apply to anything using MSDTC.

查看更多
孤傲高冷的网名
3楼-- · 2019-06-20 06:40

This means it is suppressing any Transaction that might be in effect currently when you enter your code block, so any updates your code makes will not rollback if the outer "ambient" transaction decides to rollback.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-06-20 06:40

If Distributed Transaction Coordinator service is not started, Entity framework can not connect to database. Open and start the Distributed Transaction Coordinator

Services -> Distributed Transaction Coordinator

查看更多
SAY GOODBYE
5楼-- · 2019-06-20 06:46

Hmm, it seems to work when i change the TransactionScopeOption to "Suppress":

using (TransactionScope transactionScope = new TransactionScope(TransactionScopeOption.Suppress))
{
    ...
}

Does everyone know why?

查看更多
倾城 Initia
6楼-- · 2019-06-20 06:49

"If you are using Entity Framework with Transactions, Entity Framework automatically opens and closes a connection with each database call. So when using transactions, you are attempting to spread a transaction out over multiple connections. This elevates to MSDTC."

You can pass in your database context to callee class or function in your transaction.

Maybe this is your answer: MSSQL Error 'The underlying provider failed on Open'

查看更多
Viruses.
7楼-- · 2019-06-20 06:50

By default MSDTC has network access disabled. To get it working you should go to

Control Panel-> Administrative Tools->Component Services->Component Serivces->Computes->My computer->Right click->Properties->MSDTC->Security Configuration

and check following checkboxes Network DTC Access, Allow Inbound, Allow Outbound. Authentification should be chosen according to you environment. You might also want to take a look at DTCPing tool to debug distributed transactions. To give you a shortcut - you may need to modify you registry:

HKLM\Software\Policies\Microsoft\Windows NT\RPCRestrictRemoteClients=0 HKLM\Software\Policies\Microsoft\Windows NT\RPCEnableAuthEpResolution=1

to get everything up and running.

查看更多
登录 后发表回答