Creo que el uso de AsyncControllers le ayudará aquí ya que descargan el procesamiento del subproceso de solicitud.
que haría uso de algo como esto (utilizando el patrón de eventos como se describe en this article):
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();
}
}
y asegúrese de que la configuración sea cual sea rutas que necesita, por ejemplo (en 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 = "" }
);
}
}
¿hay alguna manera de agregar un tiempo de espera a la llamada asincrónica del cliente web? –
Solo una nota, parece que esta respuesta se publicó en 09 en el MVC 1.0 días. Ahora con MVC 2/3 la respuesta es LIGERAMENTE diferente. El método MapAsyncRoute se ha ido y ya no es necesario. Además, el método MyAction necesita ser ahora renombrado como MyActionAsync.De lo contrario, todo funciona de la misma manera. – BFree