views:

392

answers:

2

I have this webservice which is sucking in data about exchange rates, my problem with it is it takes alot of time to load. So i want to get the data loaded async but when i call the GetRatesASync() method it says

"Asynchronous operations are not allowed in this context. Page starting an asynchronous operation has to have the Async attribute set to true and an asynchronous operation can only be started on a page prior to PreRenderComplete event."

Oh by the way im using a soap service and an MVC UserControl if that helps at all and i placed the GetRatesASync() method in protected override void OnPreRender(EventArgs e). So how do I got about getting this bloody data ASync with jquery after the page is loaded. And is there a way to make this getting data thing faster

A: 

If you want async in MVC you could use the AsyncController included in ASP.NET MVC Futures.

But async operation won't make your webservice any faster... it will only prevent it from blocking your thread.

Mauricio Scheffer
A: 

Using jquery, how about: 1) wait for the page to load in the browser; 2) call a javascript function that makes an asynchronous call to the server; 3) wait for the result; 4) when the result arrives from the server, display it in the browser?

Something like:

Javascript/jquery in web page:

$().ready(GetExchangeRates);
function GetExchangeRates()
{
    // TODO: Maybe show a *loading* animation.
    $.get("/Currency/GetExchangeRates",
        null, GetExchangeRatesCompleted, "json");
}
function GetExchangeRatesCompleted(result)
{
    // TODO: Hide *loading* animation.
    // TODO: Display result on page.
}

Controller action (C#):

public class CurrencyController : Controller
{
    public JsonResult GetExchangeRates()
    {
        var exchangeRates = _slowWebService.GetExchangeRates();
        return Json(exchangeRates);
    }
}


Another thing:

If you can't cache the exchange rates, how about saving them in an xml file in the App_Data folder, with an entry that says when the exchange rates were last retrieved from the slow webservice, and then refreshing that xml file when it's older than some specified time interval?

Ole Lynge