2013-04-02 38 views
4

我的應用程序已啓用transaction範圍。下面的代碼已被用來激活示波器,它被激活了一個小時。但是,它已經結束了10分鐘。C#中TransactionScope的奇怪行爲

using (TransactionScope scope = new TransactionScope(TransactionScopeOption.Required,TimeSpan.FromHours(1))) 
{ 
    // my code goes here. 
} 

什麼是奇怪的行爲?爲什麼不辜負一個小時的生命時間?

+0

什麼是連接超時?什麼是命令超時? – Oded

+0

連接超時時間爲1小時。什麼是命令超時? – Smaug

+0

在哪個環境中運行?本地,天藍色,...? –

回答

3

TransactionScope的默認最大超時時間爲10分鐘。如果你用更大的超時創建它,無論如何它會默默地將它減少到10分鐘。這是一個相當驚人的行爲,但代碼正是這麼做的:

public TransactionScope(TransactionScopeOption scopeOption, TimeSpan scopeTimeout) 
{ 
    ... 
    timeout = TransactionManager.ValidateTimeout(scopeTimeout); 
    ... 
} 

而且ValidateTimeout它減少到TransactionManager.MaximumTimeout

internal static TimeSpan ValidateTimeout(TimeSpan transactionTimeout) 
{ 
    ... 
    if (TransactionManager.MaximumTimeout != TimeSpan.Zero && (transactionTimeout > TransactionManager.MaximumTimeout || transactionTimeout == TimeSpan.Zero)) 
    { 
     return TransactionManager.MaximumTimeout; 
    } 
    return transactionTimeout; 
} 
+0

你看過代碼嗎? 'TimeSpan.FromHours(1)',特別是? – Oded

+1

它在哪裏記錄了一個'TransactionScope'會將timout截斷爲10分鐘?不在這裏:http://msdn.microsoft.com/en-us/library/ms149852.aspx –

+0

是的。亞歷克斯你是絕對正確的。它會靜靜地結束10分鐘。我怎樣才能改善超時? – Smaug

4

你必須在擴展默認事務超時您app.config(或web.config):

<configuration> 
<system.transactions> 
    <defaultSettings timeout="00:03:00" /> 
</system.transactions> 
</configuration> 

您還必須在機器級別更改配置,即您必須編輯machine.config文件。機器級別的默認最大事務超時時間爲10 minutes

<system.transactions> 
    <machineSettings maxTimeout="100.23:59:59" /> 
    </system.transactions> 

machine.config文件位於C:\Windows\Microsoft.NET\Framework\[framework version]\Config目錄:

例如,我在我的machine.config文件以下XML的末尾。

+0

我找不到這10分鐘的任何文檔,你有鏈接嗎?編輯:也許這:http://msdn.microsoft.com/en-us/library/system.transactions.configuration.machinesettingssection.maxtimeout.aspx但是,此線程建議您不能覆蓋它在應用程序級別:http:// social.msdn.microsoft.com/Forums/en-US/windowstransactionsprogramming/thread/584b8e81-f375-4c76-8cf0-a5310455a394 –

+0

不幸的是,它無法解決我的問題。我已經更新web.config以及machine.config。但沒有希望它不能解決我的問題仍存在同樣的問題。 – Smaug

+0

@TimSchmelter以下是應用程序級別:http://msdn.microsoft.com/en-us/library/system.transactions.configuration.defaultsettingssection.timeout.aspx machinesettings應該在machine.config中更新。 – ken2k

1

你在交易中的代碼中做什麼?通常,其他超時會發揮作用,例如SQL連接或命令超時,這可能比事務超時短。