Asp.net mvc MVC3中的错误-请求从不超时。适用于同一项目中的aspx页面
我在我们的生产现场以及一个小的测试现场看到了这一点,我只是为了测试这个 基本上,mvc处理的请求似乎从未超时。我在web.config中设置了executionTimeout并关闭了调试模式。然后,我将一个无限循环thread.sleeps添加到常规aspx页面和mvc页面(该循环位于mvc页面的控制器中)。aspx页面可靠地超时(HttpException(0x80004005):请求超时),但mvc页面只是永远旋转而没有超时 mvc是否有单独的设置(我已经查看了,但没有找到)?mvc请求是否默认不超时 在此方面的任何帮助都将不胜感激。如果它能帮助任何人的话,我很乐意通过电子邮件发送我的小测试站点 编辑:我正在使用MVC3 my web.config的内容:Asp.net mvc MVC3中的错误-请求从不超时。适用于同一项目中的aspx页面,asp.net-mvc,iis-7,timeout,Asp.net Mvc,Iis 7,Timeout,我在我们的生产现场以及一个小的测试现场看到了这一点,我只是为了测试这个 基本上,mvc处理的请求似乎从未超时。我在web.config中设置了executionTimeout并关闭了调试模式。然后,我将一个无限循环thread.sleeps添加到常规aspx页面和mvc页面(该循环位于mvc页面的控制器中)。aspx页面可靠地超时(HttpException(0x80004005):请求超时),但mvc页面只是永远旋转而没有超时 mvc是否有单独的设置(我已经查看了,但没有找到)?mvc请求是否
<?xml version="1.0"?>
<!--
For more information on how to configure your ASP.NET application, please visit
http://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add name="ApplicationServices"
connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnetdb.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="webpages:Enabled" value="true" />
</appSettings>
<system.web>
<httpRuntime maxRequestLength="16384" executionTimeout="30" />
<compilation debug="false" targetFramework="4.0">
<assemblies>
<add assembly="System.Web.Abstractions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Helpers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Routing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.Mvc, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<add assembly="System.Web.WebPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
</assemblies>
</compilation>
<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>
<membership>
<providers>
<clear/>
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear/>
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear/>
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
</configuration>
当满足以下条件时,它应该工作: 1) 域名不是localhost(要测试超时,应该使用“YourComputerName”而不是“localhost”) 2) 项目是在发布模式下编译的 3) 编译调试=“false” 如果没有,请在此处查找其他选项(ScriptTimeOut): 您好,
爸爸我想我找到了原因: 此方法位于WrappedAsyncResult类中,MvcHandler类通过BeginProcessRequest使用该类:
public static IAsyncResult BeginSynchronous<TResult>(AsyncCallback callback, object state, Func<TResult> func, object tag)
{
BeginInvokeDelegate beginDelegate = delegate (AsyncCallback asyncCallback, object asyncState) {
SimpleAsyncResult result = new SimpleAsyncResult(asyncState);
result.MarkCompleted(true, asyncCallback);
return result;
};
EndInvokeDelegate<TResult> endDelegate = _ => func();
WrappedAsyncResult<TResult> result = new WrappedAsyncResult<TResult>(beginDelegate, endDelegate, tag);
result.Begin(callback, state, -1);
return result;
}
编辑:我们想出了一种笨拙的方法来强制MVC控制器动作“超时”,尽管这种机制有点野蛮:
public class TimeoutController : Controller
{
private bool _isExecuting = false;
private int _controllerTimeout = 5000;
private Thread _executingThread;
private readonly object _syncRoot = new object();
protected override void ExecuteCore()
{
_executingThread = Thread.CurrentThread;
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(_controllerTimeout);
if (_isExecuting)
{
_executingThread.Abort();
}
});
base.ExecuteCore();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
_isExecuting = true;
base.OnActionExecuting(filterContext);
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
_isExecuting = false;
base.OnActionExecuted(filterContext);
}
public int ControllerTimeout
{
get
{
int retVal;
lock(_syncRoot)
{
retVal = _controllerTimeout;
}
return retVal;
}
set
{
lock(_syncRoot)
{
_controllerTimeout = value;
}
}
}
}
在MVC4中仍然发生在我身上。我已将此作为错误提交给microsoft: 更新: Microsoft发表了以下评论: 不建议在MVC中使用执行超时功能 应用。您可以改为将HttpContext.Server.ScriptTimeout设置为 所需的超时值。尽管名称不同,但这是每个请求的一个示例 设置并应应用于任何ASP.NET请求(名称“script”为 (误导性)
.这也发生在我身上!!我很惊讶我没有看到其他人抱怨这个问题!这有什么意义?这在真实场景中是否确实发生过?您是否找到了解决此问题的方法?感谢您的回复,但这似乎并没有解决此问题。我现在使用的是我的计算机名而不是localhost,我在visual studio的发布模式下编译了它,并在我的web.config中将debug设置为false。我还尝试在mvc页面上设置scriptTimeout属性,但这也没有帮助。我认为这可能是框架中的一个bug。您已经在另一个SO线程中看到关于路径中没有~的评论了吗?也许这是你的问题。我同意它应该在调试中工作。我没有使用位置标记,所以没有路径。我只想让这个超时在mvc和aspx页面的全局级别上工作。在我当前的设置中,它对aspx页面工作正常,但mvc页面不会超时。您能解释一下ControllerTimeout在哪里、如何以及做什么吗?基本上,它强制执行控制器的线程在指定的超时时间后中止;有点像烧毁你的房子杀死一只蜘蛛。微软说用ScriptTimeout代替。
public class TimeoutController : Controller
{
private bool _isExecuting = false;
private int _controllerTimeout = 5000;
private Thread _executingThread;
private readonly object _syncRoot = new object();
protected override void ExecuteCore()
{
_executingThread = Thread.CurrentThread;
ThreadPool.QueueUserWorkItem(o =>
{
Thread.Sleep(_controllerTimeout);
if (_isExecuting)
{
_executingThread.Abort();
}
});
base.ExecuteCore();
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
_isExecuting = true;
base.OnActionExecuting(filterContext);
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
_isExecuting = false;
base.OnActionExecuted(filterContext);
}
public int ControllerTimeout
{
get
{
int retVal;
lock(_syncRoot)
{
retVal = _controllerTimeout;
}
return retVal;
}
set
{
lock(_syncRoot)
{
_controllerTimeout = value;
}
}
}
}