Reputation: 65
I have tried the following so far- 1) Added Custom attribute for handling timeout.
public class TimeOutFilter : ActionFilterAttribute
{
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
System.Web.HttpContext.Current.GetType().GetField("_timeoutState", System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic).SetValue(System.Web.HttpContext.Current, 1);
base.OnActionExecuting(filterContext);
}
}
Added following in Global.asax
filters.Add(new TimeOutFilter());
Used [TimeOutFilter] attribute to decorate the contoller
2) Added AsyncTimeout attribute to decorate the controller/Action as follows:
[AsyncTimeout(10)]
3) Set executionTimeout to a value in web.config file.
<httpRuntime targetFramework="4.5" maxRequestLength="10240" executionTimeout="10" />
4) Set HttpContext.Server.ScriptTimeout to some value in the controller.
HttpContext.Server.ScriptTimeout = 10;
Using the steps in point 1, I was able to redirect the user to Login screen on request timeout. But this is not what I am looking for.
I am looking for getting or triggering an exception when the request timeout happens.
Can somebody please let me know if there is any way we can do this?
Upvotes: 4
Views: 5810
Reputation: 1352
Don't use the rude thread abort behavior (1) and (4).
The AsyncTimeout Attribute (2) is good but just declares (like executionTimeout in web.config (3)) the timeout duration.
In the fully async pipeline of mvc5 is just one efficient way to timeout an operation: Throw by using the CancellationTokens!
You can get these tokens by parameter or with the Request.TimedOutToken and Response.ClientDisconnectedToken properties.
This blog post helped me a lot.
Upvotes: 1