TransactionScope适用于某些地方,而不适用于其他地方

时间:2010-03-11 15:51:02

标签: c# sql-server-2005 linq-to-sql transactionscope msdtc

在Windows Server 2003上使用ASP.NET 3.5,Linq to SQL,SQL Server 2005.在XP SP3上本地运行VS 2008。

我们需要能够在事务中包装插入,更新和删除。当我们通过用using(var trans = new TransactionScope()) { ...; trans.Complete(); }包装代码块来首次尝试此操作时,我们得到了一个适当的异常,告诉我们需要为远程事务启用网络访问。 We did so事情开始以我们预期的方式发挥作用。

快进到今天。我们的应用程序中有一个很少使用的部分也接受了TransactionScope处理。虽然交易在我们的代码库的所有其他部分都能正常运行,但我们今天发现这个很少使用的部分会抛出与之前相同的“网络访问”例外:

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

以下是导致异常的代码:

        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();
            }
        }                       

如果您有任何建议,我们将不胜感激。如果我能解释一下,请告诉我。在此先感谢!!

1 个答案:

答案 0 :(得分:1)

我从来没有找到解决方案。继续生活。