TransactionScope works in some places and not in others
Using ASP.NET 3.5, Linq to SQL, SQL Server 2005 on Windows Server 2003. Running VS 2008 on XP SP3 locally.
开发者_如何学PythonWe need to be able to wrap inserts, updates, and deletes in a transaction. When we first tried this by wrapping code blocks with using(var trans = new TransactionScope()) { ...; trans.Complete(); }
, we got an appropriate exception telling us we needed to enable network access for remote transactions. We did so and things began to work the way we expected.
Fast forward to today. There is a little-used part of our app that also received a TransactionScope treatment. Though transactions work properly in all other parts of our codebase, we discovered today that this seldom used piece is throwing the same "Network Access" exception as before:
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. http://img101.imageshack.us/img101/5480/msdtcnetworkaccesserror.jpg
Here's the code that causes the exception:
using (TransactionScope trans = new TransactionScope(TransactionScopeOption.Required, TimeSpan.MaxValue))
{
using (var dc = new ChargeXferDataContext())
{
//create 'Line' object and set initial values
Line line = new Line();
line.Unit_Num = UnitId;
line.SubmittedBy = Viewer.Alias();
line.LineSubmittedOn = DateTime.Now;
//get codes to move from checked rows
//iterate rows in current gridview
foreach (GridViewRow row in gv.Rows)
{
//if checked, insert move order
HtmlInputCheckBox cb = (HtmlInputCheckBox)row.FindControl("RowLevelCheckBox");
if (cb.Checked)
{
//1st: get required values
int id = Convert.ToInt32(((TextBox)row.FindControl("fldCodeId")).Text);
int newId = Convert.ToInt32(((DropDownList)row.FindControl("ddlNewId")).SelectedValue);
char newPOA = Convert.ToChar(((DropDownList)row.FindControl("ddlPOA")).SelectedValue);
//2nd: get current diag code from old patient
//######## Exception happens here...
DiagCode code = dc.DiagCodes.SingleOrDefault(c => c.Id == id);
//########
//3rd: add code to emenline object
addCode(line, code, newId, newPOA);
}
}
dc.SubmitChanges();
trans.Complete();
}
}
If you've got any suggestions, they would be appreciated. Let me know if I can explain something more. Thanks in advance!!
精彩评论