TransactionScope 在某些地方有效,但在其他地方无效

发布于 2024-08-24 09:00:02 字数 2258 浏览 12 评论 0原文

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

我们需要能够将插入、更新和删除包装在事务中。当我们第一次尝试使用 using(var trans = new TransactionScope()) { ...; 包装代码块时反式.Complete(); },我们收到一个适当的异常,告诉我们需要为远程事务启用网络访问。 我们这样做了,事情开始按照我们预期的方式进行。

快进到今天。我们的应用程序中有一个很少使用的部分也接受了 TransactionScope 处理。尽管事务在我们代码库的所有其他部分都能正常工作,但我们今天发现这个很少使用的部分抛出了与以前相同的“网络访问”异常:

分布式事务管理器 (MSDTC) 的网络访问已被禁用。请使用组件服务管理工具在 MSDTC 的安全配置中启用 DTC 进行网络访问。 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();
    }
}

如果您有任何建议,我们将不胜感激。如果我可以解释更多内容,请告诉我。提前致谢!!

Using ASP.NET 3.5, Linq to SQL, SQL Server 2005 on Windows Server 2003. Running VS 2008 on XP SP3 locally.

We 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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文