使用 Global.asax 中的 Application_Start() 方法启动后 Web 服务冻结

Web Service freezes after startup with Application_Start() method in Global.asax

我正在使用 Web 服务方法返回一个列表,该列表在我的 Web 服务启动后执行了大约 40 秒(该方法从 Global.asax 中的 Application_Start() 事件启动)。

问题是 Web 服务在此方法需要的时间内被冻结,我的应用程序在获得此列表之前不会启动(实际上它启动了,但也被冻结并且甚至不显示表单).

有什么办法可以解决吗?也许异步调用 Application_Start() 中的这个方法?任何帮助将不胜感激。

Global.asax中的代码:

protected void Application_Start(object sender, EventArgs e)
    {
        WebService WS = new WebService();
        WS.RecursiveFileProcessor();
    }

您可以将该机制作为单独的任务启动:

protected void Application_Start(object sender, EventArgs e)
{
    Task.Factory.StartNew(() => 
    {
        WebService WS = new WebService();
        WS.RecursiveFileProcessor();
    });
}

这应该会停止您的服务挂起。