मुझे लगता है कि AsyncControllers का उपयोग करने से आपको यहां मदद मिलेगी क्योंकि वे अनुरोध थ्रेड से प्रसंस्करण को ऑफ़लोड करते हैं।
मैं कुछ इस तरह का उपयोग करें (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();
}
}
और सुनिश्चित करें कि आप (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-09 05:10:08
क्या वेब क्लाइंट एसिंक कॉल में टाइमआउट जोड़ने का कोई तरीका है? –
बस एक नोट, ऐसा लगता है कि यह जवाब 0 9 में एमवीसी 1.0 दिनों में पोस्ट किया गया था। अब एमवीसी 2/3 के साथ जवाब थोड़ा अलग है। MapAsyncRoute विधि चली गई है और अब इसकी आवश्यकता नहीं है। साथ ही, MyAction विधि को अब MyActionAsync में बदलना होगा।अन्यथा, सब कुछ एक ही तरीके से काम करता है। – BFree