pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

如何在 C# 中使用 TransactionScope?

How to use TransactionScope in C#?(如何在 C# 中使用 TransactionScope?)
本文介紹了如何在 C# 中使用 TransactionScope?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在嘗試使用 TransactionScope,但不斷收到以下異常.
如果重要的話,該應用程序運行在與數(shù)據庫不同的機器上.我使用的是 SQL Server 2005.

<塊引用>

分布式事務管理器 (MSDTC) 的網絡訪問已被禁用.請在安全配置中為網絡訪問啟用 DTCMSDTC 使用組件服務管理工具.

using (TransactionScope tsTransScope = new TransactionScope()){//在這里做事tsTransScope.Complete();}

編輯

我根據反饋做了一些更改.現(xiàn)在我收到此錯誤:

<塊引用>

調用 COM 組件時返回了錯誤 HRESULT E_FAIL."
與底層事務管理器的通信失敗."

解決方案我認為接受的答案解決了我遇到的最初問題.第二個錯誤似乎特定于實體框架.我會為此發(fā)布另一個問題.

以下是客戶端的屬性:
有這個有趣的建議:

<塊引用>

另一個配置設置需要注意(雖然我認為這是一個不常見的情況)是RestrictRemoteClients 注冊表項.如果此鍵的值設置為 2(RPC_RESTRICT_REMOTE_CLIENT_HIGH) 然后MSDTC 網絡交易不會被能夠正常工作.MSDTC 支持只有 RPC_RESTRICT_REMOTE_CLIENT_NONE(0) 和RPC_RESTRICT_REMOTE_CLIENT_DEFAULT (1)值.看http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx#XSLTsection128121120120有關更多信息限制遠程客戶端.

最后,雖然不是特定于您的問題,但使用 TransactionScope 類時需要注意的一個非常重要的事情是它的默認設置是使用 可序列化的事務隔離級別.Serializable 是限制性最強的隔離級別,坦率地說,它被選為默認級別令人驚訝.如果您不需要這種級別的鎖定,我強烈建議在實例化 TransactionScope 時將隔離級別設置為限制較少的選項 (ReadCommitted):

var scopeOptions = new TransactionOptions();scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;scopeOptions.Timeout = TimeSpan.MaxValue;使用 (var scope = new TransactionScope(TransactionScopeOption.Required,范圍選項)){//你的代碼在這里}

I am trying to use TransactionScope, but keep getting the exception below.
The app is running on a different machine than the database, if that matters. I am using SQL Server 2005.

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 (TransactionScope tsTransScope = new TransactionScope())
{
    //Do stuff here
    tsTransScope.Complete();
}

Edit

I made some changes based on the feedback. Now I'm getting this error:

"Error HRESULT E_FAIL has been returned from a call to a COM component."
"Communication with the underlying transaction manager has failed."

Solution I think the accepted answer fixed the initial issue I was getting. The 2nd error seems to be specific to Entity Framework. I'll post another question for it.

Here are the properties on the client:
Client http://www.portnine.com/data/images/Misc/client.jpg

Here are the properties on the server:
Server http://www.portnine.com/data/images/Misc/server.jpg

解決方案

You need to enable network DTC access as described in this Microsoft TechNet Article. This change may have to be made on both the database and application servers. Often times DTC is already turned on a database server so I'd look at the application server first.

Here is a screen shot of what we use except for the "Allow Remote Administration" option:

I have not run into the HRESULT E_Fail issue you are now having but this article on XP SP2 and transactions had this interesting suggestion:

Another configuration setting that you need to be aware (although I consider it to be an uncommon scenario) is RestrictRemoteClients registry key. If the value of this key is set to 2 (RPC_RESTRICT_REMOTE_CLIENT_HIGH) then MSDTC network transactions will not be able to work properly. MSDTC supports only RPC_RESTRICT_REMOTE_CLIENT_NONE (0) and RPC_RESTRICT_REMOTE_CLIENT_DEFAULT (1) values. See http://www.microsoft.com/technet/prodtechnol/winxppro/maintain/sp2netwk.mspx#XSLTsection128121120120 for more info on RestrictRemoteClients.

Finally, while not specific to your issue a very important thing to note about using the TransactionScope class is that its default setting is to utilize a Transaction Isolation Level of Serializable. Serializable is the most restrictive of the isolation levels and frankly its surprising that it was chosen as the default. If you do not need this level of locking I would highly recommend setting the isolation level to a less restrictive option (ReadCommitted) when instantiating a TransactionScope:

var scopeOptions = new TransactionOptions();
scopeOptions.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
scopeOptions.Timeout = TimeSpan.MaxValue;

using (var scope = new TransactionScope(TransactionScopeOption.Required,
    scopeOptions))
{
    // your code here
}

這篇關于如何在 C# 中使用 TransactionScope?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯(lián)網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

LINQ to SQL and Concurrency Issues(LINQ to SQL 和并發(fā)問題)
SQL Server 2005 Transaction Level and Stored Procedures(SQL Server 2005 事務級和存儲過程)
Yield return from a try/catch block(try/catch 塊的收益回報)
Should I call Parameters.Clear when reusing a SqlCommand with a transation?(重用帶有事務的 SqlCommand 時,我應該調用 Parameters.Clear 嗎?)
Does SqlTransaction need to have Dispose called?(SqlTransaction 是否需要調用 Dispose?)
Reason for System.Transactions.TransactionInDoubtException(System.Transactions.TransactionInDoubtException 的原因)
主站蜘蛛池模板: 机器视觉检测系统-视觉检测系统-机器视觉系统-ccd检测系统-视觉控制器-视控一体机 -海克易邦 | 青岛代理记账_青岛李沧代理记账公司_青岛崂山代理记账一个月多少钱_青岛德辉财税事务所官网 | 济南保安公司加盟挂靠-亮剑国际安保服务集团总部-山东保安公司|济南保安培训学校 | 361°官方网站| 超细粉碎机|超微气流磨|气流分级机|粉体改性设备|超微粉碎设备-山东埃尔派粉碎机厂家 | 恒温振荡混匀器-微孔板振荡器厂家-多管涡旋混匀器厂家-合肥艾本森(www.17world.net) | 郑州墨香品牌设计公司|品牌全案VI设计公司 | 山东螺杆空压机,烟台空压机,烟台开山空压机-烟台开山机电设备有限公司 | 创富网-B2B网站|供求信息网|b2b平台|专业电子商务网站 | 诚暄电子公司首页-线路板打样,pcb线路板打样加工制作厂家 | 今日热点_实时热点_奇闻异事_趣闻趣事_灵异事件 - 奇闻事件 | 实木家具_实木家具定制_全屋定制_美式家具_圣蒂斯堡官网 | 伊卡洛斯软装首页-电动窗帘,别墅窗帘,定制窗帘,江浙沪1000+别墅窗帘案例 | 钢骨架轻型板_膨石轻型板_钢骨架轻型板价格_恒道新材料 | 广州办公室设计,办公室装修,写字楼设计,办公室装修公司_德科 | CE认证_产品欧盟ROHS-REACH检测机构-商通检测 | 偏心半球阀-电动偏心半球阀-调流调压阀-旋球阀-上欧阀门有限公司 | 气体检测仪-氢气检测仪-可燃气体传感器-恶臭电子鼻-深国安电子 | 双段式高压鼓风机-雕刻机用真空泵-绍兴天晨机械有限公司 | 深圳离婚律师咨询「在线免费」华荣深圳婚姻律师事务所专办离婚纠纷案件 | 工业PH计|工业ph酸度计|在线PH计价格-合肥卓尔仪器仪表有限公司 济南画室培训-美术高考培训-山东艺霖艺术培训画室 | 众品家具网-家具品牌招商_家具代理加盟_家具门户的首选网络媒体。 | 干洗店加盟_洗衣店加盟_干洗店设备-伊蔻干洗「武汉总部」 | 刮板输送机,粉尘加湿搅拌机,螺旋输送机,布袋除尘器 | 暴风影音| 万烁建筑设计院-建筑设计公司加盟,设计院加盟分公司,市政设计加盟 | 展厅设计公司,展厅公司,展厅设计,展厅施工,展厅装修,企业展厅,展馆设计公司-深圳广州展厅设计公司 | 期货软件-专业期货分析软件下载-云智赢 | 中视电广_短视频拍摄_短视频推广_短视频代运营_宣传片拍摄_影视广告制作_中视电广 | 100_150_200_250_300_350_400公斤压力空气压缩机-舰艇航天配套厂家 | 澳门精准正版免费大全,2025新澳门全年免费,新澳天天开奖免费资料大全最新,新澳2025今晚开奖资料,新澳马今天最快最新图库-首页-东莞市傲马网络科技有限公司 | 特种阀门-调节阀门-高温熔盐阀-镍合金截止阀-钛阀门-高温阀门-高性能蝶阀-蒙乃尔合金阀门-福建捷斯特阀门制造有限公司 | 影像测量仪_三坐标测量机_一键式二次元_全自动影像测量仪-广东妙机精密科技股份有限公司 | 南京PVC快速门厂家南京快速卷帘门_南京pvc快速门_世界500强企业国内供应商_南京美高门业 | 诸城网站建设-网络推广-网站优化-阿里巴巴托管-诸城恒泰互联 | 厂房出售_厂房仓库出租_写字楼招租_土地出售-中苣招商网-中苣招商网 | 金属回收_废铜废铁回收_边角料回收_废不锈钢回收_废旧电缆线回收-广东益夫金属回收公司 | elisa试剂盒-PCR试剂盒「上海谷研实业有限公司」 | 学校用栓剂模,玻璃瓶轧盖钳,小型安瓿熔封机,实验室安瓿熔封机-长沙中亚制药设备有限公司 | 智能汉显全自动量热仪_微机全自动胶质层指数测定仪-鹤壁市科达仪器仪表有限公司 | 焦作网 WWW.JZRB.COM |