惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
MyScale Blog
MyScale Blog
Recent Announcements
Recent Announcements
N
Netflix TechBlog - Medium
GbyAI
GbyAI
Vercel News
Vercel News
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
V
Visual Studio Blog
Martin Fowler
Martin Fowler
腾讯CDC
大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
云风的 BLOG
云风的 BLOG
J
Java Code Geeks
WordPress大学
WordPress大学
P
Proofpoint News Feed
雷峰网
雷峰网
酷 壳 – CoolShell
酷 壳 – CoolShell
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Y
Y Combinator Blog

博客园 - Franz

从如此简单的代码谈起 谈谈C#基元类型 TFS代码签入指导 并发之阿喀琉斯之踵 SmallDateTime时间范围检查 too many automatic redirections were attempted 预览Cube出现没有注册类错误 .NET下的延迟加载 我的VisualStudio工具箱 简单的谈一下.NET下的AOP TFS上使用Beyond Compare来比较源码 SqlBulkCopy 是个好对象 释放Sql Server内存 吐槽一下Silverlight的SaveFileDialog. 书评《模式-工程化实现及扩展》 定义加载动画 代码共享的小技巧 WPF将控件保存为图片 《编程人生》的书评
Transaction Manager Maximum Timeout
Franz · 2013-07-31 · via 博客园 - Franz

2013-07-31 12:43  Franz  阅读(562)  评论()    收藏  举报

TransactionManager.MaximumTimeout是个只读的属性, 默认只有10分钟, 要想修改它必须通过machine.config来修改. 为了单个应用而去修改这个值是不合适的. stackoverflow.com上是给出的解释都是修改machine.config来完成的.

下面我给出一种单个应用独立解决的办法, 方法很简单就是修改这个只读属性.

先通用你自己的工具查看一下代码实现, 我这里是用Reshaper通过从微软那里下载下来的.

/// <summary>
/// Gets the default maximum timeout interval for new transactions.
/// </summary>
/// 
/// <returns>
/// A <see cref="T:System.TimeSpan"/> value that specifies the maximum timeout interval that is allowed when creating new transactions.
/// </returns>
public static TimeSpan MaximumTimeout
{
  get
  {
    if (!TransactionManager._platformValidated)
      TransactionManager.ValidatePlatform();
    if (DiagnosticTrace.Verbose)
      MethodEnteredTraceRecord.Trace(SR.GetString("TraceSourceBase"), "TransactionManager.get_DefaultMaximumTimeout");
    if (!TransactionManager._cachedMaxTimeout)
    {
      lock (TransactionManager.ClassSyncObject)
      {
        if (!TransactionManager._cachedMaxTimeout)
        {
          TransactionManager._maximumTimeout = TransactionManager.MachineSettings.MaxTimeout;
          TransactionManager._cachedMaxTimeout = true;
        }
      }
    }
    if (DiagnosticTrace.Verbose)
      MethodExitedTraceRecord.Trace(SR.GetString("TraceSourceBase"), "TransactionManager.get_DefaultMaximumTimeout");
    return TransactionManager._maximumTimeout;
  }
}

看上去只要读取一些Config文件然后修改_maximumTimeout这个就可以了.实现代码如下:

    private static void ChangeTransactionManagerMaximumTimeout()
    {
        var customMaximumTimeout = TimeSpan.MaxValue;
        var maximumTimeout = TransactionManager.MaximumTimeout;

        FieldInfo fieldInfo = typeof(TransactionManager).GetFields(BindingFlags.NonPublic | BindingFlags.Static).Single(item => item.Name == "_maximumTimeout");
        fieldInfo.SetValue(null, customMaximumTimeout);
        maximumTimeout = TransactionManager.MaximumTimeout;
    }

这样在以后程序内部再次调用TransactionManager.MaximumTimeout就是修改过的timeout了.