Reputation: 2814
I have the following scenario: I am calling to my controller Index function,the controller in order to generate the model that it pass to the view have to call some web services to get the data here is the code(without real names):
public ActionResult Index()
{
var viewModel = new SomeViewModel();
viewModel.SomeData1=wcfProxy.Function1();
viewModel.SomeData2=wcfProxy.Function2();
viewModel.SomeData3=wcfProxy.Function3();
return View(viewModel);
}
My problem is that it is took long time without get page content in parallel look at this screen shot:
you can see took 14.5 seconds until it start to load the scripts in parallel with simultaneous connections. this is really annoying! How can I reduce the load time?
Upvotes: 1
Views: 223
Reputation: 350
The best option may be just to return the view with out the data and then get the page to load the 'slow' data async using ajax calls.
So your controller would look something like this:
public ActionResult Index()
{
var viewModel = new SomeViewModel();
return View(viewModel);
}
public ActionResult DataOne()
{
var data =wcfProxy.Function1();
return JSON(data,, JsonRequestBehavior.AllowGet);
}
...
You should then be able to use javascript to retrieve and display the data. The example below uses the JQUery library.
$.get("/ControllerName/DataOne").done( function (data) {
//display data.
});
The JQuery docs, which can be found JQuery Docs, are really useful and have lots of examples.
Upvotes: 2
Reputation: 13600
We don't know. You haven't shown us any code so far, only a bunch of some generic method calls. You either have to optimize the WCF methods
so they don't take 15s (the best solution, because 15s is really ridiculous), or at least make your logic parallel/async
(for example use ajax).
This is a nice link for parallel programming in c# and .NET4 http://blogs.msdn.com/b/csharpfaq/archive/2010/06/01/parallel-programming-in-net-framework-4-getting-started.aspx
The main idea is to start loading the data without waiting for the previous method call. At the moment your functions function2 and function3 wait for the previous ones to finish. If you call them in parallel, the final time will be shorter. It's something like emptying a tube with buckets. More buckets take the water out at the same time, the faster the process is.
With web development you can use ajax as well. The basic idea is to send the page to the client without these data, wait for the page to load and then use ajax to load the content asynchronously (don't forget to notify the client that the data are still being loaded).
For instance jQuery is great for this: http://api.jquery.com/jquery.ajax/
Upvotes: 2
Reputation: 6366
You wrote:
without get page content in parallel
Shouldn't you then simply make your code parallel? E.g. by using async/await keywords with TPL support? Otherwise each call to WCF will be always run one by one.
Upvotes: 1