'交易已中止'交易完成后出错

时间:2018-02-03 06:20:53

标签: c# transactionscope

在以下方法中,我想上传图像并将其保存到服务器目录,然后将记录(将该图像的路径作为字段值)插入到事务范围内的数据库中。但是在交易完成后(在ts.Complete()的下一行 - 当第一个交易范围使用与{完成的语句时)我会收到此错误:'交易已中止'。我该如何解决?

这是我的方法:

    private void Save( String fullPath, Uploader DocImageUploader, string docImageFileName, htPinPadDelivery pinPadDelivery, User user, Enumes.ActionType actionType)
    {
        BasketResult<bool> rtnBasketResult = new BasketResult<bool>();
        fullPath += "\\";

        using (var ts = new TransactionScope(TransactionScopeOption.Required, new TransactionOptions()
        {
            IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted,
            Timeout = new TimeSpan(0, 1, 0)
        }))
        {
            bool transactionIsSuccess = true;

            if (!Directory.Exists(fullPath))
            {
                String path = Helper.GetFileName(fullPath);
                Directory.CreateDirectory(path);
            }

            //Upload image to a server directory:
            transactionIsSuccess = UploadFile(DocImageUploader, fullPath, docImageFileName, user);

            //Insert to database
            if (transactionIsSuccess)
            {
                pinPadDelivery.UploadedDocImage = docImageFileName;

                BasketResult<Boolean> basketResultOfSave = PinPadDeliveriesSave(actionType, pinPadDelivery, user.UserID);
                transactionIsSuccess = basketResultOfSave.IsSuccess;
                if (!transactionIsSuccess)
                    basketResultOfSave.persianErrorMessages.Add(Messages.Err_UploadFileFaild);
            }
            else
                transactionIsSuccess = false;

            using (TransactionScope outOfTransactionScope = new TransactionScope(TransactionScopeOption.Suppress)) 
            {
                rtnBasketResult.IsSuccess = transactionIsSuccess;
                rtnBasketResult.Result.Add(transactionIsSuccess);
                if (!transactionIsSuccess)
                    rtnBasketResult.persianErrorMessages.Add(Messages.Err_UploadFileFaild);

                outOfTransactionScope.Complete();
            }

            if (transactionIsSuccess)
                ts.Complete();
        }

    }

通过更深入的回顾,我知道主要的错误是:

  

已禁用分布式事务管理器(MSDTC)的网络访问。请使用组件服务管理工具在MSDTC的安全配置中启用DTC以进行网络访问。

描述:我在vs中调试我的代码,并连接到其他服务器以从数据库获取数据。数据库服务器MSDTC已根据this link

进行设置

0 个答案:

没有答案