Reputation: 20078
error:
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.
using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope())
11 {
12 try
13 {
14 foreach (DataRow row in this.dt1.Rows)
15 {
16 int titleId = int.Parse(row["titleId"].ToString());
17 string fname = row["fname"].ToString();
18 string lname = row["lname"].ToString();
19
20 if (cmd.Parameters.Count > 0)
21 cmd.Parameters.Clear();
22
23 cmd.Parameters.AddWithValue("@titleId", titleId);
24 cmd.Parameters.AddWithValue("@fname", fname);
25 cmd.Parameters.AddWithValue("@lname", lname);
26 cmd.ExecuteNonQuery();
27
28 }
29 con.Close();
30 ts.Complete();
31 }
32 catch (Exception ex)
33 {
34
35 }
36 }
37 }
Upvotes: 31
Views: 70728
Reputation: 1709
So a few / couple things since this is a pretty important question and answer.
In my case, I had successfully used TransactionScope for another app separate from the problem app so I knew that something was different between these 2.
The first app did not trigger escalation orthis error, so what was different ??
Well it turns out that , we actually did trigger a true distributed transaction within the problem application because the stored procedures being invoked within the body / scope of the parent TransactionScope were actually on 2 different SQL Server instances (not 2 different databases on the same server … this is actually common and ok, no, I mean literally MSSqlInstance1 and MsSqlInstance2).
But why did this not manifest in our test environment?
This is because we manually copied the tables on Instance2 over to Instance1 because we didn’t want to write to Instance2 in our test environment as it would have tainted production data.
Then, once I deployed to Production, instead of both sprocs being invoked on Instance1 which masked the OG error , we were now pointing to our primary instance in Production (Instance1), and our reporting database / replication based instance (Instance2).
At this point we made .NET angry because now I’m dealing with tables housed on 2 totally different servers which is the actual definition of a distributed transaction.
So I told my manager would either get rid of the write to Instance2 since we already had that data on hand in Instance1, or simply take the write to Instance2 outside the TransactionScope and synchronously wait for the parent transaction running on Instance2 to finish (worst case we’d be missing the data in Instance2 if the second write failed but it was non critical and could easily be restored / derived via Instance1).
I chose the second option which made TransactionScope happy (just move the second write outside the closing curly brace of my using statement which made it run once TransactionScope was completed and finalized).
Hope this makes sense.
Upvotes: 0
Reputation: 3418
Adding Enlist=false;
at the end of the connection string helped me!
Upvotes: 3
Reputation: 1901
If someone is looking at this, and using linq, then just wrap the whole transaction into a JoinScope like this:
using js = new JoinScope(false) {
using (System.Transactions.TransactionScope ts = new Sytem.Transactions.TransactionScope()) {
...
}
}
Upvotes: -1
Reputation: 19
InnerException = {"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."}
I followed the below steps to resolve above issue on my local system
Enable Network DTC Access :
Run dcomcnfg in start to open the Component Services Administrative tool
Click on Local DTC and open the property window
Click on security Tab and make below security settings to enable Network DTC Access
Enabled Firewall Rules related to Distributed Transaction Coordinator (TCP-IN/TPC-
For more details @https://rajeevdotnet.blogspot.com/2018/10/wcf-exception-network-access-for.html
Upvotes: -1
Reputation: 1795
Close the connection after the transaction scope Complete method.
ts.Complete();
con.Close();
the completed code is
using (System.Transactions.TransactionScope ts = new Sytem.Transactions.TransactionScope())
{
try
{
foreach (DataRow row in this.dt1.Rows)
{
int titleId = int.Parse(row["titleId"].ToString());
string fname = row["fname"].ToString();
string lname = row["lname"].ToString();
if (cmd.Parameters.Count > 0)
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@titleId", titleId);
cmd.Parameters.AddWithValue("@fname", fname);
cmd.Parameters.AddWithValue("@lname", lname);
cmd.ExecuteNonQuery();
}
ts.Complete();
con.Close();
}
catch (Exception ex)
{
}
}
Upvotes: 3
Reputation: 2135
To enable Network access to MSDTC on Windows Vista/7/8 Server 2008R2/2012, follow the steps below:
Click Start, click Run, type dcomcnfg and then click OK to open Component Services.
In the console tree, click to expand Component Services, click to expand Computers, click to expand My Computer, click to expand Distributed Transaction Coordinator and then click Local DTC.
Right click Local DTC and click Properties to display the Local DTC Properties dialog box.
Click the Security tab.
Check mark "Network DTC Access" checkbox.
Finally check mark "Allow Inbound" and "Allow Outbound" checkboxes.
Click Apply, OK.
A message will pop up about restarting the service.
Click OK and That's all.
Upvotes: 73