【问题标题】:How to deliberately return a page slowly in IIS如何故意在IIS中缓慢返回页面
【发布时间】:2013-03-06 09:50:15
【问题描述】:

我在 IIS 7.5 上运行 MVC4 应用程序,在某些情况下我想减慢页面的响应时间。以用户尝试自行注册为例。

成功后,使用有效的新用户名和密码,我希望页面立即响应。如果失败,当尝试使用预先存在的用户名注册时,我希望页面将响应速度减慢到大约 15 秒。

在框架内执行此操作的最佳方法是什么,以无线程/资源重的方式延迟 HTTP 响应。

【问题讨论】:

    标签: iis asp.net-mvc-4 iis-7.5


    【解决方案1】:

    我相信最简单的解决方案是在发生错误时让当前线程休眠 15 秒。您的代码将如下所示:

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            // TODO: do something to determine if the action is a success or not
            var error = true;
    
            if (error)
            {
                Thread.Sleep(TimeSpan.FromSeconds(15));
            }
    
            return this.View();
        }
    }
    

    编辑:或者可能是异步版本:

    public class HomeController : Controller
    {
        public async Task<ActionResult> Index()
        {
            // TODO: do something to determine if the action is a success or not
            var error = true;
    
            if (error)
            {
                await Task.Delay(TimeSpan.FromSeconds(15));
            }
    
            return this.View();
        }
    }
    

    【讨论】:

    • 感谢您的回答,您是对的,这将很容易编码。但是,这将占用我的线程,并且 Web 服务器上的并发使用将是性能密集型的,因为我根据 stackoverflow.com/questions/3886171/… 启动更多线程以补偿睡眠线程。我正在寻找线程/性能密集度较低的东西。
    • 您使用的是 .NET 4.5 吗?如果是,您可以使用 aysnc 操作。请参阅更新的示例。这将导致在等待开始时处理该操作的线程返回给 IIS。
    • 很有趣,我去看看。
    猜你喜欢
    • 2018-09-03
    • 1970-01-01
    • 2012-11-25
    • 2016-05-03
    • 2019-05-04
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    相关资源
    最近更新 更多