public static T ExecuteWithoutRequestTimeout <T>(Func <T> action) { if (HttpContext.Current == null) { return(action()); } else { int oldScriptTimeout = RuntimePlatformUtils.GetScriptTimeout(); try { // ScriptTimeout requires a positive number so we need to set to int.MaxValue to "disable" request timeouts // https://forums.asp.net/t/1925766.aspx?Httpruntime+ExecutionTimeOut+ // https://referencesource.microsoft.com/#system.web/httpserverutility.cs,f8f47afb04ad1133,references RuntimePlatformUtils.SetScriptTimeout(int.MaxValue); return(action()); } finally { RuntimePlatformUtils.SetScriptTimeout(oldScriptTimeout); } } }