8

我有一个 ASP.NET MVC 应用程序,它当前使用 WebClient 类从控制器操作中对外部 Web 服务进行简单调用。

目前我正在使用同步运行的 DownloadString 方法。我遇到了外部 Web 服务无响应的问题,这导致我的整个 ASP.NET 应用程序线程匮乏且无响应。

解决此问题的最佳方法是什么?有一个 DownloadStringAsync 方法,但我不确定如何从控制器调用它。我需要使用 AsyncController 类吗?如果是这样,AsyncController 和 DownloadStringAsync 方法如何交互?

谢谢您的帮助。

4

2 回答 2

7

我认为使用 AsyncControllers 将帮助您,因为它们可以将处理从请求线程中卸载。

我会使用这样的东西(使用本文中描述的事件模式):

public class MyAsyncController : AsyncController
{
    // The async framework will call this first when it matches the route
    public void MyAction()
    {
        // Set a default value for our result param
        // (will be passed to the MyActionCompleted method below)
        AsyncManager.Parameters["webClientResult"] = "error";
        // Indicate that we're performing an operation we want to offload
        AsyncManager.OutstandingOperations.Increment();

        var client = new WebClient();
        client.DownloadStringCompleted += (s, e) =>
        {
            if (!e.Cancelled && e.Error == null)
            {
                // We were successful, set the result
                AsyncManager.Parameters["webClientResult"] = e.Result;
            }
            // Indicate that we've completed the offloaded operation
            AsyncManager.OutstandingOperations.Decrement();
        };
        // Actually start the download
        client.DownloadStringAsync(new Uri("http://www.apple.com"));
    }

    // This will be called when the outstanding operation(s) have completed
    public ActionResult MyActionCompleted(string webClientResult)
    {
        ViewData["result"] = webClientResult;
        return View();
    }
}

并确保您设置了您需要的任何路线,例如(在 Global.asax.cs 中):

public class MvcApplication : System.Web.HttpApplication
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapAsyncRoute(
            "Default",
            "{controller}/{action}/{id}",
            new { controller = "Home", action = "Index", id = "" }
        );
    }
}
于 2009-08-09T05:10:08.250 回答
3

DownloadStringAsync 方法使用事件模型,完成后引发DownloadStringCompleted。如果请求时间过长,您也可以通过调用来停止请求WebClient.CancelAsync()。这将使您的主请求线程和您的 WebClient 线程并行运行,并允许您准确地决定您希望主线程在返回之前等待多长时间。

在下面的示例中,我们启动下载并设置我们希望在下载完成时调用的事件处理程序。DownloadStringAsync 立即返回,因此我们可以继续处理请求的其余部分。

为了演示对此操作的更精细控制,当我们到达控制器操作的末尾时,我们可以检查下载是否完成;如果没有,再给它 3 秒,然后中止。

string downloadString = null;

ActionResult MyAction()
{
    //get the download location
    WebClient client = StartDownload(uri);
    //do other stuff
    CheckAndFinalizeDownload(client);
    client.Dispose();
}

WebClient StartDownload(Uri uri)
{
    WebClient client = new WebClient();
    client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(Download_Completed);
    client.DownloadStringAsync(uri);
    return client;
}

void CheckAndFinalizeDownload(WebClient client)
{
    if(this.downloadString == null)
    {
        Thread.Sleep(3000);
    }
    if(this.downloadString == null)
    {
        client.CancelAsync();
        this.downloadString = string.Empty;
    }
}

void Download_Completed(object sender, DownloadStringCompletedEventArgs e)
{
    if(!e.Cancelled && e.Error == null)
    {
        this.downloadString = (string)e.Result;
    }
}
于 2009-08-08T16:00:10.670 回答