Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simulating timeout on ASP.NET MVC

Problem:

I need to create a web project with a controller that times out.

What I have done:

  • Create a new web application
  • Empty the web.config, and write the values below:

.

<?xml version="1.0"?>
<configuration>
    <system.web>
        <httpRuntime executionTimeout="1" />
        <compilation debug="false" />
        <httpModules>
            <add name="UrlRoutingModule" type="System.Web.Routing.UrlRoutingModule, System.Web.Routing, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
        </httpModules>
    </system.web>
</configuration>
  • Write the following code to the controller:

.

public class DefaultController : Controller
{
    public EmptyResult Index()
    {
        System.Threading.Thread.Sleep(3000);
        Response.Write("ScriptTimeout: " + HttpContext.Server.ScriptTimeout);
        return new EmptyResult();
    }
}

When run, the server sleeps for 3 seconds, then returns the response without any timeout error. ScriptTimeout value is 1.

Question:

Any idea what went wrong?

like image 601
Adrian Godong Avatar asked Dec 30 '25 20:12

Adrian Godong


2 Answers

Do you want a 408 Request Timeout HTTP status code?

public ActionResult Index()
{
    return new HttpStatusCodeResult(408);
}

http://en.wikipedia.org/wiki/List_of_HTTP_status_codes

like image 101
Ryan Avatar answered Jan 02 '26 10:01

Ryan


The executionTimeout attribute on the <httpRuntime/> element is only taken into account if debug="false" on the <compilation/> element.

Try removing the debug attribute or explicitly setting it to false. Of course, you'll have to run your application without debugging to test it.

See http://msdn.microsoft.com/en-gb/library/vstudio/e1f13641(v=vs.100).aspx for more details.

like image 42
James Simm Avatar answered Jan 02 '26 10:01

James Simm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!